Repository navigation
fix(v3): update discriminator mapping of a nested oneOf after external ref resolution #2407
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
Open
wyhasany
wants to merge
1
commit into
swagger-api:master
Choose a base branch
from
wyhasany:bug/2406-nested-discriminator-mapping-null-key
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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
41 changes: 41 additions & 0 deletions
41
modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/Issue2406Test.java
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,41 @@ | ||
| package io.swagger.v3.parser.test; | ||
|
|
||
| import io.swagger.v3.oas.models.media.Discriminator; | ||
| import io.swagger.v3.oas.models.media.Schema; | ||
| import io.swagger.v3.parser.OpenAPIV3Parser; | ||
| import io.swagger.v3.parser.core.models.ParseOptions; | ||
| import io.swagger.v3.parser.core.models.SwaggerParseResult; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
|
|
||
| public class Issue2406Test { | ||
|
|
||
| @Test | ||
| public void shouldUpdateDiscriminatorMappingOfOneOfNestedInArrayItemsWithExternalRefs() { | ||
| ParseOptions options = new ParseOptions(); | ||
| options.setResolve(true); | ||
| SwaggerParseResult result = new OpenAPIV3Parser().readLocation("issue-2406/openapi.yaml", null, options); | ||
|
|
||
| assertEquals(result.getMessages().toString(), 0, result.getMessages().size()); | ||
|
|
||
| Schema<?> searchRequest = result.getOpenAPI().getComponents().getSchemas().get("SearchRequest"); | ||
| Schema<?> items = ((Schema<?>) searchRequest.getProperties().get("query")).getItems(); | ||
| List<String> oneOfRefs = items.getOneOf().stream().map(Schema::get$ref).collect(Collectors.toList()); | ||
| assertEquals(Arrays.asList("#/components/schemas/TypeA", "#/components/schemas/TypeB"), oneOfRefs); | ||
|
|
||
| Discriminator discriminator = items.getDiscriminator(); | ||
| assertEquals("kind", discriminator.getPropertyName()); | ||
| // no entry with a null key must be added, and the mapping must follow the rewritten oneOf refs | ||
| assertFalse(discriminator.getMapping().containsKey(null)); | ||
| assertEquals(3, discriminator.getMapping().size()); | ||
| assertEquals("#/components/schemas/TypeA", discriminator.getMapping().get("a")); | ||
| assertEquals("#/components/schemas/TypeB", discriminator.getMapping().get("b")); | ||
| assertEquals("#/components/schemas/TypeB", discriminator.getMapping().get("bLegacy")); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
modules/swagger-parser-v3/src/test/resources/issue-2406/openapi.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,17 @@ | ||
| openapi: 3.0.3 | ||
| info: | ||
| title: Discriminator mapping of a oneOf nested in array items with external refs | ||
| version: 1.0.0 | ||
| paths: | ||
| /search: | ||
| post: | ||
| operationId: search | ||
| requestBody: | ||
| required: true | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: './schemas/SearchRequest.yaml' | ||
| responses: | ||
| '204': | ||
| description: OK |
15 changes: 15 additions & 0 deletions
15
modules/swagger-parser-v3/src/test/resources/issue-2406/schemas/SearchRequest.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,15 @@ | ||
| type: object | ||
| properties: | ||
| query: | ||
| type: array | ||
| items: | ||
| oneOf: | ||
| - $ref: './TypeA.yaml' | ||
| - $ref: './TypeB.yaml' | ||
| discriminator: | ||
| propertyName: kind | ||
| mapping: | ||
| a: './TypeA.yaml' | ||
| b: './TypeB.yaml' | ||
| # several discriminator values may map to the same schema | ||
| bLegacy: './TypeB.yaml' |
7 changes: 7 additions & 0 deletions
7
modules/swagger-parser-v3/src/test/resources/issue-2406/schemas/TypeA.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,7 @@ | ||
| type: object | ||
| required: [kind] | ||
| properties: | ||
| kind: | ||
| type: string | ||
| valueA: | ||
| type: string |
7 changes: 7 additions & 0 deletions
7
modules/swagger-parser-v3/src/test/resources/issue-2406/schemas/TypeB.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,7 @@ | ||
| type: object | ||
| required: [kind] | ||
| properties: | ||
| kind: | ||
| type: string | ||
| valueB: | ||
| type: integer |
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.
suggestion: I don't think the new test reaches this part.
SearchRequestresolves throughExternalRefProcessor,which rewrites theoneOfrefs to#/components/schemas/...beforechangeDiscriminatorMappingruns. TheoldRef.equals(newRef)holds and the method returns early.Could you add a case where
SchemaProcessorrenames a ref, like a local$refwith two discriminator aliases pointing at it.