Skip to content

Commit 0b76992

Browse files
authored
Merge pull request #1522 from ptrsd/cant-resolve-deep-properties
test case with deep properties which can't be resolved
2 parents 997c03b + 61d1997 commit 0b76992

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,18 @@ public Schema resolveSchema(Schema schema) {
283283

284284
if(schema.get$ref() != null) {
285285
String ref= schema.get$ref();
286-
ref = ref.substring(ref.lastIndexOf("/") + 1);
287-
Schema resolved = schemas != null ? schemas.get(ref) : null;
286+
Schema resolved;
287+
//This validation is done to solve deep properties eg. '#/components/schemas/TypeProject/properties/id'
288+
if (ref.contains("/properties/")){
289+
String split[] = ref.split("/");
290+
String refSchema = split[3];
291+
Schema parentSchema = schemas.get(refSchema);
292+
ref = ref.substring(ref.lastIndexOf("/") + 1);
293+
resolved = (Schema)parentSchema.getProperties().get(ref);
294+
}else {
295+
ref = ref.substring(ref.lastIndexOf("/") + 1);
296+
resolved = schemas != null ? schemas.get(ref) : null;
297+
}
288298

289299
if (resolved != null) {
290300

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import static org.hamcrest.CoreMatchers.equalTo;
7070
import static org.hamcrest.CoreMatchers.instanceOf;
7171
import static org.hamcrest.CoreMatchers.notNullValue;
72+
import static org.junit.Assert.assertNotEquals;
7273
import static org.junit.Assert.assertThat;
7374
import static org.testng.Assert.assertEquals;
7475
import static org.testng.Assert.assertFalse;
@@ -82,6 +83,18 @@ public class OpenAPIV3ParserTest {
8283
protected int serverPort = getDynamicPort();
8384
protected WireMockServer wireMockServer;
8485

86+
@Test
87+
public void testCantReadDeepProperties() {
88+
OpenAPIV3Parser parser = new OpenAPIV3Parser();
89+
ParseOptions options = new ParseOptions();
90+
options.setResolveFully(true);
91+
92+
final SwaggerParseResult parseResult = parser.readLocation("src/test/resources/cant-read-deep-properties.yaml", null, options);
93+
assertEquals(parseResult.getMessages().size(), 0);
94+
Schema projects = (Schema) parseResult.getOpenAPI().getComponents().getSchemas().get("Project").getProperties().get("project_type");
95+
assertEquals(projects.getType(), "integer");
96+
}
97+
8598
@Test
8699
public void testIssueSameRefsDifferentModel() throws IOException {
87100
String pathFile = FileUtils.readFileToString(new File("src/test/resources/same-refs-different-model-domain.yaml"), "UTF-8");
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
openapi: 3.0.0
2+
servers:
3+
# Added by API Auto Mocking Plugin
4+
- description: SwaggerHub API Auto Mocking
5+
url: http://host.docker.internal:8081/p.siudy2/awfsdgrfhtgh/1.0
6+
7+
info:
8+
version: '1.0'
9+
title: issue
10+
description: TypeProject/properties/id is not resolved
11+
license:
12+
name: Apache 2.0
13+
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
14+
tags:
15+
- name: issue
16+
description: issue
17+
paths:
18+
/projects:
19+
get:
20+
tags:
21+
- project
22+
description: list projects
23+
responses:
24+
'200':
25+
description: results
26+
content:
27+
application/json:
28+
schema:
29+
type: object
30+
properties:
31+
projects:
32+
type: array
33+
items:
34+
$ref: '#/components/schemas/Project'
35+
'400':
36+
description: Invalid
37+
'401':
38+
description: You are not authorized
39+
'500':
40+
description: Internal Server Error
41+
components:
42+
schemas:
43+
ProjectId:
44+
type: integer
45+
enum: [1, 2]
46+
description: one of project type ids
47+
Project:
48+
type: object
49+
required:
50+
- user
51+
- project_type
52+
properties:
53+
user:
54+
$ref: '#/components/schemas/User'
55+
project_type:
56+
$ref: '#/components/schemas/TypeProject/properties/id'
57+
User:
58+
type: object
59+
required:
60+
- full_name
61+
- id
62+
properties:
63+
full_name:
64+
type: string
65+
example: ful lname
66+
id:
67+
type: string
68+
format: uuid
69+
TypeProject:
70+
type: object
71+
properties:
72+
id:
73+
type: integer
74+
enum: [1, 2]
75+
description: one of project type ids
76+
name:
77+
type: string
78+
enum: ['project1', 'project2']

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@
369369
<swagger-parser-v2-version>1.0.55-SNAPSHOT</swagger-parser-v2-version>
370370
<commons-io-version>2.6</commons-io-version>
371371
<slf4j-version>1.7.30</slf4j-version>
372-
<swagger-core-version>2.1.7-SNAPSHOT</swagger-core-version>
372+
<swagger-core-version>2.1.8-SNAPSHOT</swagger-core-version>
373373
<swagger-core-v2-version>1.6.3-SNAPSHOT</swagger-core-v2-version>
374374
<junit-version>4.13.1</junit-version>
375375
<testng-version>6.14.2</testng-version>

0 commit comments

Comments
 (0)