Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#2104): properly process inline refs in composed schemas #2123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Schema> 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"));
}

}
Original file line number Diff line number Diff line change
@@ -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'

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
components:
schemas:
Product:
type: object
additionalProperties: false
required:
- type
properties:
id:
type: string
Original file line number Diff line number Diff line change
@@ -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'