Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue 1975: bug in const processing, const values of all types now processable #1996

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4155,10 +4155,9 @@ public Schema getJsonSchema(JsonNode jsonNode, String location, ParseResult resu
schema.setPatternProperties(patternProperties);
}

//const is a String
value = getString("const", node, false, location, result);
if (value != null) {
schema.setConst(value);
Object constValue = getAnyType("const", node, location, result);
if (constValue != null) {
schema.setConst(constValue);
}

value = getString("contentEncoding", node, false, location, result);
Expand Down
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a LinkedHashMap instead?

ArrayNode arrayNode = mapper.createArrayNode();
ObjectNode arrayItem = mapper.createObjectNode();
arrayItem.put("foo", "bar");
arrayNode.add(arrayItem);
assertEquals(openAPI.getComponents().getSchemas().get("ConstWithArray").getConst(), arrayNode);
Copy link
Contributor Author

@spacether spacether Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a List or ArrayList instead?

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"
Loading