Skip to content

Commit bbc19ba

Browse files
committed
fix and test for issue#1643
1 parent e29a4f4 commit bbc19ba

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ public class OpenAPIDeserializer {
139139
private static final String REFERENCE_SEPARATOR = "#/";
140140
private Components components;
141141
private final Set<String> operationIDs = new HashSet<>();
142+
private List<String> internalProperties = new ArrayList();
142143

143-
public SwaggerParseResult deserialize(JsonNode rootNode) {
144+
public SwaggerParseResult deserialize(JsonNode rootNode) {
144145
return deserialize(rootNode, null);
145146
}
146147

@@ -184,6 +185,11 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
184185
Components components = getComponents(obj, "components", result);
185186
openAPI.setComponents(components);
186187
this.components = components;
188+
for (String schema : internalProperties){
189+
if(components.getSchemas().get(schema) == null){
190+
result.missing("components.schemas",schema);
191+
}
192+
}
187193
}
188194

189195
obj = getObject("paths", rootNode, true, location, result);
@@ -2292,6 +2298,10 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result) {
22922298
} else {
22932299
schema.set$ref(ref.asText());
22942300
}
2301+
if(schema.get$ref().startsWith("#/components/schemas")){// it's internal
2302+
String refName = schema.get$ref().substring(schema.get$ref().lastIndexOf("/")+1);
2303+
internalProperties.add(refName);
2304+
}
22952305
return schema;
22962306
} else {
22972307
result.invalidType(location, "$ref", "string", node);

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,49 @@ public class OpenAPIV3ParserTest {
8585
protected int serverPort = getDynamicPort();
8686
protected WireMockServer wireMockServer;
8787

88+
@Test
89+
public void testIssue1643() throws Exception{
90+
ParseOptions options = new ParseOptions();
91+
String issue1643 = "openapi: \"3.0.0\"\n" +
92+
"info:\n" +
93+
" version: 1.0.0\n" +
94+
" title: People\n" +
95+
"paths:\n" +
96+
" /person:\n" +
97+
" get:\n" +
98+
" operationId: getPerson\n" +
99+
" parameters:\n" +
100+
" - name: name\n" +
101+
" in: query\n" +
102+
" required: true\n" +
103+
" schema:\n" +
104+
" type: string\n" +
105+
" responses:\n" +
106+
" '200':\n" +
107+
" description: The person with the given name. \n" +
108+
" content:\n" +
109+
" application/json:\n" +
110+
" schema:\n" +
111+
" $ref: '#/components/schemas/Person'\n" +
112+
"components:\n" +
113+
" schemas:\n" +
114+
" Person:\n" +
115+
" required:\n" +
116+
" - name\n" +
117+
" type: object\n" +
118+
" properties:\n" +
119+
" name:\n" +
120+
" type: string\n" +
121+
" employee:\n" +
122+
" $ref: '#/components/schemas/Employee'";
123+
SwaggerParseResult result = new OpenAPIV3Parser().readContents(issue1643, null, options);
124+
125+
Assert.assertNotNull(result);
126+
Assert.assertNotNull(result.getOpenAPI());
127+
assertEquals(result.getMessages().size(),1);
128+
assertTrue(result.getMessages().contains("attribute components.schemas.Employee is missing"));
129+
}
130+
88131
@Test
89132
public void testExampleFormatByte() throws Exception{
90133

0 commit comments

Comments
 (0)