Skip to content

Commit ea9937a

Browse files
committed
test: fix implementation and add unit test for resolving default in nested allOf schemas
1 parent 4c7032c commit ea9937a

2 files changed

Lines changed: 41 additions & 15 deletions

File tree

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ public static boolean isMapSchema(Schema schema) {
583583

584584
// additionalProperties explicitly set to false
585585
if ((schema.getAdditionalProperties() instanceof Boolean && Boolean.FALSE.equals(schema.getAdditionalProperties())) ||
586-
(schema.getAdditionalProperties() instanceof Schema && Boolean.FALSE.equals(((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue()))
586+
(schema.getAdditionalProperties() instanceof Schema && Boolean.FALSE.equals(((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue()))
587587
) {
588588
return false;
589589
}
@@ -726,7 +726,7 @@ public static boolean isDateTimeLocalSchema(Schema schema) {
726726
public static boolean isTimeLocalSchema(Schema schema) {
727727
// format: time-local, see https://spec.openapis.org/registry/format/time-local.html
728728
return (SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
729-
&& "time-local".equals(schema.getFormat()));
729+
&& "time-local".equals(schema.getFormat()));
730730
}
731731

732732
public static boolean isPasswordSchema(Schema schema) {
@@ -845,12 +845,12 @@ public static boolean isModelWithPropertiesOnly(Schema schema) {
845845
(null != schema.getProperties() && !schema.getProperties().isEmpty()) &&
846846
// no additionalProperties is set
847847
(schema.getAdditionalProperties() == null ||
848-
// additionalProperties is boolean and set to false
849-
(schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties()) ||
850-
// additionalProperties is a schema with its boolean value set to false
851-
(schema.getAdditionalProperties() instanceof Schema &&
852-
((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue() != null &&
853-
!((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue())
848+
// additionalProperties is boolean and set to false
849+
(schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties()) ||
850+
// additionalProperties is a schema with its boolean value set to false
851+
(schema.getAdditionalProperties() instanceof Schema &&
852+
((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue() != null &&
853+
!((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue())
854854
);
855855
}
856856

@@ -989,7 +989,7 @@ public static ResolvedMaxBound resolveMaximumBound(OpenAPI openAPI, Schema<?> sc
989989
return !hasAllOf(schema)
990990
? result
991991
: schema.getAllOf().stream()
992-
// recursive search for smallest max bound
992+
// recursive search for smallest max bound
993993
.map(allOfItem -> resolveMaximumBound(openAPI, allOfItem))
994994
.reduce(result, ResolvedMaxBound::getSmallerMaxBound);
995995
}
@@ -1022,9 +1022,20 @@ public static ResolvedMinBound resolveMinimumBound(OpenAPI openAPI, Schema<?> sc
10221022

10231023
/**
10241024
* Returns the effective {@code default} for the given schema, resolving through a top-level
1025-
* {@code $ref} and walking any {@code allOf} items (each resolved via their own {@code $ref}).
1026-
* Unlike constraints, the inline schema's default takes precedence (explicit per-endpoint
1027-
* override); falls back to the first non-null default found in {@code allOf} items.
1025+
* {@code $ref} and recursively walking any {@code allOf} items.
1026+
* The inline schema's default takes precedence (explicit per-endpoint override);
1027+
* falls back to the first non-null default found via depth-first search of {@code allOf} items.
1028+
* Circular {@code allOf} references are detected and skipped.
1029+
*
1030+
* @param openAPI the OpenAPI document used to resolve {@code $ref}s
1031+
* @param schema the schema to inspect
1032+
* @return the effective default value, or {@code null} if none is defined
1033+
*/
1034+
/**
1035+
* Returns the effective {@code default} for the given schema, resolving through a top-level
1036+
* {@code $ref} and recursively walking any {@code allOf} items.
1037+
* The inline schema's default takes precedence (explicit per-endpoint override);
1038+
* falls back to the first non-null default found via depth-first search of {@code allOf} items.
10281039
*
10291040
* @param openAPI the OpenAPI document used to resolve {@code $ref}s
10301041
* @param schema the schema to inspect
@@ -1042,9 +1053,8 @@ public static Object resolveDefault(OpenAPI openAPI, Schema<?> schema) {
10421053
return !hasAllOf(schema)
10431054
? null
10441055
: schema.getAllOf().stream()
1045-
.map(item -> getReferencedSchema(openAPI, item))
1046-
.filter(Objects::nonNull)
1047-
.map(Schema::getDefault)
1056+
// recursive search for default
1057+
.map(item -> resolveDefault(openAPI, item))
10481058
.filter(Objects::nonNull)
10491059
.findFirst()
10501060
.orElse(null);

modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,4 +1257,20 @@ public void resolveDefault_stringDefault_returnsIt() {
12571257
schema.setDefault("hello");
12581258
assertEquals(ModelUtils.resolveDefault(openAPI, schema), "hello");
12591259
}
1260+
1261+
@Test
1262+
public void resolveDefault_nestedAllOf_findsDefaultInNestedItem() {
1263+
OpenAPI openAPI = TestUtils.createOpenAPI();
1264+
// base has default=99; mid allOf → base; top allOf → mid
1265+
Schema<?> base = new IntegerSchema();
1266+
base.setDefault(99);
1267+
openAPI.getComponents().addSchemas("Base", base);
1268+
1269+
Schema<?> mid = new Schema<>().allOf(List.of(new Schema<>().$ref("#/components/schemas/Base")));
1270+
openAPI.getComponents().addSchemas("Mid", mid);
1271+
1272+
Schema<?> top = new Schema<>().allOf(List.of(new Schema<>().$ref("#/components/schemas/Mid")));
1273+
1274+
assertEquals(ModelUtils.resolveDefault(openAPI, top), 99);
1275+
}
12601276
}

0 commit comments

Comments
 (0)