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

Issue 2071. Correctly handle external refs in array items composed sc… #2072

Merged
merged 2 commits into from
Apr 5, 2024
Merged
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 @@ -147,36 +147,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {

if(schema instanceof ComposedSchema){
ComposedSchema composedSchema = (ComposedSchema) schema;
if (composedSchema.getAllOf() != null){
for(Schema item : composedSchema.getAllOf()){
if (item.get$ref() != null){
processRefSchema(item,file);
} else{
processSchema(item, 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);
}
}
}
}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);
}
}
}
}
processComposedSchema(composedSchema, file);
}
//Loop the properties and recursively call this method;
Map<String, Schema> subProps = schema.getProperties();
Expand Down Expand Up @@ -213,13 +184,52 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
if (StringUtils.isNotBlank(arraySchema.getItems().get$ref())) {
processRefSchema(((ArraySchema) schema).getItems(), file);
} else {
processProperties(arraySchema.getItems().getProperties() ,file);
if (arraySchema.getItems() instanceof ComposedSchema) {
ComposedSchema composedSchema = (ComposedSchema) arraySchema.getItems();
processComposedSchema(composedSchema, file);
}
processProperties(arraySchema.getItems().getProperties(), file);
}
}
}
return newRef;
}

private void processComposedSchema(ComposedSchema composedSchema, String file) {
if (composedSchema.getAllOf() != null) {
for (Schema item : composedSchema.getAllOf()) {
if (item.get$ref() != null) {
processRefSchema(item, file);
} else {
processSchema(item, 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);
}
}
}
}
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);
}
}
}
}
}

private void processSchema(Schema property, String file) {
if (property != null) {
if (StringUtils.isNotBlank(property.get$ref())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.ResolverCache;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.parser.models.RefFormat;
import io.swagger.v3.parser.util.RemoteUrl;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import mockit.Expectations;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.ArrayList;
Expand All @@ -23,6 +27,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.testng.Assert.assertEquals;


Expand Down Expand Up @@ -206,4 +211,22 @@ public void testRelativeRefIncludingUrlRef()
is("./relative-with-url/relative-with-local.yaml#/relative-same-file")
);
}

@Test
public void testHandleComposedSchemasInArrayItems() {
chimmi marked this conversation as resolved.
Show resolved Hide resolved
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult parseResult = openApiParser.readLocation("issue-2071/openapi.yaml", null, options);
OpenAPI openAPI = parseResult.getOpenAPI();

Map<String, Schema> components = openAPI.getComponents().getSchemas();
assertEquals(components.size(), 5);
assertTrue(components.containsKey("Response"));
assertTrue(components.containsKey("ProductRow"));
assertTrue(components.containsKey("ProductRowType"));
assertTrue(components.containsKey("OrderRow"));
assertTrue(components.containsKey("OrderRowType"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
components:
schemas:
Response:
type: array
items:
oneOf:
- $ref: '#/components/schemas/ProductRow'
properties:
orderRow:
$ref: '#/components/schemas/OrderRow'
discriminator:
propertyName: type
mapping:
'product': '#/components/schemas/ProductRow'
ProductRow:
type: object
additionalProperties: false
required:
- type
properties:
type:
$ref: '#/components/schemas/ProductRowType'
payload:
type: string
ProductRowType:
type: string
enum:
- product
OrderRow:
type: object
additionalProperties: false
required:
- type
properties:
type:
$ref: '#/components/schemas/OrderRowType'
payload:
type: string
OrderRowType:
type: string
enum:
- order
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.0.1
info:
title: API
description: API
version: LATEST
paths:
/test-path:
post:
requestBody:
required: true
content:
text/plain:
schema:
type: string
responses:
200:
description: OK
content:
application/json:
schema:
$ref: 'definitions.yaml#/components/schemas/Response'
Loading