Skip to content

Commit

Permalink
Adds java tests of deserialized const values
Browse files Browse the repository at this point in the history
  • Loading branch information
spacether authored and frantuma committed Nov 7, 2023
1 parent 2ff89df commit 1b1fa85
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
64 changes: 64 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/3.1.0/issue-1975.yaml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 1b1fa85

Please sign in to comment.