From 6c4f0941cd258b58ccd3befd790cfb41bc744ae5 Mon Sep 17 00:00:00 2001 From: Scott Russell Date: Tue, 13 Aug 2019 16:22:13 +1000 Subject: [PATCH] Issue #1157 - Modified ResolverFully to replace old Schema with $ref with new aggregated Schema in schemas Map --- .../swagger/v3/parser/util/ResolverFully.java | 1 + .../v3/parser/test/OpenAPIResolverTest.java | 26 ++++++++++++-- .../resources/issue-1157/allOf-example.yaml | 35 +++++++++++++++++++ .../resources/issue-1157/anyOf-example.yaml | 35 +++++++++++++++++++ .../resources/issue-1157/oneOf-example.yaml | 35 +++++++++++++++++++ 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-1157/allOf-example.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-1157/anyOf-example.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-1157/oneOf-example.yaml diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java index 56f5a9833c..cc85636a8b 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java @@ -291,6 +291,7 @@ public Schema resolveSchema(Schema schema) { // if we make it without a resolution loop, we can update the reference resolvedModels.put(ref, model); + schemas.put(ref, model); return model; diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java index d5c96600b5..b46344ecac 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java @@ -66,6 +66,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; @@ -490,6 +491,29 @@ public void testIssue85(@Injectable final List auths) { assertTrue(prop instanceof Schema); } + @Test + public void testIssue1157(@Injectable final List auths) { + ParseOptions options = new ParseOptions(); + options.setResolve(true); + options.setResolveFully(true); + + OpenAPI openAPIAnyOf = new OpenAPIV3Parser().readLocation("/issue-1157/anyOf-example.yaml", auths, options).getOpenAPI(); + Schema petSchemaAnyOf = openAPIAnyOf.getComponents().getSchemas().get("Pet"); + assertTrue(petSchemaAnyOf instanceof ComposedSchema); + assertTrue(((ComposedSchema) petSchemaAnyOf).getAnyOf() != null); + + OpenAPI openAPIOneOf = new OpenAPIV3Parser().readLocation("/issue-1157/oneOf-example.yaml", auths, options).getOpenAPI(); + Schema petSchemaOneOf = openAPIOneOf.getComponents().getSchemas().get("Pet"); + assertTrue(petSchemaOneOf instanceof ComposedSchema); + assertTrue(((ComposedSchema) petSchemaOneOf).getOneOf() != null); + + OpenAPI openAPIAllOf = new OpenAPIV3Parser().readLocation("/issue-1157/allOf-example.yaml", auths, options).getOpenAPI(); + Schema petSchemaAllOf = openAPIAllOf.getComponents().getSchemas().get("Pet"); + assertFalse(petSchemaAllOf instanceof ComposedSchema); + assertTrue(petSchemaAllOf.getProperties() != null); + + } + @Test public void testIssue1161(@Injectable final List auths) { String path = "/issue-1161/swagger.yaml"; @@ -499,8 +523,6 @@ public void testIssue1161(@Injectable final List auths) { options.setResolveFully(true); OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI(); - ResolverFully resolverUtil = new ResolverFully(); - resolverUtil.resolveFully(openAPI); Schema petsSchema = openAPI.getComponents().getSchemas().get("Pets"); Schema colouringsSchema = openAPI.getComponents().getSchemas().get("Colouring"); diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1157/allOf-example.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1157/allOf-example.yaml new file mode 100644 index 0000000000..b3b2eb76bd --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-1157/allOf-example.yaml @@ -0,0 +1,35 @@ +openapi: 3.0.0 +info: + version: 15.3.0 + title: test +paths: + /pets: + get: + description: "Test" + responses: + 200: + description: Parameters missing or invalid + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' +components: + schemas: + Dog: + type: object + properties: + bark: + type: boolean + breed: + type: string + Cat: + type: object + properties: + hunts: + type: boolean + age: + type: integer + Pet: + allOf: + - $ref: '#/components/schemas/Cat' + - $ref: '#/components/schemas/Dog' \ No newline at end of file diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1157/anyOf-example.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1157/anyOf-example.yaml new file mode 100644 index 0000000000..084f63a889 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-1157/anyOf-example.yaml @@ -0,0 +1,35 @@ +openapi: 3.0.0 +info: + version: 15.3.0 + title: test +paths: + /pets: + get: + description: "Test" + responses: + 200: + description: Parameters missing or invalid + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' +components: + schemas: + Dog: + type: object + properties: + bark: + type: boolean + breed: + type: string + Cat: + type: object + properties: + hunts: + type: boolean + age: + type: integer + Pet: + anyOf: + - $ref: '#/components/schemas/Cat' + - $ref: '#/components/schemas/Dog' diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1157/oneOf-example.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1157/oneOf-example.yaml new file mode 100644 index 0000000000..b098ffad15 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-1157/oneOf-example.yaml @@ -0,0 +1,35 @@ +openapi: 3.0.0 +info: + version: 15.3.0 + title: test +paths: + /pets: + get: + description: "Test" + responses: + 200: + description: Parameters missing or invalid + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' +components: + schemas: + Dog: + type: object + properties: + bark: + type: boolean + breed: + type: string + Cat: + type: object + properties: + hunts: + type: boolean + age: + type: integer + Pet: + oneOf: + - $ref: '#/components/schemas/Cat' + - $ref: '#/components/schemas/Dog'