Skip to content
Open
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 @@ -1782,6 +1782,21 @@ protected Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchema
return null;
}

// process const
if (schema.getConst() != null) {
Object value = schema.getConst();
schema.setEnum(Arrays.asList(value));
schema.setConst(null);

if (schema.getTypes() == null) {
if (value instanceof String) {
schema.addType("string");
} else if (value instanceof Integer) {
schema.addType("integer");
}
}
}

if (schema instanceof JsonSchema &&
schema.get$schema() == null &&
schema.getTypes() == null && schema.getType() == null) {
Expand All @@ -1803,12 +1818,6 @@ protected Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchema
schema.getTypes().remove("null");
}

// process const
if (schema.getConst() != null) {
schema.setEnum(Arrays.asList(schema.getConst()));
schema.setConst(null);
}

// only one item (type) left
if (schema.getTypes().size() == 1) {
String type = String.valueOf(schema.getTypes().iterator().next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,10 @@ public static boolean isNullTypeSchema(OpenAPI openAPI, Schema schema) {
}
}

if (schema.getConst() != null) {
return false;
}

if (schema.getTypes() != null && !schema.getTypes().isEmpty()) {
// 3.1 spec
if (schema.getTypes().size() == 1) { // 1 type only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,23 @@ public void testOpenAPINormalizerSingleConstEnum31Spec() {
assertEquals(Arrays.asList(originalConst), normalizedTypeSchema.getEnum());
}

@Test
public void testOpenAPINormalizerConstTypeInference31Spec() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/const-type-inference.yaml");

assertNotEquals(
openAPI.getComponents().getSchemas().get("ConstStyle"),
openAPI.getComponents().getSchemas().get("EnumStyle")
);

new OpenAPINormalizer(openAPI, Map.of("NORMALIZE_31SPEC", "true")).normalize();

assertEquals(
openAPI.getComponents().getSchemas().get("ConstStyle"),
openAPI.getComponents().getSchemas().get("EnumStyle")
);
}

@Test
public void testOpenAPINormalizerProcessingAllOfSchema31Spec() {
// to test array schema processing in 3.1 spec
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.1.0
info:
title: "Const Type Inference Test"
version: 1.0.0
components:
schemas:
ConstStyle:
type: object
properties:
stringValue:
const: "hello-world"
intValue:
const: 42
EnumStyle:
type: object
properties:
stringValue:
type: string
enum:
- "hello-world"
intValue:
type: integer
enum:
- 42