Skip to content
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 @@ -8,6 +8,7 @@
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.links.Link;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
Expand Down Expand Up @@ -83,64 +84,88 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {

String file = $ref.split("#/")[0];
if (schema.get$ref() != null) {
RefFormat format = computeRefFormat(schema.get$ref());
if (isAnExternalRefFormat(format)) {
schema.set$ref(processRefToExternalSchema(schema.get$ref(), format));
} else {
processRefToExternalSchema(file + schema.get$ref(), RefFormat.RELATIVE);
processRefSchema(schema,file);
}

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 if (composedSchema.getOneOf() != null){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace else if with if, as allOf, anyOf and oneOf can theoretically coexist, check with a test case

for(Schema item : composedSchema.getOneOf()){
if (item.get$ref() != null){
if (item.get$ref() != null){
processRefSchema(item,file);
}
}
}
}else if (composedSchema.getAnyOf() != null){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace else if with if, as allOf, anyOf and oneOf can theoretically coexist, check with a test case

for(Schema item : composedSchema.getAnyOf()){
if (item.get$ref() != null){
if (item.get$ref() != null){
processRefSchema(item,file);
}
}
}

}
}
//Loop the properties and recursively call this method;
Map<String, Schema> subProps = schema.getProperties();
if (subProps != null) {
for (Map.Entry<String, Schema> prop : subProps.entrySet()) {
if (prop.getValue().get$ref() != null) {
processRefProperty(prop.getValue(), file);
processRefSchema(prop.getValue(), file);
} else if (prop.getValue() instanceof ArraySchema) {
ArraySchema arrayProp = (ArraySchema) prop.getValue();
if (arrayProp.getItems() != null && arrayProp.getItems().get$ref() != null &&
StringUtils.isNotBlank(arrayProp.get$ref())) {
processRefProperty(arrayProp.getItems(), file);
processRefSchema(arrayProp.getItems(), file);
}
} else if (prop.getValue().getAdditionalProperties() != null && prop.getValue().getAdditionalProperties() instanceof Schema) {
Schema mapProp = (Schema) prop.getValue().getAdditionalProperties();
if (mapProp.get$ref() != null) {
processRefProperty(mapProp, file);
processRefSchema(mapProp, file);
} else if (mapProp.getAdditionalProperties() instanceof ArraySchema &&
((ArraySchema) mapProp).getItems()!= null &&
((ArraySchema) mapProp).getItems().get$ref() != null
&& StringUtils.isNotBlank(((ArraySchema) mapProp).getItems().get$ref())) {
processRefProperty(((ArraySchema) mapProp.getAdditionalProperties()).getItems(), file);
processRefSchema(((ArraySchema) mapProp.getAdditionalProperties()).getItems(), file);
}
}
}
}
if(schema.getAdditionalProperties() != null && schema.getAdditionalProperties() instanceof Schema){
Schema additionalProperty = (Schema) schema.getAdditionalProperties();
if (additionalProperty.get$ref() != null) {
processRefProperty(additionalProperty, file);
processRefSchema(additionalProperty, file);
} else if (additionalProperty instanceof ArraySchema) {
ArraySchema arrayProp = (ArraySchema) additionalProperty;
if (arrayProp.getItems() != null && arrayProp.getItems().get$ref() != null &&
StringUtils.isNotBlank(arrayProp.get$ref())) {
processRefProperty(arrayProp.getItems(), file);
processRefSchema(arrayProp.getItems(), file);
}
} else if (additionalProperty.getAdditionalProperties() != null && additionalProperty.getAdditionalProperties() instanceof Schema) {
Schema mapProp = (Schema) additionalProperty.getAdditionalProperties();
if (mapProp.get$ref() != null) {
processRefProperty(mapProp, file);
processRefSchema(mapProp, file);
} else if (mapProp.getAdditionalProperties() instanceof ArraySchema &&
((ArraySchema) mapProp).getItems() != null &&
((ArraySchema) mapProp).getItems().get$ref() != null
&& StringUtils.isNotBlank(((ArraySchema) mapProp).getItems().get$ref())) {
processRefProperty(((ArraySchema) mapProp).getItems(), file);
processRefSchema(((ArraySchema) mapProp).getItems(), file);
}
}

}
if (schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null && ((ArraySchema) schema).getItems().get$ref() != null
&& StringUtils.isNotBlank(((ArraySchema) schema).getItems().get$ref())) {
processRefProperty(((ArraySchema) schema).getItems(), file);
processRefSchema(((ArraySchema) schema).getItems(), file);
}
}

Expand Down Expand Up @@ -614,7 +639,7 @@ public String processRefToExternalCallback(String $ref, RefFormat refFormat) {
}


private void processRefProperty(Schema subRef, String externalFile) {
private void processRefSchema(Schema subRef, String externalFile) {
RefFormat format = computeRefFormat(subRef.get$ref());
if (isAnExternalRefFormat(format)) {
String $ref = constructRef(subRef, externalFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
Expand Down Expand Up @@ -324,6 +325,23 @@ public void testRefAndInlineAllOf(@Injectable final List<AuthorizationValue> aut
Assert.assertTrue(openAPI.getPaths().get("/refToAllOf").getGet().getResponses().get("200").getContent().get("application/json").getSchema().getProperties().size() == 2);
}

@Test
public void testComposedRefResolvingIssue628(@Injectable final List<AuthorizationValue> auths) throws Exception {
ParseOptions options = new ParseOptions();
options.setResolve(true);
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/composedSchemaRef.yaml", auths, options);

Assert.assertNotNull(openAPI);

Assert.assertTrue(openAPI.getComponents().getSchemas().size() == 5);
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("Cat"));
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("Dog"));
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("Lion"));
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("Bear"));

}

@Test
public void testOneOfExternalRefConflictName() throws Exception {
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/oneof_name_conflict/oneOf-external-ref-name-conflict.yaml");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.0.0
info:
version: 0.0.0
title: oneOf and anyOf

paths:
/oneOf:
get:
responses:
'200':
description: One of Cat or Dog
content:
application/json:
schema:
oneOf:
- $ref: './refComponents.yaml#/components/schemas/Cat'
- $ref: './refComponents.yaml#/components/schemas/Dog'
- $ref: './refComponents.yaml#/components/schemas/Lion'
/anyOf:
get:
responses:
'200':
description: Any of Cat or Dog
content:
application/json:
schema:
anyOf:
- $ref: './refComponents.yaml#/components/schemas/Cat'
- $ref: './refComponents.yaml#/components/schemas/Dog'
- $ref: './refComponents.yaml#/components/schemas/Bear'
62 changes: 62 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/refComponents.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
openapi: 3.0.0
info:
version: 0.0.0
title: apa

components:
schemas:
Pet:
required:
- name
- petType
properties:
name:
type: string
petType:
type: string
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
huntingSkill:
type: string
default: lazy
enum:
- lazy
- aggressive
required:
- huntingSkill
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
packSize:
description: The size of the pack the dog is from
type: integer
required:
- packSize
Lion:
oneOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
huntingSkill:
type: string
default: lazy
enum:
- lazy
- aggressive
required:
- huntingSkill
Bear:
anyOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
packSize:
description: The size of the pack the dog is from
type: integer
required:
- packSize