-
Notifications
You must be signed in to change notification settings - Fork 537
fixing allOf external resolving refs #628 #630
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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){ | ||
| for(Schema item : composedSchema.getOneOf()){ | ||
| if (item.get$ref() != null){ | ||
| if (item.get$ref() != null){ | ||
| processRefSchema(item,file); | ||
| } | ||
| } | ||
| } | ||
| }else if (composedSchema.getAnyOf() != null){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace |
||
| 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); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
modules/swagger-parser-v3/src/test/resources/composedSchemaRef.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
62
modules/swagger-parser-v3/src/test/resources/refComponents.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace
else ifwithif, as allOf, anyOf and oneOf can theoretically coexist, check with a test case