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 d3e79dbf8c1c..a6932090f857 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 @@ -1037,7 +1037,7 @@ public Schema normalizeSchema(Schema schema, Set visitedSchemas) { } else if (schema.getAdditionalProperties() instanceof Schema) { // map Schema result = normalizeMapSchema(schema); Schema additionalProperties = (Schema) result.getAdditionalProperties(); - if (getRule(NORMALIZE_31SPEC) && ModelUtils.isNullTypeSchema(openAPI, additionalProperties)) { + if (getRule(NORMALIZE_31SPEC) && isExplicitNullTypeSchema(additionalProperties)) { // OAS 3.1 allows a map value schema of `type: "null"` (e.g. // `additionalProperties: { type: "null" }`). There's no OAS 3.0 equivalent type, // so generators emit a fictional `Null` / `ModelNull` value type that fails to @@ -1845,6 +1845,16 @@ protected Schema processReplaceOneOfByMapping(Schema schema) { return schema; } + /** + * {@link ModelUtils#isNullTypeSchema} also matches type-less schemas such as {@code {}} and + * {@code true}, which mean "any value" and must not become nullable. + */ + private boolean isExplicitNullTypeSchema(Schema schema) { + Schema resolved = ModelUtils.getReferencedSchema(openAPI, schema); + return ModelUtils.isNullTypeSchema(openAPI, resolved) + && !ModelUtils.isAnyType(resolved); + } + private boolean isInlineSchema(Schema schema) { if (openAPI.getComponents()!=null && openAPI.getComponents().getSchemas()!=null) { int identity = System.identityHashCode(schema); 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 8fc52c3f2f68..06a888fca6d8 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 @@ -2261,6 +2261,17 @@ public void testOpenAPINormalizer31SpecNullMapAdditionalProperties() { assertEquals(errorsValue.getType(), "array"); assertTrue(errorsValue.getNullable()); assertNotNull(errorsValue.getItems()); + + Schema nullEnumValue = ModelUtils.getAdditionalProperties( + (Schema) schema.getProperties().get("nullEnumMap")); + assertTrue(nullEnumValue.getNullable()); + + // type-less values mean "any value" and must stay non-nullable + for (String name : List.of("emptySchemaMap", "trueMap", "refToEmptyMap")) { + Schema value = ModelUtils.getAdditionalProperties( + (Schema) schema.getProperties().get(name)); + assertNull(value.getNullable(), name); + } } } diff --git a/modules/openapi-generator/src/test/resources/3_1/issue_23945.yaml b/modules/openapi-generator/src/test/resources/3_1/issue_23945.yaml index b1bb362584d5..d5484b0b7b56 100644 --- a/modules/openapi-generator/src/test/resources/3_1/issue_23945.yaml +++ b/modules/openapi-generator/src/test/resources/3_1/issue_23945.yaml @@ -20,3 +20,18 @@ components: properties: code: type: string + emptySchemaMap: + type: object + additionalProperties: {} + trueMap: + type: object + additionalProperties: true + nullEnumMap: + type: object + additionalProperties: + enum: [null] + refToEmptyMap: + type: object + additionalProperties: + $ref: '#/components/schemas/Anything' + Anything: {}