Skip to content

Commit 28fd164

Browse files
committed
Fix: handle nullable schemas in ModelUtils and DefaultCodegen
1 parent d605afe commit 28fd164

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4043,7 +4043,15 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
40434043
property.isWriteOnly = p.getWriteOnly();
40444044
}
40454045
if (p.getNullable() != null) {
4046-
property.isNullable = p.getNullable();
4046+
if (Boolean.TRUE.equals(p.getNullable())) {
4047+
property.isNullable = true;
4048+
} else if (p.getOneOf() != null && p.getOneOf().stream().anyMatch(
4049+
s -> "null".equals(s.getType()))) {
4050+
property.isNullable = true;
4051+
} else {
4052+
property.isNullable = false;
4053+
}
4054+
40474055
}
40484056

40494057
if (p.getExtensions() != null && !p.getExtensions().isEmpty()) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,9 +2261,20 @@ public static Schema cloneSchema(Schema schema, boolean openapi31) {
22612261
*/
22622262
public static Schema simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(OpenAPI openAPI, Schema schema, List<Schema> subSchemas) {
22632263
if (subSchemas.removeIf(subSchema -> isNullTypeSchema(openAPI, subSchema))) {
2264-
schema.setNullable(true);
2264+
if (subSchemas.size() == 1) {
2265+
Schema<?> nonNull = subSchemas.get(0);
2266+
schema.setOneOf(Arrays.asList(
2267+
nonNull,
2268+
new Schema<>().type("null")
2269+
));
2270+
return schema;
2271+
} else {
2272+
schema.setOneOf(subSchemas);
2273+
return schema;
2274+
}
22652275
}
22662276

2277+
22672278
// if only one element left, simplify to just the element (schema)
22682279
if (subSchemas.size() == 1) {
22692280
Schema<?> subSchema = subSchemas.get(0);
@@ -2608,4 +2619,4 @@ public LinkedHashSet<String> build() {
26082619
}
26092620
}
26102621
}
2611-
}
2622+
}

0 commit comments

Comments
 (0)