Skip to content

Commit

Permalink
fix(swagger-api#2104): properly process inline refs in composed schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
fpaul-1A committed Oct 4, 2024
1 parent 0ab2e8a commit 245477e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 10 deletions.
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'

0 comments on commit 245477e

Please sign in to comment.