Skip to content

Commit dfda9d9

Browse files
spacetherfrantuma
authored andcommitted
Adds java tests of deserialized const values
1 parent 6874eaf commit dfda9d9

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OAI31DeserializationTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.swagger.v3.parser.test;
22

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;
37
import io.swagger.v3.oas.models.OpenAPI;
48
import io.swagger.v3.oas.models.media.Schema;
59
import io.swagger.v3.oas.models.security.SecurityRequirement;
@@ -8,6 +12,7 @@
812
import io.swagger.v3.parser.core.models.SwaggerParseResult;
913
import org.testng.annotations.Test;
1014

15+
import java.math.BigDecimal;
1116
import java.util.Arrays;
1217
import java.util.Collections;
1318
import java.util.List;
@@ -56,6 +61,46 @@ public void testSchemaKeysOAS31() {
5661
assertTrue(patternProperties.getTypes().contains("string"));
5762
}
5863

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+
59104
@Test(description = "Test basic OAS31 deserialization/validation")
60105
public void testBasicOAS31() {
61106
SwaggerParseResult result = new OpenAPIV3Parser().readLocation( "3.1.0/test/basicOAS31.yaml", null, null);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
openapi: 3.1.0
2+
servers:
3+
- url: https://someserver.com/v1
4+
info:
5+
title: openapi 3.1.0 sample spec
6+
version: 0.0.1
7+
description: sample spec for testing openapi functionality, built from json schema
8+
tests for draft2020-12
9+
tags: []
10+
paths: {}
11+
components:
12+
schemas:
13+
ConstValidation:
14+
$schema: https://json-schema.org/draft/2020-12/schema
15+
const: 2
16+
ConstWithObject:
17+
$schema: https://json-schema.org/draft/2020-12/schema
18+
const:
19+
foo: bar
20+
baz: bax
21+
ConstWithArray:
22+
$schema: https://json-schema.org/draft/2020-12/schema
23+
const:
24+
- foo: bar
25+
ConstWithNull:
26+
$schema: https://json-schema.org/draft/2020-12/schema
27+
const: null
28+
ConstWithFalseDoesNotMatch0:
29+
$schema: https://json-schema.org/draft/2020-12/schema
30+
const: false
31+
ConstWithTrueDoesNotMatch1:
32+
$schema: https://json-schema.org/draft/2020-12/schema
33+
const: true
34+
ConstWithArrayFalseDoesNotMatch0:
35+
$schema: https://json-schema.org/draft/2020-12/schema
36+
const:
37+
- false
38+
ConstWithArrayTrueDoesNotMatch1:
39+
$schema: https://json-schema.org/draft/2020-12/schema
40+
const:
41+
- true
42+
ConstWithAFalseDoesNotMatchA0:
43+
$schema: https://json-schema.org/draft/2020-12/schema
44+
const:
45+
a: false
46+
ConstWithATrueDoesNotMatchA1:
47+
$schema: https://json-schema.org/draft/2020-12/schema
48+
const:
49+
a: true
50+
ConstWith0DoesNotMatchOtherZeroLikeTypes:
51+
$schema: https://json-schema.org/draft/2020-12/schema
52+
const: 0
53+
ConstWith1DoesNotMatchTrue:
54+
$schema: https://json-schema.org/draft/2020-12/schema
55+
const: 1
56+
ConstWith20MatchesIntegerAndFloatTypes:
57+
$schema: https://json-schema.org/draft/2020-12/schema
58+
const: -2.0
59+
ConstFloatAndIntegersAreEqualUpTo64BitRepresentationLimits:
60+
$schema: https://json-schema.org/draft/2020-12/schema
61+
const: 9007199254740992
62+
ConstNulCharactersInStrings:
63+
$schema: https://json-schema.org/draft/2020-12/schema
64+
const: "hello\0there"

0 commit comments

Comments
 (0)