diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OAI31DeserializationTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OAI31DeserializationTest.java index 0ac3105187..ff515a16b3 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OAI31DeserializationTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OAI31DeserializationTest.java @@ -1,5 +1,9 @@ package io.swagger.v3.parser.test; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.NullNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.security.SecurityRequirement; @@ -8,6 +12,7 @@ import io.swagger.v3.parser.core.models.SwaggerParseResult; import org.testng.annotations.Test; +import java.math.BigDecimal; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -56,6 +61,46 @@ public void testSchemaKeysOAS31() { assertTrue(patternProperties.getTypes().contains("string")); } + @Test(description = "Test OAS31 Schema const deserialization") + public void testSchemaConstOAS31() { + SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/issue-1975.yaml", null, null); + assertNotNull(result.getOpenAPI()); + OpenAPI openAPI = result.getOpenAPI(); + + assertTrue(result.getMessages().size() == 0); + assertEquals(openAPI.getComponents().getSchemas().get("ConstValidation").getConst(), 2); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode objNode = mapper.createObjectNode(); + objNode.put("foo", "bar"); + objNode.put("baz", "bax"); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithObject").getConst(), objNode); + ArrayNode arrayNode = mapper.createArrayNode(); + ObjectNode arrayItem = mapper.createObjectNode(); + arrayItem.put("foo", "bar"); + arrayNode.add(arrayItem); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithArray").getConst(), arrayNode); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithNull").getConst(), NullNode.getInstance()); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithFalseDoesNotMatch0").getConst(), false); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithTrueDoesNotMatch1").getConst(), true); + arrayNode = mapper.createArrayNode(); + arrayNode.add(false); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithArrayFalseDoesNotMatch0").getConst(), arrayNode); + arrayNode = mapper.createArrayNode(); + arrayNode.add(true); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithArrayTrueDoesNotMatch1").getConst(), arrayNode); + objNode = mapper.createObjectNode(); + objNode.put("a", false); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithAFalseDoesNotMatchA0").getConst(), objNode); + objNode = mapper.createObjectNode(); + objNode.put("a", true); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWithATrueDoesNotMatchA1").getConst(), objNode); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWith0DoesNotMatchOtherZeroLikeTypes").getConst(), 0); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWith1DoesNotMatchTrue").getConst(), 1); + assertEquals(openAPI.getComponents().getSchemas().get("ConstWith20MatchesIntegerAndFloatTypes").getConst(), new BigDecimal("-2.0")); + assertEquals(openAPI.getComponents().getSchemas().get("ConstFloatAndIntegersAreEqualUpTo64BitRepresentationLimits").getConst(), new BigDecimal(9007199254740992L)); + assertEquals(openAPI.getComponents().getSchemas().get("ConstNulCharactersInStrings").getConst(), "hello\0there"); + } + @Test(description = "Test basic OAS31 deserialization/validation") public void testBasicOAS31() { SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/test/basicOAS31.yaml", null, null); diff --git a/modules/swagger-parser-v3/src/test/resources/3.1.0/issue-1975.yaml b/modules/swagger-parser-v3/src/test/resources/3.1.0/issue-1975.yaml new file mode 100644 index 0000000000..08db7434b8 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/3.1.0/issue-1975.yaml @@ -0,0 +1,64 @@ +openapi: 3.1.0 +servers: + - url: https://someserver.com/v1 +info: + title: openapi 3.1.0 sample spec + version: 0.0.1 + description: sample spec for testing openapi functionality, built from json schema + tests for draft2020-12 +tags: [] +paths: {} +components: + schemas: + ConstValidation: + $schema: https://json-schema.org/draft/2020-12/schema + const: 2 + ConstWithObject: + $schema: https://json-schema.org/draft/2020-12/schema + const: + foo: bar + baz: bax + ConstWithArray: + $schema: https://json-schema.org/draft/2020-12/schema + const: + - foo: bar + ConstWithNull: + $schema: https://json-schema.org/draft/2020-12/schema + const: null + ConstWithFalseDoesNotMatch0: + $schema: https://json-schema.org/draft/2020-12/schema + const: false + ConstWithTrueDoesNotMatch1: + $schema: https://json-schema.org/draft/2020-12/schema + const: true + ConstWithArrayFalseDoesNotMatch0: + $schema: https://json-schema.org/draft/2020-12/schema + const: + - false + ConstWithArrayTrueDoesNotMatch1: + $schema: https://json-schema.org/draft/2020-12/schema + const: + - true + ConstWithAFalseDoesNotMatchA0: + $schema: https://json-schema.org/draft/2020-12/schema + const: + a: false + ConstWithATrueDoesNotMatchA1: + $schema: https://json-schema.org/draft/2020-12/schema + const: + a: true + ConstWith0DoesNotMatchOtherZeroLikeTypes: + $schema: https://json-schema.org/draft/2020-12/schema + const: 0 + ConstWith1DoesNotMatchTrue: + $schema: https://json-schema.org/draft/2020-12/schema + const: 1 + ConstWith20MatchesIntegerAndFloatTypes: + $schema: https://json-schema.org/draft/2020-12/schema + const: -2.0 + ConstFloatAndIntegersAreEqualUpTo64BitRepresentationLimits: + $schema: https://json-schema.org/draft/2020-12/schema + const: 9007199254740992 + ConstNulCharactersInStrings: + $schema: https://json-schema.org/draft/2020-12/schema + const: "hello\0there" \ No newline at end of file