From 245477e85e158acd029bdb841c7f71055d4ae53d Mon Sep 17 00:00:00 2001 From: Florian PAUL Date: Fri, 4 Oct 2024 09:14:22 +0200 Subject: [PATCH] fix(#2104): properly process inline refs in composed schemas --- .../processors/ExternalRefProcessor.java | 16 +++--- .../processors/ExternalRefProcessorTest.java | 25 +++++++++ .../issue-2104/depth1/depth2/definitions.yaml | 24 +++++++++ .../depth1/depth2/product/definitions.yaml | 10 ++++ .../test/resources/issue-2104/openapi.yaml | 51 +++++++++++++++++++ 5 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-2104/depth1/depth2/definitions.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-2104/depth1/depth2/product/definitions.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-2104/openapi.yaml diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java index 0413f79312..f0ee42b742 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java @@ -209,22 +209,18 @@ private void processComposedSchema(ComposedSchema composedSchema, String file) { if (composedSchema.getOneOf() != null) { for (Schema item : composedSchema.getOneOf()) { if (item.get$ref() != null) { - if (item.get$ref() != null) { - processRefSchema(item, file); - } else { - processSchema(item, file); - } + processRefSchema(item, file); + } else { + processSchema(item, file); } } } if (composedSchema.getAnyOf() != null) { for (Schema item : composedSchema.getAnyOf()) { if (item.get$ref() != null) { - if (item.get$ref() != null) { - processRefSchema(item, file); - } else { - processSchema(item, file); - } + processRefSchema(item, file); + } else { + processSchema(item, file); } } } diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ExternalRefProcessorTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ExternalRefProcessorTest.java index 267591ad0c..ace17ff003 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ExternalRefProcessorTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ExternalRefProcessorTest.java @@ -229,4 +229,29 @@ public void testHandleComposedSchemasInArrayItems() { assertTrue(components.containsKey("OrderRowType")); } + @Test + public void testHandleInlineRefsInComposedSchemas() { + OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); + ParseOptions options = new ParseOptions(); + options.setResolve(true); + SwaggerParseResult parseResult = openApiParser.readLocation("issue-2104/openapi.yaml", null, options); + OpenAPI openAPI = parseResult.getOpenAPI(); + + Map components = openAPI.getComponents().getSchemas(); + assertEquals(components.size(), 4); + assertTrue(components.containsKey("ResponseAllOf")); + assertTrue(components.containsKey("ResponseOneOf")); + assertTrue(components.containsKey("ResponseAnyOf")); + assertTrue(components.containsKey("Product")); + + Schema allOfInlineProduct = (Schema)((Schema)components.get("ResponseAllOf").getAllOf().get(1)).getProperties().get("product"); + assertThat(allOfInlineProduct.get$ref(), is("#/components/schemas/Product")); + + Schema oneOfInlineProduct = (Schema)((Schema)components.get("ResponseOneOf").getOneOf().get(1)).getProperties().get("product"); + assertThat(oneOfInlineProduct.get$ref(), is("#/components/schemas/Product")); + + Schema anyOfInlineProduct = (Schema)((Schema)components.get("ResponseAnyOf").getAnyOf().get(1)).getProperties().get("product"); + assertThat(anyOfInlineProduct.get$ref(), is("#/components/schemas/Product")); + } + } diff --git a/modules/swagger-parser-v3/src/test/resources/issue-2104/depth1/depth2/definitions.yaml b/modules/swagger-parser-v3/src/test/resources/issue-2104/depth1/depth2/definitions.yaml new file mode 100644 index 0000000000..560245d988 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-2104/depth1/depth2/definitions.yaml @@ -0,0 +1,24 @@ +components: + schemas: + ResponseAllOf: + allOf: + - $ref: '../depth2/product/definitions.yaml#/components/schemas/Product' + - type: object + properties: + product: + $ref: '../depth2/product/definitions.yaml#/components/schemas/Product' + ResponseOneOf: + oneOf: + - $ref: '../depth2/product/definitions.yaml#/components/schemas/Product' + - type: object + properties: + product: + $ref: '../depth2/product/definitions.yaml#/components/schemas/Product' + ResponseAnyOf: + anyOf: + - $ref: '../depth2/product/definitions.yaml#/components/schemas/Product' + - type: object + properties: + product: + $ref: '../depth2/product/definitions.yaml#/components/schemas/Product' + \ No newline at end of file diff --git a/modules/swagger-parser-v3/src/test/resources/issue-2104/depth1/depth2/product/definitions.yaml b/modules/swagger-parser-v3/src/test/resources/issue-2104/depth1/depth2/product/definitions.yaml new file mode 100644 index 0000000000..43e7d914f3 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-2104/depth1/depth2/product/definitions.yaml @@ -0,0 +1,10 @@ +components: + schemas: + Product: + type: object + additionalProperties: false + required: + - type + properties: + id: + type: string diff --git a/modules/swagger-parser-v3/src/test/resources/issue-2104/openapi.yaml b/modules/swagger-parser-v3/src/test/resources/issue-2104/openapi.yaml new file mode 100644 index 0000000000..7f35e3fdab --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-2104/openapi.yaml @@ -0,0 +1,51 @@ +openapi: 3.0.1 +info: + title: API + description: API + version: LATEST +paths: + /test-all-of: + post: + requestBody: + required: true + content: + text/plain: + schema: + type: string + responses: + 200: + description: OK + content: + application/json: + schema: + $ref: './depth1/depth2/definitions.yaml#/components/schemas/ResponseAllOf' + /test-one-of: + post: + requestBody: + required: true + content: + text/plain: + schema: + type: string + responses: + 200: + description: OK + content: + application/json: + schema: + $ref: './depth1/depth2/definitions.yaml#/components/schemas/ResponseOneOf' + /test-any-of: + post: + requestBody: + required: true + content: + text/plain: + schema: + type: string + responses: + 200: + description: OK + content: + application/json: + schema: + $ref: './depth1/depth2/definitions.yaml#/components/schemas/ResponseAnyOf'