|
| 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.Json; |
| 8 | +import io.swagger.v3.core.util.Json31; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +import java.math.BigDecimal; |
| 12 | + |
| 13 | +import static org.testng.Assert.assertNotNull; |
| 14 | +import static org.testng.Assert.assertTrue; |
| 15 | + |
| 16 | +/** |
| 17 | + * test documenting how example values for numbers/non-number differ in their JSON-representation. |
| 18 | + */ |
| 19 | +public class Issue5061Test { |
| 20 | + |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testExampleValuesAreSerializedAsJsonDifferentlyBetweenStringAndNumber() throws Exception { |
| 24 | + ResolvedSchema schema = ModelConverters.getInstance(true).readAllAsResolvedSchema( |
| 25 | + ModelWithDifferentCombinationOfNumberFieldsWithExamples.class |
| 26 | + ); |
| 27 | + |
| 28 | + assertNotNull(schema, "Schema should resolve"); |
| 29 | + String json = Json31.pretty(schema); |
| 30 | + assertNotNull(json); |
| 31 | + ObjectMapper mapper = new ObjectMapper(); |
| 32 | + JsonNode root = mapper.readTree(json); |
| 33 | + |
| 34 | + JsonNode stringFieldType = root.at("/schema/properties/stringFieldType"); |
| 35 | + assertExampleIsString(stringFieldType); |
| 36 | + |
| 37 | + JsonNode stringFieldTypeWithExplicitStringSchemaType = root.at("/schema/properties/stringFieldTypeWithExplicitStringSchemaType"); |
| 38 | + assertExampleIsString(stringFieldTypeWithExplicitStringSchemaType); |
| 39 | + |
| 40 | + JsonNode stringFieldTypeWithExplicitNumberSchemaType = root.at("/schema/properties/stringFieldTypeWithExplicitNumberSchemaType"); |
| 41 | + assertExampleIsNumber(stringFieldTypeWithExplicitNumberSchemaType); |
| 42 | + |
| 43 | + JsonNode stringFieldTypeWithExplicitIntegerSchemaType = root.at("/schema/properties/stringFieldTypeWithExplicitIntegerSchemaType"); |
| 44 | + assertExampleIsNumber(stringFieldTypeWithExplicitIntegerSchemaType); |
| 45 | + |
| 46 | + JsonNode bigDecimalFieldTypeWithExplicitStringSchemaType = root.at("/schema/properties/bigDecimalFieldTypeWithExplicitStringSchemaType"); |
| 47 | + assertExampleIsString(bigDecimalFieldTypeWithExplicitStringSchemaType); |
| 48 | + |
| 49 | + JsonNode bigDecimalFieldType = root.at("/schema/properties/bigDecimalFieldType"); |
| 50 | + assertExampleIsNumber(bigDecimalFieldType); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testDefaultValueNumericConversionThroughModelResolver() throws Exception { |
| 55 | + ResolvedSchema schema = ModelConverters.getInstance(false).readAllAsResolvedSchema( |
| 56 | + ModelWithDefaultValues.class |
| 57 | + ); |
| 58 | + |
| 59 | + assertNotNull(schema, "Schema should resolve"); |
| 60 | + String json = Json.pretty(schema); |
| 61 | + assertNotNull(json); |
| 62 | + ObjectMapper mapper = new ObjectMapper(); |
| 63 | + JsonNode root = mapper.readTree(json); |
| 64 | + |
| 65 | + JsonNode bigDecimalDefault = root.at("/schema/properties/bigDecimalWithDefault"); |
| 66 | + assertTrue(bigDecimalDefault.get("default").isNumber(), |
| 67 | + "default on BigDecimal field should be serialized as a JSON number"); |
| 68 | + |
| 69 | + JsonNode stringDefault = root.at("/schema/properties/stringWithDefault"); |
| 70 | + assertTrue(stringDefault.get("default").isTextual(), |
| 71 | + "default on explicit type=\"string\" field should remain a JSON string"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testExamplesArrayValuesAreSerializedAsJsonNumbers() throws Exception { |
| 76 | + ResolvedSchema schema = ModelConverters.getInstance(true).readAllAsResolvedSchema( |
| 77 | + ModelWithExamplesArray.class |
| 78 | + ); |
| 79 | + |
| 80 | + assertNotNull(schema, "Schema should resolve"); |
| 81 | + String json = Json31.pretty(schema); |
| 82 | + assertNotNull(json); |
| 83 | + ObjectMapper mapper = new ObjectMapper(); |
| 84 | + JsonNode root = mapper.readTree(json); |
| 85 | + |
| 86 | + JsonNode bigDecimalExamples = root.at("/schema/properties/bigDecimalWithExamples"); |
| 87 | + assertNotNull(bigDecimalExamples.get("examples"), "examples key should exist in JSON, full node: " + bigDecimalExamples); |
| 88 | + assertTrue(bigDecimalExamples.get("examples").get(0).isNumber(), |
| 89 | + "examples on BigDecimal field should be serialized as JSON numbers"); |
| 90 | + assertTrue(bigDecimalExamples.get("examples").get(1).isNumber(), |
| 91 | + "examples on BigDecimal field should be serialized as JSON numbers"); |
| 92 | + |
| 93 | + JsonNode stringExamples = root.at("/schema/properties/stringWithExamples"); |
| 94 | + assertTrue(stringExamples.get("examples").get(0).isTextual(), |
| 95 | + "examples on explicit type=\"string\" field should remain JSON strings"); |
| 96 | + } |
| 97 | + |
| 98 | + private void assertExampleIsNumber(JsonNode node) { |
| 99 | + assertTrue(node.get("example").isNumber(), "should be a number"); |
| 100 | + } |
| 101 | + |
| 102 | + private void assertExampleIsString(JsonNode node) { |
| 103 | + assertTrue(node.get("example").isTextual(), "should be a string"); |
| 104 | + } |
| 105 | + |
| 106 | + public static class ModelWithDefaultValues { |
| 107 | + |
| 108 | + @io.swagger.v3.oas.annotations.media.Schema(defaultValue = "10.00") |
| 109 | + BigDecimal bigDecimalWithDefault; |
| 110 | + |
| 111 | + @io.swagger.v3.oas.annotations.media.Schema(defaultValue = "42", type = "string") |
| 112 | + String stringWithDefault; |
| 113 | + |
| 114 | + public BigDecimal getBigDecimalWithDefault() { |
| 115 | + return bigDecimalWithDefault; |
| 116 | + } |
| 117 | + |
| 118 | + public String getStringWithDefault() { |
| 119 | + return stringWithDefault; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static class ModelWithExamplesArray { |
| 124 | + |
| 125 | + @io.swagger.v3.oas.annotations.media.Schema(examples = {"10.00", "20.50"}) |
| 126 | + BigDecimal bigDecimalWithExamples; |
| 127 | + |
| 128 | + @io.swagger.v3.oas.annotations.media.Schema(examples = {"hello", "world"}, type = "string") |
| 129 | + String stringWithExamples; |
| 130 | + |
| 131 | + public BigDecimal getBigDecimalWithExamples() { |
| 132 | + return bigDecimalWithExamples; |
| 133 | + } |
| 134 | + |
| 135 | + public String getStringWithExamples() { |
| 136 | + return stringWithExamples; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public static class ModelWithDifferentCombinationOfNumberFieldsWithExamples { |
| 141 | + |
| 142 | + @io.swagger.v3.oas.annotations.media.Schema(example = "5 lacs per annum") |
| 143 | + String stringFieldType; |
| 144 | + |
| 145 | + @io.swagger.v3.oas.annotations.media.Schema(type = "string", example = "5 lacs per annum") |
| 146 | + String stringFieldTypeWithExplicitStringSchemaType; |
| 147 | + |
| 148 | + @io.swagger.v3.oas.annotations.media.Schema(type = "number", example = "10") |
| 149 | + String stringFieldTypeWithExplicitNumberSchemaType; |
| 150 | + |
| 151 | + @io.swagger.v3.oas.annotations.media.Schema(type = "integer", example = "5") |
| 152 | + String stringFieldTypeWithExplicitIntegerSchemaType; |
| 153 | + |
| 154 | + @io.swagger.v3.oas.annotations.media.Schema(type = "string", example = "13.37") |
| 155 | + BigDecimal bigDecimalFieldTypeWithExplicitStringSchemaType; |
| 156 | + |
| 157 | + @io.swagger.v3.oas.annotations.media.Schema(example = "13.37") |
| 158 | + BigDecimal bigDecimalFieldType; |
| 159 | + |
| 160 | + public String getStringFieldType() { |
| 161 | + return stringFieldType; |
| 162 | + } |
| 163 | + |
| 164 | + public String getStringFieldTypeWithExplicitStringSchemaType() { |
| 165 | + return stringFieldTypeWithExplicitStringSchemaType; |
| 166 | + } |
| 167 | + |
| 168 | + public String getStringFieldTypeWithExplicitNumberSchemaType() { |
| 169 | + return stringFieldTypeWithExplicitNumberSchemaType; |
| 170 | + } |
| 171 | + |
| 172 | + public String getStringFieldTypeWithExplicitIntegerSchemaType() { |
| 173 | + return stringFieldTypeWithExplicitIntegerSchemaType; |
| 174 | + } |
| 175 | + |
| 176 | + public BigDecimal getBigDecimalFieldTypeWithExplicitStringSchemaType() { |
| 177 | + return bigDecimalFieldTypeWithExplicitStringSchemaType; |
| 178 | + } |
| 179 | + |
| 180 | + public BigDecimal getBigDecimalFieldType() { |
| 181 | + return bigDecimalFieldType; |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | +} |
0 commit comments