|
| 1 | +package io.swagger.v3.core.converting; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import io.swagger.v3.core.converter.ModelConverters; |
| 6 | +import io.swagger.v3.core.converter.ResolvedSchema; |
| 7 | +import io.swagger.v3.core.util.Json31; |
| 8 | +import org.testng.annotations.Test; |
| 9 | + |
| 10 | +import java.math.BigDecimal; |
| 11 | + |
| 12 | +import static org.testng.Assert.assertNotNull; |
| 13 | +import static org.testng.Assert.assertTrue; |
| 14 | + |
| 15 | +/** |
| 16 | + * test documenting how example values for numbers/non-number differ in their JSON-representation. |
| 17 | + */ |
| 18 | +public class Issue5061Test { |
| 19 | + |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testExampleValuesAreSerializedAsJsonDifferentlyBetweenStringAndNumber() throws Exception { |
| 23 | + ResolvedSchema schema = ModelConverters.getInstance().readAllAsResolvedSchema( |
| 24 | + ModelWithDifferentCombinationOfNumberFieldsWithExamples.class |
| 25 | + ); |
| 26 | + |
| 27 | + assertNotNull(schema, "Schema should resolve"); |
| 28 | + String json = Json31.pretty(schema); |
| 29 | + assertNotNull(json); |
| 30 | + ObjectMapper mapper = new ObjectMapper(); |
| 31 | + JsonNode root = mapper.readTree(json); |
| 32 | + |
| 33 | + JsonNode stringFieldType = root.at("/schema/properties/stringFieldType"); |
| 34 | + assertExampleIsString(stringFieldType); |
| 35 | + |
| 36 | + JsonNode stringFieldTypeWithExplicitStringSchemaType = root.at("/schema/properties/stringFieldTypeWithExplicitStringSchemaType"); |
| 37 | + assertExampleIsString(stringFieldTypeWithExplicitStringSchemaType); |
| 38 | + |
| 39 | + JsonNode stringFieldTypeWithExplicitNumberSchemaType = root.at("/schema/properties/stringFieldTypeWithExplicitNumberSchemaType"); |
| 40 | + assertExampleIsNumber(stringFieldTypeWithExplicitNumberSchemaType); |
| 41 | + |
| 42 | + JsonNode stringFieldTypeWithExplicitIntegerSchemaType = root.at("/schema/properties/stringFieldTypeWithExplicitIntegerSchemaType"); |
| 43 | + assertExampleIsNumber(stringFieldTypeWithExplicitIntegerSchemaType); |
| 44 | + |
| 45 | + JsonNode bigDecimalFieldTypeWithExplicitStringSchemaType = root.at("/schema/properties/bigDecimalFieldTypeWithExplicitStringSchemaType"); |
| 46 | + assertExampleIsString(bigDecimalFieldTypeWithExplicitStringSchemaType); |
| 47 | + |
| 48 | + JsonNode bigDecimalFieldType = root.at("/schema/properties/bigDecimalFieldType"); |
| 49 | + assertExampleIsNumber(bigDecimalFieldType); |
| 50 | + } |
| 51 | + |
| 52 | + private void assertExampleIsNumber(JsonNode node) { |
| 53 | + assertTrue(node.get("example").isNumber(), "should be a number"); |
| 54 | + } |
| 55 | + |
| 56 | + private void assertExampleIsString(JsonNode node) { |
| 57 | + assertTrue(node.get("example").isTextual(), "should be a string"); |
| 58 | + } |
| 59 | + |
| 60 | + public static class ModelWithDifferentCombinationOfNumberFieldsWithExamples { |
| 61 | + |
| 62 | + @io.swagger.v3.oas.annotations.media.Schema(example = "5 lacs per annum") |
| 63 | + String stringFieldType; |
| 64 | + |
| 65 | + @io.swagger.v3.oas.annotations.media.Schema(type = "string", example = "5 lacs per annum") |
| 66 | + String stringFieldTypeWithExplicitStringSchemaType; |
| 67 | + |
| 68 | + @io.swagger.v3.oas.annotations.media.Schema(type = "number", example = "10") |
| 69 | + String stringFieldTypeWithExplicitNumberSchemaType; |
| 70 | + |
| 71 | + @io.swagger.v3.oas.annotations.media.Schema(type = "integer", example = "5") |
| 72 | + String stringFieldTypeWithExplicitIntegerSchemaType; |
| 73 | + |
| 74 | + @io.swagger.v3.oas.annotations.media.Schema(type = "string", example = "13.37") |
| 75 | + BigDecimal bigDecimalFieldTypeWithExplicitStringSchemaType; |
| 76 | + |
| 77 | + @io.swagger.v3.oas.annotations.media.Schema(example = "13.37") |
| 78 | + BigDecimal bigDecimalFieldType; |
| 79 | + |
| 80 | + public String getStringFieldType() { |
| 81 | + return stringFieldType; |
| 82 | + } |
| 83 | + |
| 84 | + public String getStringFieldTypeWithExplicitStringSchemaType() { |
| 85 | + return stringFieldTypeWithExplicitStringSchemaType; |
| 86 | + } |
| 87 | + |
| 88 | + public String getStringFieldTypeWithExplicitNumberSchemaType() { |
| 89 | + return stringFieldTypeWithExplicitNumberSchemaType; |
| 90 | + } |
| 91 | + |
| 92 | + public String getStringFieldTypeWithExplicitIntegerSchemaType() { |
| 93 | + return stringFieldTypeWithExplicitIntegerSchemaType; |
| 94 | + } |
| 95 | + |
| 96 | + public BigDecimal getBigDecimalFieldTypeWithExplicitStringSchemaType() { |
| 97 | + return bigDecimalFieldTypeWithExplicitStringSchemaType; |
| 98 | + } |
| 99 | + |
| 100 | + public BigDecimal getBigDecimalFieldType() { |
| 101 | + return bigDecimalFieldType; |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | +} |
0 commit comments