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 @@ -1037,7 +1037,7 @@ public Schema normalizeSchema(Schema schema, Set<Schema> 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
Expand Down Expand Up @@ -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);
Comment thread
Robuske marked this conversation as resolved.
Resolved
Comment thread
Robuske marked this conversation as resolved.
Resolved
}

private boolean isInlineSchema(Schema schema) {
if (openAPI.getComponents()!=null && openAPI.getComponents().getSchemas()!=null) {
int identity = System.identityHashCode(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Comment thread
Robuske marked this conversation as resolved.
Resolved
Schema value = ModelUtils.getAdditionalProperties(
(Schema) schema.getProperties().get(name));
assertNull(value.getNullable(), name);
}
}

}
Expand Down
15 changes: 15 additions & 0 deletions modules/openapi-generator/src/test/resources/3_1/issue_23945.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}