Skip to content
Draft
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 @@ -1037,32 +1037,25 @@ protected Schema normalizeAllOfWithProperties(Schema schema, Set<Schema> visited
}

protected Schema normalizeOneOf(Schema schema, Set<Schema> visitedSchemas) {
List<Schema> oneOfSchemas = schema.getOneOf();

if (oneOfSchemas == null) {
return schema;
}

oneOfSchemas = oneOfSchemas.stream()
.map(subSchema -> normalizeSchema(subSchema, visitedSchemas))
.collect(Collectors.toCollection(ArrayList::new));

schema.setOneOf(oneOfSchemas);

// Remove duplicate oneOf entries
ModelUtils.deduplicateOneOfSchema(schema);

schema = processSimplifyOneOfEnum(schema);

// simplify first as the schema may no longer be a oneOf after processing the rule below
schema = processSimplifyOneOf(schema);

// if it's still a oneOf, loop through the sub-schemas
if (schema.getOneOf() != null) {
for (int i = 0; i < schema.getOneOf().size(); i++) {
// normalize oneOf sub schemas one by one
Object item = schema.getOneOf().get(i);

if (item == null) {
continue;
}
if (!(item instanceof Schema)) {
throw new RuntimeException("Error! oneOf schema is not of the type Schema: " + item);
}

// update sub-schema with the updated schema
schema.getOneOf().set(i, normalizeSchema((Schema) item, visitedSchemas));
}
} else {
// normalize it as it's no longer an oneOf
if (schema.getOneOf() == null) {
schema = normalizeSchema(schema, visitedSchemas);
}

Expand Down Expand Up @@ -1551,7 +1544,6 @@ protected Schema processSimplifyOneOf(Schema schema) {
schema = simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, schema, oneOfSchemas);

if (ModelUtils.isIntegerSchema(schema) || ModelUtils.isNumberSchema(schema) || ModelUtils.isStringSchema(schema)) {
// TODO convert oneOf const to enum
schema.setOneOf(null);
}
}
Expand Down Expand Up @@ -1782,6 +1774,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 +1810,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,43 @@ 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 testOpenAPINormalizerOneOfConst31Spec() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/oneof-const.yaml");

assertNotEquals(
openAPI.getComponents().getSchemas().get("OneOfConst"),
openAPI.getComponents().getSchemas().get("Enum")
);

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

assertEquals(
openAPI.getComponents().getSchemas().get("OneOfConst"),
openAPI.getComponents().getSchemas().get("Enum")
);
}

@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
22 changes: 22 additions & 0 deletions modules/openapi-generator/src/test/resources/3_1/oneof-const.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.1.0
info:
title: "OneOf const to enum normalization"
version: 1.0.0
components:
schemas:
OneOfConst:
oneOf:
- const: foo
- const: bar
description: This is bar
- const: baz
Enum:
type: string
enum:
- foo
- bar
- baz
x-enum-descriptions:
- ""
- This is bar
- ""