Skip to content

Commit

Permalink
Merge branch 'master' into fix_test_build
Browse files Browse the repository at this point in the history
  • Loading branch information
gracekarina authored Sep 23, 2023
2 parents a9db06e + d2e6b3a commit 011c06e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ public SwaggerParseResult deserialize(JsonNode rootNode, String path, ParseOptio
result.setOpenAPI(api);
result.setMessages(rootParse.getMessages());
} catch (Exception e) {
result.setMessages(Arrays.asList(e.getMessage()));
if (StringUtils.isNotBlank(e.getMessage())) {
result.setMessages(Arrays.asList(e.getMessage()));
} else {
result.setMessages(Arrays.asList("Unexpected error deserialising spec"));
}
}
return result;
}
Expand Down Expand Up @@ -343,10 +347,13 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
* e.g. #/components/schemas/foo/properties/bar
*/
for (String schema : localSchemaRefs.keySet()) {
if (components.getSchemas().get(schema) == null) {
if (components.getSchemas() == null){
result.missing(localSchemaRefs.get(schema), schema);
} else if (components.getSchemas().get(schema) == null) {
result.invalidType(localSchemaRefs.get(schema), schema, "schema", rootNode);
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
package io.swagger.v3.parser.test;


import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.SpecVersion;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.parser.OpenAPIV3Parser;
import org.testng.annotations.Test;

import java.util.HashMap;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.AssertJUnit.assertEquals;

public class OAIDeserializationTest {
Expand Down Expand Up @@ -42,4 +60,81 @@ public void testIssue911() {
assertEquals(result.getMessages().size(),1);
assertNotNull(result.getOpenAPI());
}

@Test
public void testDeserializeYamlDefinitionMissingSchema_Issue1951() throws Exception {
// Create Spec missing schema but with schema items referenced in responses
OpenAPI api = new OpenAPI();
api.setSpecVersion(SpecVersion.V30);
api.setComponents(new Components()); // no schema
ApiResponse errorResponse = new ApiResponse().content(new Content());
errorResponse
.description("My error type")
.getContent()
.addMediaType("application/json", new MediaType()
.schema(new ComposedSchema().
$ref("NotAddedYet")
.type("schema")));
api.getComponents().setResponses(new HashMap<>());
api.getComponents().getResponses().put("ErrorObj", errorResponse);
ComposedSchema schema = new ComposedSchema();
schema.set$ref("ThingRequest");
schema.setType("schema");
Content content = new Content().addMediaType("application/json", new MediaType().schema(schema));
ApiResponses resp = new ApiResponses();
resp.addApiResponse("401", new ApiResponse().$ref("#/components/responses/ErrorObj"));
resp.setDefault(new ApiResponse()
.description("Default response is just a string")
.content(new Content()
.addMediaType("application/json", new MediaType().schema(new StringSchema()))
)
);
api.setPaths(new Paths()
.addPathItem("/thingy", new PathItem()
.post(new Operation()
.responses(resp)
.requestBody(new RequestBody().content(content))
)
)
);

String yaml = Yaml.mapper().writeValueAsString(api);

// Generated YAML. Intentionally missing schemas
// openapi: 3.0.1
// paths:
// /thingy:
// post:
// requestBody:
// content:
// application/json:
// schema:
// $ref: '#/components/schemas/ThingRequest'
// responses:
// "401":
// $ref: '#/components/responses/ErrorObj'
// default:
// description: Default response is just a string
// content:
// application/json:
// schema:
// type: string
// components:
// responses:
// ErrorObj:
// description: My error type
// content:
// application/json:
// schema:
// $ref: '#/components/schemas/NotAddedYet'

ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml, null, options);

assertNotNull(result.getOpenAPI());
assertTrue(result.getMessages().contains("attribute info is missing"));
assertTrue(result.getMessages().contains("attribute components.responses.ErrorObj.content.'application/json'.schema.NotAddedYet is missing"));
assertTrue(result.getMessages().contains("attribute paths.'/thingy'(post).requestBody.content.'application/json'.schema.#/components/schemas/ThingRequest is missing"));
}
}

0 comments on commit 011c06e

Please sign in to comment.