Skip to content

Commit 6be7be5

Browse files
committed
added option to configure the valdiation of internal references
1 parent 90760b2 commit 6be7be5

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

modules/swagger-parser-core/src/main/java/io/swagger/v3/parser/core/models/ParseOptions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class ParseOptions {
1010
private boolean skipMatches;
1111
private boolean allowEmptyStrings = true;
1212
private boolean validateExternalRefs = false;
13+
private boolean validateInternalRefs = false;
1314
private boolean legacyYamlDeserialization = false;
1415

1516
public boolean isResolve() {
@@ -89,4 +90,12 @@ public boolean isLegacyYamlDeserialization() {
8990
public void setLegacyYamlDeserialization(boolean legacyYamlDeserialization) {
9091
this.legacyYamlDeserialization = legacyYamlDeserialization;
9192
}
93+
94+
public void setValidateInternalRefs(boolean validateInternalRefs) {
95+
this.validateInternalRefs = validateInternalRefs;
96+
}
97+
98+
public boolean isValidateInternalRefs() {
99+
return validateInternalRefs;
100+
}
92101
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public SwaggerParseResult deserialize(JsonNode rootNode, String path, ParseOptio
153153
SwaggerParseResult result = new SwaggerParseResult();
154154
ParseResult rootParse = new ParseResult();
155155
rootParse.setAllowEmptyStrings(options.isAllowEmptyString());
156+
rootParse.setValidateInternalRefs(options.isValidateInternalRefs());
156157
OpenAPI api = parseRoot(rootNode, rootParse, path);
157158
result.setOpenAPI(api);
158159
result.setMessages(rootParse.getMessages());
@@ -185,9 +186,11 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
185186
Components components = getComponents(obj, "components", result);
186187
openAPI.setComponents(components);
187188
this.components = components;
188-
for (String schema : localSchemaRefs.keySet()){
189-
if(components.getSchemas().get(schema) == null){
190-
result.invalidType(localSchemaRefs.get(schema), schema, "schema", rootNode);
189+
if(result.validateInternalRefs) {
190+
for (String schema : localSchemaRefs.keySet()) {
191+
if (components.getSchemas().get(schema) == null) {
192+
result.invalidType(localSchemaRefs.get(schema), schema, "schema", rootNode);
193+
}
191194
}
192195
}
193196
}
@@ -3195,6 +3198,7 @@ public static class ParseResult {
31953198
private List<Location> unique = new ArrayList<>();
31963199
private List<Location> uniqueTags = new ArrayList<>();
31973200
private boolean allowEmptyStrings = true;
3201+
private boolean validateInternalRefs;
31983202

31993203
public ParseResult() {
32003204
}
@@ -3289,7 +3293,15 @@ public List<String> getMessages() {
32893293
}
32903294
return messages;
32913295
}
3292-
}
3296+
3297+
public void setValidateInternalRefs(boolean validateInternalRefs) {
3298+
this.validateInternalRefs = validateInternalRefs;
3299+
}
3300+
3301+
public boolean isValidateInternalRefs() {
3302+
return validateInternalRefs;
3303+
}
3304+
}
32933305

32943306

32953307
protected static class Location {

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ public class OpenAPIV3ParserTest {
8686
protected WireMockServer wireMockServer;
8787

8888
@Test
89-
public void testIssue1643() throws Exception{
89+
public void testIssue1643_True() throws Exception{
9090
ParseOptions options = new ParseOptions();
91+
options.setValidateInternalRefs(true);
9192
String issue1643 = "openapi: \"3.0.0\"\n" +
9293
"info:\n" +
9394
" version: 1.0.0\n" +
@@ -128,6 +129,49 @@ public void testIssue1643() throws Exception{
128129
assertTrue(result.getMessages().contains("attribute components.schemas.Person.Employee is not of type `schema`"));
129130
}
130131

132+
@Test
133+
public void testIssue1643_False() throws Exception{
134+
ParseOptions options = new ParseOptions();
135+
String issue1643 = "openapi: \"3.0.0\"\n" +
136+
"info:\n" +
137+
" version: 1.0.0\n" +
138+
" title: People\n" +
139+
"paths:\n" +
140+
" /person:\n" +
141+
" get:\n" +
142+
" operationId: getPerson\n" +
143+
" parameters:\n" +
144+
" - name: name\n" +
145+
" in: query\n" +
146+
" required: true\n" +
147+
" schema:\n" +
148+
" type: string\n" +
149+
" responses:\n" +
150+
" '200':\n" +
151+
" description: The person with the given name. \n" +
152+
" content:\n" +
153+
" application/json:\n" +
154+
" schema:\n" +
155+
" $ref: '#/components/schemas/Person'\n" +
156+
"components:\n" +
157+
" schemas:\n" +
158+
" Person:\n" +
159+
" required:\n" +
160+
" - name\n" +
161+
" type: object\n" +
162+
" properties:\n" +
163+
" name:\n" +
164+
" type: string\n" +
165+
" employee:\n" +
166+
" $ref: '#/components/schemas/Employee'";
167+
SwaggerParseResult result = new OpenAPIV3Parser().readContents(issue1643, null, options);
168+
169+
Assert.assertNotNull(result);
170+
Assert.assertNotNull(result.getOpenAPI());
171+
assertEquals(result.getMessages().size(),0);
172+
assertFalse(result.getMessages().contains("attribute components.schemas.Person.Employee is not of type `schema`"));
173+
}
174+
131175
@Test
132176
public void testExampleFormatByte() throws Exception{
133177

@@ -328,7 +372,7 @@ public void testCantReadDeepProperties() {
328372
options.setResolveFully(true);
329373

330374
final SwaggerParseResult parseResult = parser.readLocation("src/test/resources/cant-read-deep-properties.yaml", null, options);
331-
assertEquals(parseResult.getMessages().size(), 1);
375+
assertEquals(parseResult.getMessages().size(), 0);
332376
Schema projects = (Schema) parseResult.getOpenAPI().getComponents().getSchemas().get("Project").getProperties().get("project_type");
333377
assertEquals(projects.getType(), "integer");
334378
}
@@ -2511,7 +2555,7 @@ public void testIssue931() {
25112555
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("Issue_931.json", null, options);
25122556
assertNotNull(result.getOpenAPI());
25132557
assertTrue(result.getMessages().size() > 0);
2514-
assertEquals(result.getMessages().get(1).contains("doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$"), true);
2558+
assertEquals(result.getMessages().get(0).contains("doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$"), true);
25152559

25162560
}
25172561

0 commit comments

Comments
 (0)