Skip to content

Commit 350a04d

Browse files
committed
fix internal references validation for nested schemas
1 parent 84713d4 commit 350a04d

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
332332
openAPI.setComponents(components);
333333
this.components = components;
334334
if(result.validateInternalRefs) {
335+
/* TODO currently only capable of validating if ref is to root schema withing #/components/schemas
336+
* need to evaluate json pointer instead to also allow validation of nested schemas
337+
* e.g. #/components/schemas/foo/properties/bar
338+
*/
335339
for (String schema : localSchemaRefs.keySet()) {
336340
if (components.getSchemas().get(schema) == null) {
337341
result.invalidType(localSchemaRefs.get(schema), schema, "schema", rootNode);
@@ -2763,7 +2767,11 @@ at the moment path passed as string (basePath) from upper components can be both
27632767
} else {
27642768
schema.set$ref(ref.asText());
27652769
}
2766-
if(schema.get$ref().startsWith("#/components/schemas")){// it's internal
2770+
/* TODO currently only capable of validating if ref is to root schema withing #/components/schemas
2771+
* need to evaluate json pointer instead to also allow validation of nested schemas
2772+
* e.g. #/components/schemas/foo/properties/bar
2773+
*/
2774+
if(schema.get$ref().startsWith("#/components/schemas") && StringUtils.countMatches(schema.get$ref(), "/") == 3){
27672775
String refName = schema.get$ref().substring(schema.get$ref().lastIndexOf("/")+1);
27682776
localSchemaRefs.put(refName,location);
27692777
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3362,4 +3362,12 @@ public void testNullExample() throws Exception{
33623362
Assert.assertNotNull(openAPI);
33633363
assertEquals(Yaml.pretty(openAPI), yamlStringResolved);
33643364
}
3365+
3366+
@Test
3367+
public void testInternalRefsValidation() throws Exception {
3368+
String yamlString = FileUtils.readFileToString(new File("src/test/resources/internal-refs.yaml"), "UTF-8");
3369+
ParseOptions options = new ParseOptions();
3370+
SwaggerParseResult parseResult = new OpenAPIV3Parser().readContents(yamlString, null, options);
3371+
assertEquals(parseResult.getMessages().size(), 1);
3372+
}
33653373
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: test
5+
paths: {}
6+
components:
7+
schemas:
8+
MemberRef:
9+
description: Provides basic ID info about a member with an href to the full object
10+
type: object
11+
properties:
12+
uuid:
13+
description: "Uniquely identifies a member across the entire platform"
14+
type: string
15+
format: uuid
16+
minLength: 36
17+
maxLength: 36
18+
example: "26a0bac8-5c37-4c54-b3b1-1ad4551db061"
19+
alt_id:
20+
description: "An ID defined for a member, typically provided by a customer during integration, which will be unique within a given group."
21+
type: string
22+
minLength: 1
23+
maxLength: 36
24+
example: "ABC-123"
25+
group:
26+
$ref: "#/components/schemas/MemberGroup"
27+
href:
28+
type: string
29+
format: uri
30+
readOnly: true
31+
anyOf:
32+
- required: ["uuid"]
33+
properties:
34+
uuid:
35+
$ref: "#/components/schemas/MemberRef/properties/uuid"
36+
alt_id:
37+
$ref: "#/components/schemas/MemberRef/properties/alt_id"
38+
group:
39+
$ref: "#/components/schemas/MemberRef/properties/group"
40+
href:
41+
$ref: "#/components/schemas/MemberRef/properties/href"
42+
- required: ["alt_id", "group"]
43+
properties:
44+
uuid:
45+
$ref: "#/components/schemas/MemberRef/properties/uuid"
46+
alt_id:
47+
$ref: "#/components/schemas/MemberRef/properties/alt_id"
48+
group:
49+
$ref: "#/components/schemas/MemberRef/properties/group"
50+
href:
51+
$ref: "#/components/schemas/MemberRef/properties/href"
52+
MemberSmall:
53+
description: >-
54+
Provides abbreviated information about a member and is used when returning an array of members
55+
readOnly: true
56+
properties:
57+
uuid:
58+
$ref: "#/components/schemas/MemberRef/properties/uuid"
59+
alt_id:
60+
$ref: "#/components/schemas/MemberRef/properties/alt_id"
61+
group:
62+
$ref: "#/components/schemas/MemberRef/properties/group"
63+
href:
64+
$ref: "#/components/schemas/MemberRef/properties/href"
65+
activated:
66+
description: >-
67+
Indicates that the member has logged into their account
68+
type: boolean
69+
example: true
70+
disabled:
71+
description: >-
72+
Indicates that the member's account is disabled and they will no longer be able to login
73+
type: boolean
74+
example: false
75+
76+

0 commit comments

Comments
 (0)