diff --git a/README.md b/README.md index 23dee7fd14..e60e55bdcf 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f io.swagger.parser.v3 swagger-parser - 2.1.21 + 2.1.22 ``` diff --git a/modules/swagger-parser-cli/pom.xml b/modules/swagger-parser-cli/pom.xml index 22875309fd..72d5fd571f 100644 --- a/modules/swagger-parser-cli/pom.xml +++ b/modules/swagger-parser-cli/pom.xml @@ -5,7 +5,7 @@ swagger-parser-project io.swagger.parser.v3 - 2.1.22-SNAPSHOT + 2.1.22 ../.. 4.0.0 @@ -91,7 +91,7 @@ io.swagger.parser.v3 swagger-parser-v3 - 2.1.22-SNAPSHOT + 2.1.22 compile diff --git a/modules/swagger-parser-core/pom.xml b/modules/swagger-parser-core/pom.xml index 02108f8879..5e45329d53 100644 --- a/modules/swagger-parser-core/pom.xml +++ b/modules/swagger-parser-core/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.22-SNAPSHOT + 2.1.22 ../.. 4.0.0 diff --git a/modules/swagger-parser-safe-url-resolver/pom.xml b/modules/swagger-parser-safe-url-resolver/pom.xml index e2b7f8ec2f..3b398d61ef 100644 --- a/modules/swagger-parser-safe-url-resolver/pom.xml +++ b/modules/swagger-parser-safe-url-resolver/pom.xml @@ -6,7 +6,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.22-SNAPSHOT + 2.1.22 ../../pom.xml diff --git a/modules/swagger-parser-v2-converter/pom.xml b/modules/swagger-parser-v2-converter/pom.xml index 3a990c3d11..55910a9df5 100644 --- a/modules/swagger-parser-v2-converter/pom.xml +++ b/modules/swagger-parser-v2-converter/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.22-SNAPSHOT + 2.1.22 ../.. 4.0.0 diff --git a/modules/swagger-parser-v3/pom.xml b/modules/swagger-parser-v3/pom.xml index 3bdfce002a..e297c3c9e5 100644 --- a/modules/swagger-parser-v3/pom.xml +++ b/modules/swagger-parser-v3/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.22-SNAPSHOT + 2.1.22 ../.. 4.0.0 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 c62f5ef771..0413f79312 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 @@ -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 subProps = schema.getProperties(); @@ -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())) { 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 218f97571a..267591ad0c 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 @@ -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; @@ -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; @@ -206,4 +211,22 @@ public void testRelativeRefIncludingUrlRef() is("./relative-with-url/relative-with-local.yaml#/relative-same-file") ); } + + @Test + public void testHandleComposedSchemasInArrayItems() { + 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 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")); + } + } diff --git a/modules/swagger-parser-v3/src/test/resources/issue-2071/definitions.yaml b/modules/swagger-parser-v3/src/test/resources/issue-2071/definitions.yaml new file mode 100644 index 0000000000..3d0068dfb0 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-2071/definitions.yaml @@ -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 diff --git a/modules/swagger-parser-v3/src/test/resources/issue-2071/openapi.yaml b/modules/swagger-parser-v3/src/test/resources/issue-2071/openapi.yaml new file mode 100644 index 0000000000..d457a1dd94 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-2071/openapi.yaml @@ -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' diff --git a/modules/swagger-parser/pom.xml b/modules/swagger-parser/pom.xml index 5c039c41a0..2c6e4f8157 100644 --- a/modules/swagger-parser/pom.xml +++ b/modules/swagger-parser/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.22-SNAPSHOT + 2.1.22 ../.. 4.0.0 diff --git a/pom.xml b/pom.xml index ff81e26d6a..30438a25b7 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ 4.0.0 io.swagger.parser.v3 swagger-parser-project - 2.1.22-SNAPSHOT + 2.1.22 pom swagger-parser-project