-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: treat number example as number and not string #5062
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
Mattias-Sehlstedt
wants to merge
2
commits into
swagger-api:master
Choose a base branch
from
Mattias-Sehlstedt:treat-number-example-as-number-and-not-string
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.
+166
−4
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
105 changes: 105 additions & 0 deletions
105
modules/swagger-core/src/test/java/io/swagger/v3/core/converting/Issue5061Test.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,105 @@ | ||
| package io.swagger.v3.core.converting; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.swagger.v3.core.converter.ModelConverters; | ||
| import io.swagger.v3.core.converter.ResolvedSchema; | ||
| import io.swagger.v3.core.util.Json31; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| import static org.testng.Assert.assertNotNull; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * test documenting how example values for numbers/non-number differ in their JSON-representation. | ||
| */ | ||
| public class Issue5061Test { | ||
|
|
||
|
|
||
| @Test | ||
| public void testExampleValuesAreSerializedAsJsonDifferentlyBetweenStringAndNumber() throws Exception { | ||
| ResolvedSchema schema = ModelConverters.getInstance(true).readAllAsResolvedSchema( | ||
| ModelWithDifferentCombinationOfNumberFieldsWithExamples.class | ||
| ); | ||
|
|
||
| assertNotNull(schema, "Schema should resolve"); | ||
| String json = Json31.pretty(schema); | ||
| assertNotNull(json); | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| JsonNode root = mapper.readTree(json); | ||
|
|
||
| JsonNode stringFieldType = root.at("/schema/properties/stringFieldType"); | ||
| assertExampleIsString(stringFieldType); | ||
|
|
||
| JsonNode stringFieldTypeWithExplicitStringSchemaType = root.at("/schema/properties/stringFieldTypeWithExplicitStringSchemaType"); | ||
| assertExampleIsString(stringFieldTypeWithExplicitStringSchemaType); | ||
|
|
||
| JsonNode stringFieldTypeWithExplicitNumberSchemaType = root.at("/schema/properties/stringFieldTypeWithExplicitNumberSchemaType"); | ||
| assertExampleIsNumber(stringFieldTypeWithExplicitNumberSchemaType); | ||
|
|
||
| JsonNode stringFieldTypeWithExplicitIntegerSchemaType = root.at("/schema/properties/stringFieldTypeWithExplicitIntegerSchemaType"); | ||
| assertExampleIsNumber(stringFieldTypeWithExplicitIntegerSchemaType); | ||
|
|
||
| JsonNode bigDecimalFieldTypeWithExplicitStringSchemaType = root.at("/schema/properties/bigDecimalFieldTypeWithExplicitStringSchemaType"); | ||
| assertExampleIsString(bigDecimalFieldTypeWithExplicitStringSchemaType); | ||
|
|
||
| JsonNode bigDecimalFieldType = root.at("/schema/properties/bigDecimalFieldType"); | ||
| assertExampleIsNumber(bigDecimalFieldType); | ||
| } | ||
|
|
||
| private void assertExampleIsNumber(JsonNode node) { | ||
| assertTrue(node.get("example").isNumber(), "should be a number"); | ||
| } | ||
|
|
||
| private void assertExampleIsString(JsonNode node) { | ||
| assertTrue(node.get("example").isTextual(), "should be a string"); | ||
| } | ||
|
|
||
| public static class ModelWithDifferentCombinationOfNumberFieldsWithExamples { | ||
|
|
||
| @io.swagger.v3.oas.annotations.media.Schema(example = "5 lacs per annum") | ||
| String stringFieldType; | ||
|
|
||
| @io.swagger.v3.oas.annotations.media.Schema(type = "string", example = "5 lacs per annum") | ||
| String stringFieldTypeWithExplicitStringSchemaType; | ||
|
|
||
| @io.swagger.v3.oas.annotations.media.Schema(type = "number", example = "10") | ||
| String stringFieldTypeWithExplicitNumberSchemaType; | ||
|
|
||
| @io.swagger.v3.oas.annotations.media.Schema(type = "integer", example = "5") | ||
| String stringFieldTypeWithExplicitIntegerSchemaType; | ||
|
|
||
| @io.swagger.v3.oas.annotations.media.Schema(type = "string", example = "13.37") | ||
| BigDecimal bigDecimalFieldTypeWithExplicitStringSchemaType; | ||
|
|
||
| @io.swagger.v3.oas.annotations.media.Schema(example = "13.37") | ||
| BigDecimal bigDecimalFieldType; | ||
|
|
||
| public String getStringFieldType() { | ||
| return stringFieldType; | ||
| } | ||
|
|
||
| public String getStringFieldTypeWithExplicitStringSchemaType() { | ||
| return stringFieldTypeWithExplicitStringSchemaType; | ||
| } | ||
|
|
||
| public String getStringFieldTypeWithExplicitNumberSchemaType() { | ||
| return stringFieldTypeWithExplicitNumberSchemaType; | ||
| } | ||
|
|
||
| public String getStringFieldTypeWithExplicitIntegerSchemaType() { | ||
| return stringFieldTypeWithExplicitIntegerSchemaType; | ||
| } | ||
|
|
||
| public BigDecimal getBigDecimalFieldTypeWithExplicitStringSchemaType() { | ||
| return bigDecimalFieldTypeWithExplicitStringSchemaType; | ||
| } | ||
|
|
||
| public BigDecimal getBigDecimalFieldType() { | ||
| return bigDecimalFieldType; | ||
| } | ||
|
|
||
| } | ||
| } |
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
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.
With the addition of support for example/default "null", it was stated that a
nullshould only occur if the field is also nullable. But I would assume that that is not the case here when the schema is aNumberschema, and that the correct behavior is instead to retain the null as a JsonNode like the change here. Otherwise the value will not align with the type.Ignoring null altogether could also be an option for this scenario. But I would need someone to state the expected behavior then. I could also implement so that default and example share their definition, since it seems that that should actually be the case? But that is for a separate PR most likely.