diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java index d396acc8566f..9102fd5bb41d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java @@ -1782,6 +1782,21 @@ protected Schema processNormalize31Spec(Schema schema, Set 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) { @@ -1803,12 +1818,6 @@ protected Schema processNormalize31Spec(Schema schema, Set 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()); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index a8f62b1b8331..5a67339fcaf3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -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 diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java index e38373e8d81c..0226a24e7dc4 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java @@ -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 diff --git a/modules/openapi-generator/src/test/resources/3_1/const-type-inference.yaml b/modules/openapi-generator/src/test/resources/3_1/const-type-inference.yaml new file mode 100644 index 000000000000..561860737b82 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_1/const-type-inference.yaml @@ -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