|
1 | 1 | package io.swagger.v3.parser.test;
|
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 5 | +import com.fasterxml.jackson.databind.node.NullNode; |
| 6 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
3 | 7 | import io.swagger.v3.oas.models.OpenAPI;
|
4 | 8 | import io.swagger.v3.oas.models.media.Schema;
|
5 | 9 | import io.swagger.v3.oas.models.security.SecurityRequirement;
|
|
8 | 12 | import io.swagger.v3.parser.core.models.SwaggerParseResult;
|
9 | 13 | import org.testng.annotations.Test;
|
10 | 14 |
|
| 15 | +import java.math.BigDecimal; |
11 | 16 | import java.util.Arrays;
|
12 | 17 | import java.util.Collections;
|
13 | 18 | import java.util.List;
|
@@ -56,6 +61,46 @@ public void testSchemaKeysOAS31() {
|
56 | 61 | assertTrue(patternProperties.getTypes().contains("string"));
|
57 | 62 | }
|
58 | 63 |
|
| 64 | + @Test(description = "Test OAS31 Schema const deserialization") |
| 65 | + public void testSchemaConstOAS31() { |
| 66 | + SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/issue-1975.yaml", null, null); |
| 67 | + assertNotNull(result.getOpenAPI()); |
| 68 | + OpenAPI openAPI = result.getOpenAPI(); |
| 69 | + |
| 70 | + assertTrue(result.getMessages().size() == 0); |
| 71 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstValidation").getConst(), 2); |
| 72 | + ObjectMapper mapper = new ObjectMapper(); |
| 73 | + ObjectNode objNode = mapper.createObjectNode(); |
| 74 | + objNode.put("foo", "bar"); |
| 75 | + objNode.put("baz", "bax"); |
| 76 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithObject").getConst(), objNode); |
| 77 | + ArrayNode arrayNode = mapper.createArrayNode(); |
| 78 | + ObjectNode arrayItem = mapper.createObjectNode(); |
| 79 | + arrayItem.put("foo", "bar"); |
| 80 | + arrayNode.add(arrayItem); |
| 81 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithArray").getConst(), arrayNode); |
| 82 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithNull").getConst(), NullNode.getInstance()); |
| 83 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithFalseDoesNotMatch0").getConst(), false); |
| 84 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithTrueDoesNotMatch1").getConst(), true); |
| 85 | + arrayNode = mapper.createArrayNode(); |
| 86 | + arrayNode.add(false); |
| 87 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithArrayFalseDoesNotMatch0").getConst(), arrayNode); |
| 88 | + arrayNode = mapper.createArrayNode(); |
| 89 | + arrayNode.add(true); |
| 90 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithArrayTrueDoesNotMatch1").getConst(), arrayNode); |
| 91 | + objNode = mapper.createObjectNode(); |
| 92 | + objNode.put("a", false); |
| 93 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithAFalseDoesNotMatchA0").getConst(), objNode); |
| 94 | + objNode = mapper.createObjectNode(); |
| 95 | + objNode.put("a", true); |
| 96 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithATrueDoesNotMatchA1").getConst(), objNode); |
| 97 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWith0DoesNotMatchOtherZeroLikeTypes").getConst(), 0); |
| 98 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWith1DoesNotMatchTrue").getConst(), 1); |
| 99 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstWith20MatchesIntegerAndFloatTypes").getConst(), new BigDecimal("-2.0")); |
| 100 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstFloatAndIntegersAreEqualUpTo64BitRepresentationLimits").getConst(), new BigDecimal(9007199254740992L)); |
| 101 | + assertEquals(openAPI.getComponents().getSchemas().get("ConstNulCharactersInStrings").getConst(), "hello\0there"); |
| 102 | + } |
| 103 | + |
59 | 104 | @Test(description = "Test basic OAS31 deserialization/validation")
|
60 | 105 | public void testBasicOAS31() {
|
61 | 106 | SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/test/basicOAS31.yaml", null, null);
|
|
0 commit comments