Skip to content

Commit 011c06e

Browse files
authored
Merge branch 'master' into fix_test_build
2 parents a9db06e + d2e6b3a commit 011c06e

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,11 @@ public SwaggerParseResult deserialize(JsonNode rootNode, String path, ParseOptio
299299
result.setOpenAPI(api);
300300
result.setMessages(rootParse.getMessages());
301301
} catch (Exception e) {
302-
result.setMessages(Arrays.asList(e.getMessage()));
302+
if (StringUtils.isNotBlank(e.getMessage())) {
303+
result.setMessages(Arrays.asList(e.getMessage()));
304+
} else {
305+
result.setMessages(Arrays.asList("Unexpected error deserialising spec"));
306+
}
303307
}
304308
return result;
305309
}
@@ -343,10 +347,13 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
343347
* e.g. #/components/schemas/foo/properties/bar
344348
*/
345349
for (String schema : localSchemaRefs.keySet()) {
346-
if (components.getSchemas().get(schema) == null) {
350+
if (components.getSchemas() == null){
351+
result.missing(localSchemaRefs.get(schema), schema);
352+
} else if (components.getSchemas().get(schema) == null) {
347353
result.invalidType(localSchemaRefs.get(schema), schema, "schema", rootNode);
348354
}
349355
}
356+
350357
}
351358
}
352359

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OAIDeserializationTest.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
package io.swagger.v3.parser.test;
22

33

4+
import io.swagger.v3.core.util.Yaml;
5+
import io.swagger.v3.oas.models.Components;
6+
import io.swagger.v3.oas.models.OpenAPI;
7+
import io.swagger.v3.oas.models.Operation;
8+
import io.swagger.v3.oas.models.PathItem;
9+
import io.swagger.v3.oas.models.Paths;
10+
import io.swagger.v3.oas.models.SpecVersion;
11+
import io.swagger.v3.oas.models.media.ComposedSchema;
12+
import io.swagger.v3.oas.models.media.Content;
13+
import io.swagger.v3.oas.models.media.MediaType;
14+
import io.swagger.v3.oas.models.media.ObjectSchema;
15+
import io.swagger.v3.oas.models.media.StringSchema;
16+
import io.swagger.v3.oas.models.parameters.RequestBody;
17+
import io.swagger.v3.oas.models.responses.ApiResponse;
18+
import io.swagger.v3.oas.models.responses.ApiResponses;
419
import io.swagger.v3.parser.core.models.ParseOptions;
520
import io.swagger.v3.parser.core.models.SwaggerParseResult;
621
import io.swagger.v3.parser.OpenAPIV3Parser;
722
import org.testng.annotations.Test;
823

24+
import java.util.HashMap;
25+
926
import static org.testng.Assert.assertNotNull;
27+
import static org.testng.Assert.assertTrue;
1028
import static org.testng.AssertJUnit.assertEquals;
1129

1230
public class OAIDeserializationTest {
@@ -42,4 +60,81 @@ public void testIssue911() {
4260
assertEquals(result.getMessages().size(),1);
4361
assertNotNull(result.getOpenAPI());
4462
}
63+
64+
@Test
65+
public void testDeserializeYamlDefinitionMissingSchema_Issue1951() throws Exception {
66+
// Create Spec missing schema but with schema items referenced in responses
67+
OpenAPI api = new OpenAPI();
68+
api.setSpecVersion(SpecVersion.V30);
69+
api.setComponents(new Components()); // no schema
70+
ApiResponse errorResponse = new ApiResponse().content(new Content());
71+
errorResponse
72+
.description("My error type")
73+
.getContent()
74+
.addMediaType("application/json", new MediaType()
75+
.schema(new ComposedSchema().
76+
$ref("NotAddedYet")
77+
.type("schema")));
78+
api.getComponents().setResponses(new HashMap<>());
79+
api.getComponents().getResponses().put("ErrorObj", errorResponse);
80+
ComposedSchema schema = new ComposedSchema();
81+
schema.set$ref("ThingRequest");
82+
schema.setType("schema");
83+
Content content = new Content().addMediaType("application/json", new MediaType().schema(schema));
84+
ApiResponses resp = new ApiResponses();
85+
resp.addApiResponse("401", new ApiResponse().$ref("#/components/responses/ErrorObj"));
86+
resp.setDefault(new ApiResponse()
87+
.description("Default response is just a string")
88+
.content(new Content()
89+
.addMediaType("application/json", new MediaType().schema(new StringSchema()))
90+
)
91+
);
92+
api.setPaths(new Paths()
93+
.addPathItem("/thingy", new PathItem()
94+
.post(new Operation()
95+
.responses(resp)
96+
.requestBody(new RequestBody().content(content))
97+
)
98+
)
99+
);
100+
101+
String yaml = Yaml.mapper().writeValueAsString(api);
102+
103+
// Generated YAML. Intentionally missing schemas
104+
// openapi: 3.0.1
105+
// paths:
106+
// /thingy:
107+
// post:
108+
// requestBody:
109+
// content:
110+
// application/json:
111+
// schema:
112+
// $ref: '#/components/schemas/ThingRequest'
113+
// responses:
114+
// "401":
115+
// $ref: '#/components/responses/ErrorObj'
116+
// default:
117+
// description: Default response is just a string
118+
// content:
119+
// application/json:
120+
// schema:
121+
// type: string
122+
// components:
123+
// responses:
124+
// ErrorObj:
125+
// description: My error type
126+
// content:
127+
// application/json:
128+
// schema:
129+
// $ref: '#/components/schemas/NotAddedYet'
130+
131+
ParseOptions options = new ParseOptions();
132+
options.setResolve(true);
133+
SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml, null, options);
134+
135+
assertNotNull(result.getOpenAPI());
136+
assertTrue(result.getMessages().contains("attribute info is missing"));
137+
assertTrue(result.getMessages().contains("attribute components.responses.ErrorObj.content.'application/json'.schema.NotAddedYet is missing"));
138+
assertTrue(result.getMessages().contains("attribute paths.'/thingy'(post).requestBody.content.'application/json'.schema.#/components/schemas/ThingRequest is missing"));
139+
}
45140
}

0 commit comments

Comments
 (0)