Skip to content

Commit a4dd90c

Browse files
authored
Better allOf handling in fromProperty (#15035)
* fix allOf handling in fromProperty * add null check, update samples * update dart generator to handle allof with a single ref
1 parent 56e5122 commit a4dd90c

27 files changed

Lines changed: 129 additions & 37 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,8 +3795,8 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
37953795
}
37963796

37973797
Schema original = null;
3798-
// check if it's allOf (only 1 sub schema) with default/nullable/etc set in the top level
3799-
if (ModelUtils.isAllOf(p) && p.getAllOf().size() == 1 && ModelUtils.hasCommonAttributesDefined(p) ) {
3798+
// check if it's allOf (only 1 sub schema) with or without default/nullable/etc set in the top level
3799+
if (ModelUtils.isAllOf(p) && p.getAllOf().size() == 1 && !(this instanceof PythonClientCodegen)) {
38003800
if (p.getAllOf().get(0) instanceof Schema) {
38013801
original = p;
38023802
p = (Schema) p.getAllOf().get(0);
@@ -4002,7 +4002,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
40024002
// restore original schema with default value, nullable, readonly etc
40034003
if (original != null) {
40044004
p = original;
4005-
// evaluate common attributes defined in the top level
4005+
// evaluate common attributes if defined in the top level
40064006
if (p.getNullable() != null) {
40074007
property.isNullable = p.getNullable();
40084008
} else if (p.getExtensions() != null && p.getExtensions().containsKey("x-nullable")) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
555555
public CodegenProperty fromProperty(String name, Schema p, boolean required) {
556556
final CodegenProperty property = super.fromProperty(name, p, required);
557557

558-
// Handle composed properties
559-
if (ModelUtils.isComposedSchema(p)) {
558+
// Handle composed properties and it's NOT allOf with a single ref only
559+
if (ModelUtils.isComposedSchema(p) && !(ModelUtils.isAllOf(p) && p.getAllOf().size() == 1)) {
560560
ComposedSchema composed = (ComposedSchema) p;
561561

562562
// Count the occurrences of allOf/anyOf/oneOf with exactly one child element

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4582,6 +4582,18 @@ public void testAllOfDefaultEnumType() {
45824582
Assert.assertFalse(defaultEnumSchemaProperty.isContainer);
45834583
Assert.assertFalse(defaultEnumSchemaProperty.isPrimitiveType);
45844584
Assert.assertEquals(defaultEnumSchemaProperty.defaultValue, "2");
4585+
4586+
// test allOf with a single sub-schema and no default value set in the top level
4587+
CodegenProperty allOfEnumSchemaProperty = modelWithReferencedSchema.vars.get(5);
4588+
Assert.assertEquals(allOfEnumSchemaProperty.getName(), "allofMinusnumberMinusenum");
4589+
Assert.assertFalse(allOfEnumSchemaProperty.isEnum);
4590+
Assert.assertTrue(allOfEnumSchemaProperty.getIsEnumOrRef());
4591+
Assert.assertTrue(allOfEnumSchemaProperty.isEnumRef);
4592+
Assert.assertFalse(allOfEnumSchemaProperty.isInnerEnum);
4593+
Assert.assertFalse(allOfEnumSchemaProperty.isString);
4594+
Assert.assertFalse(allOfEnumSchemaProperty.isContainer);
4595+
Assert.assertFalse(allOfEnumSchemaProperty.isPrimitiveType);
4596+
Assert.assertEquals(allOfEnumSchemaProperty.defaultValue, "null");
45854597
}
45864598

45874599
@Test

modules/openapi-generator/src/test/resources/3_0/issue-5676-enums.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,7 @@ components:
215215
default: 2
216216
allOf:
217217
- $ref: "#/components/schemas/NumberEnum"
218+
allof-number-enum:
219+
allOf:
220+
- $ref: "#/components/schemas/NumberEnum"
218221

samples/client/petstore/elixir/lib/openapi_petstore/model/all_of_with_single_ref.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ defmodule OpenapiPetstore.Model.AllOfWithSingleRef do
1414

1515
@type t :: %__MODULE__{
1616
:username => String.t | nil,
17-
:SingleRefType => any() | nil
17+
:SingleRefType => OpenapiPetstore.Model.SingleRefType.t | nil
1818
}
1919
end
2020

2121
defimpl Poison.Decoder, for: OpenapiPetstore.Model.AllOfWithSingleRef do
22-
def decode(value, _options) do
22+
import OpenapiPetstore.Deserializer
23+
def decode(value, options) do
2324
value
25+
|> deserialize(:SingleRefType, :struct, OpenapiPetstore.Model.SingleRefType, options)
2426
end
2527
end
2628

samples/client/petstore/java-helidon-client/mp/docs/AllOfWithSingleRef.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
| Name | Type | Description | Notes |
99
|------------ | ------------- | ------------- | -------------|
1010
|**username** | **String** | | [optional] |
11-
|**singleRefType** | [**SingleRefType**](SingleRefType.md) | | [optional] |
11+
|**singleRefType** | **SingleRefType** | | [optional] |
1212

1313

1414

samples/client/petstore/java-helidon-client/se/docs/AllOfWithSingleRef.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
| Name | Type | Description | Notes |
99
|------------ | ------------- | ------------- | -------------|
1010
|**username** | **String** | | [optional] |
11-
|**singleRefType** | [**SingleRefType**](SingleRefType.md) | | [optional] |
11+
|**singleRefType** | **SingleRefType** | | [optional] |
1212

1313

1414

samples/client/petstore/java/apache-httpclient/docs/AllOfWithSingleRef.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
| Name | Type | Description | Notes |
99
|------------ | ------------- | ------------- | -------------|
1010
|**username** | **String** | | [optional] |
11-
|**singleRefType** | [**SingleRefType**](SingleRefType.md) | | [optional] |
11+
|**singleRefType** | **SingleRefType** | | [optional] |
1212

1313

1414

samples/client/petstore/java/apache-httpclient/src/main/java/org/openapitools/client/model/AllOfWithSingleRef.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ public String toUrlQueryString(String prefix) {
180180

181181
// add `SingleRefType` to the URL query string
182182
if (getSingleRefType() != null) {
183-
joiner.add(getSingleRefType().toUrlQueryString(prefix + "SingleRefType" + suffix));
183+
try {
184+
joiner.add(String.format("%sSingleRefType%s=%s", prefix, suffix, URLEncoder.encode(String.valueOf(getSingleRefType()), "UTF-8").replaceAll("\\+", "%20")));
185+
} catch (UnsupportedEncodingException e) {
186+
// Should never happen, UTF-8 is always supported
187+
throw new RuntimeException(e);
188+
}
184189
}
185190

186191
return joiner.toString();

samples/client/petstore/java/webclient-jakarta/docs/AllOfWithSingleRef.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
| Name | Type | Description | Notes |
99
|------------ | ------------- | ------------- | -------------|
1010
|**username** | **String** | | [optional] |
11-
|**singleRefType** | [**SingleRefType**](SingleRefType.md) | | [optional] |
11+
|**singleRefType** | **SingleRefType** | | [optional] |
1212

1313

1414

0 commit comments

Comments
 (0)