Skip to content

Commit 48e8375

Browse files
authored
fix ref to allOf wrapper, add tests (#19986)
1 parent 8916987 commit 48e8375

File tree

6 files changed

+117
-2
lines changed

6 files changed

+117
-2
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3861,8 +3861,29 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
38613861
}
38623862

38633863
Schema original = null;
3864+
// process the dereference schema if it's a ref to allOf with a single item
3865+
// and certain field(s) (e.g. description, readyOnly, etc) is set
3866+
if (p.get$ref() != null) {
3867+
Schema derefSchema = ModelUtils.getReferencedSchema(openAPI, p);
3868+
if (ModelUtils.isAllOfWithSingleItem(derefSchema) && (
3869+
derefSchema.getReadOnly() != null ||
3870+
derefSchema.getWriteOnly() != null ||
3871+
derefSchema.getDeprecated() != null ||
3872+
derefSchema.getDescription() != null ||
3873+
derefSchema.getMaxLength() != null ||
3874+
derefSchema.getMinLength() != null ||
3875+
derefSchema.getMinimum() != null ||
3876+
derefSchema.getMaximum() != null ||
3877+
derefSchema.getMaximum() != null ||
3878+
derefSchema.getMinItems() != null ||
3879+
derefSchema.getTitle() != null
3880+
)) {
3881+
p = ModelUtils.getReferencedSchema(openAPI, p);
3882+
}
3883+
}
3884+
38643885
// check if it's allOf (only 1 sub schema) with or without default/nullable/etc set in the top level
3865-
if (ModelUtils.isAllOf(p) && p.getAllOf().size() == 1) {
3886+
if (ModelUtils.isAllOfWithSingleItem(p)) {
38663887
if (p.getAllOf().get(0) instanceof Schema) {
38673888
original = p;
38683889
p = (Schema) p.getAllOf().get(0);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,18 @@ public static boolean isAllOf(Schema schema) {
20312031
return false;
20322032
}
20332033

2034+
2035+
/**
2036+
* Returns true if the schema contains allOf with a single item but
2037+
* no properties/oneOf/anyOf defined
2038+
*
2039+
* @param schema the schema
2040+
* @return true if the schema contains allOf but no properties/oneOf/anyOf defined.
2041+
*/
2042+
public static boolean isAllOfWithSingleItem(Schema schema) {
2043+
return (isAllOf(schema) && schema.getAllOf().size() == 1);
2044+
}
2045+
20342046
/**
20352047
* Returns true if the schema contains allOf and may or may not have
20362048
* properties/oneOf/anyOf defined.

modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,6 +2610,10 @@ components:
26102610
minItems: 1
26112611
items:
26122612
$ref: '#/components/schemas/Scalar'
2613+
AllOfRefToString:
2614+
allOf:
2615+
- $ref: '#/components/schemas/OuterString'
2616+
description: testing allOf with a ref to string
26132617
NewPet:
26142618
type: object
26152619
required:
@@ -2620,6 +2624,12 @@ components:
26202624
type: integer
26212625
format: int64
26222626
x-is-unique: true
2627+
category_ref_to_inline_allof_string:
2628+
$ref: '#/components/schemas/AllOfRefToString'
2629+
category_inline_allof_string:
2630+
allOf:
2631+
- $ref: '#/components/schemas/OuterString'
2632+
description: testing allOf with a ref to string
26232633
category_inline_allof:
26242634
allOf:
26252635
- type: object

samples/client/petstore/java/okhttp-gson/api/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,12 +2689,22 @@ components:
26892689
$ref: '#/components/schemas/Scalar'
26902690
minItems: 1
26912691
type: array
2692+
AllOfRefToString:
2693+
allOf:
2694+
- $ref: '#/components/schemas/OuterString'
2695+
description: testing allOf with a ref to string
26922696
NewPet:
26932697
properties:
26942698
id:
26952699
format: int64
26962700
type: integer
26972701
x-is-unique: true
2702+
category_ref_to_inline_allof_string:
2703+
$ref: '#/components/schemas/AllOfRefToString'
2704+
category_inline_allof_string:
2705+
allOf:
2706+
- $ref: '#/components/schemas/OuterString'
2707+
description: testing allOf with a ref to string
26982708
category_inline_allof:
26992709
$ref: '#/components/schemas/NewPet_category_inline_allof'
27002710
category_allOf_ref:

samples/client/petstore/java/okhttp-gson/docs/NewPet.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
| Name | Type | Description | Notes |
99
|------------ | ------------- | ------------- | -------------|
1010
|**id** | **Long** | | [optional] |
11+
|**categoryRefToInlineAllofString** | **String** | testing allOf with a ref to string | [optional] |
12+
|**categoryInlineAllofString** | **String** | testing allOf with a ref to string | [optional] |
1113
|**categoryInlineAllof** | [**NewPetCategoryInlineAllof**](NewPetCategoryInlineAllof.md) | | [optional] |
1214
|**categoryAllOfRef** | [**Category**](Category.md) | | [optional] |
1315
|**categoryAllOfRefDescription** | [**Category**](Category.md) | Adding description to property using allOf | [optional] |

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/NewPet.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public class NewPet {
6060
@javax.annotation.Nullable
6161
private Long id;
6262

63+
public static final String SERIALIZED_NAME_CATEGORY_REF_TO_INLINE_ALLOF_STRING = "category_ref_to_inline_allof_string";
64+
@SerializedName(SERIALIZED_NAME_CATEGORY_REF_TO_INLINE_ALLOF_STRING)
65+
@javax.annotation.Nullable
66+
private String categoryRefToInlineAllofString;
67+
68+
public static final String SERIALIZED_NAME_CATEGORY_INLINE_ALLOF_STRING = "category_inline_allof_string";
69+
@SerializedName(SERIALIZED_NAME_CATEGORY_INLINE_ALLOF_STRING)
70+
@javax.annotation.Nullable
71+
private String categoryInlineAllofString;
72+
6373
public static final String SERIALIZED_NAME_CATEGORY_INLINE_ALLOF = "category_inline_allof";
6474
@SerializedName(SERIALIZED_NAME_CATEGORY_INLINE_ALLOF)
6575
@javax.annotation.Nullable
@@ -183,6 +193,44 @@ public void setId(@javax.annotation.Nullable Long id) {
183193
}
184194

185195

196+
public NewPet categoryRefToInlineAllofString(@javax.annotation.Nullable String categoryRefToInlineAllofString) {
197+
this.categoryRefToInlineAllofString = categoryRefToInlineAllofString;
198+
return this;
199+
}
200+
201+
/**
202+
* testing allOf with a ref to string
203+
* @return categoryRefToInlineAllofString
204+
*/
205+
@javax.annotation.Nullable
206+
public String getCategoryRefToInlineAllofString() {
207+
return categoryRefToInlineAllofString;
208+
}
209+
210+
public void setCategoryRefToInlineAllofString(@javax.annotation.Nullable String categoryRefToInlineAllofString) {
211+
this.categoryRefToInlineAllofString = categoryRefToInlineAllofString;
212+
}
213+
214+
215+
public NewPet categoryInlineAllofString(@javax.annotation.Nullable String categoryInlineAllofString) {
216+
this.categoryInlineAllofString = categoryInlineAllofString;
217+
return this;
218+
}
219+
220+
/**
221+
* testing allOf with a ref to string
222+
* @return categoryInlineAllofString
223+
*/
224+
@javax.annotation.Nullable
225+
public String getCategoryInlineAllofString() {
226+
return categoryInlineAllofString;
227+
}
228+
229+
public void setCategoryInlineAllofString(@javax.annotation.Nullable String categoryInlineAllofString) {
230+
this.categoryInlineAllofString = categoryInlineAllofString;
231+
}
232+
233+
186234
public NewPet categoryInlineAllof(@javax.annotation.Nullable NewPetCategoryInlineAllof categoryInlineAllof) {
187235
this.categoryInlineAllof = categoryInlineAllof;
188236
return this;
@@ -398,6 +446,8 @@ public boolean equals(Object o) {
398446
}
399447
NewPet newPet = (NewPet) o;
400448
return Objects.equals(this.id, newPet.id) &&
449+
Objects.equals(this.categoryRefToInlineAllofString, newPet.categoryRefToInlineAllofString) &&
450+
Objects.equals(this.categoryInlineAllofString, newPet.categoryInlineAllofString) &&
401451
Objects.equals(this.categoryInlineAllof, newPet.categoryInlineAllof) &&
402452
Objects.equals(this.categoryAllOfRef, newPet.categoryAllOfRef) &&
403453
Objects.equals(this.categoryAllOfRefDescription, newPet.categoryAllOfRefDescription) &&
@@ -411,14 +461,16 @@ public boolean equals(Object o) {
411461

412462
@Override
413463
public int hashCode() {
414-
return Objects.hash(id, categoryInlineAllof, categoryAllOfRef, categoryAllOfRefDescription, categoryAllOfRefDescriptionReadonly, name, photoUrls, tags, status, additionalProperties);
464+
return Objects.hash(id, categoryRefToInlineAllofString, categoryInlineAllofString, categoryInlineAllof, categoryAllOfRef, categoryAllOfRefDescription, categoryAllOfRefDescriptionReadonly, name, photoUrls, tags, status, additionalProperties);
415465
}
416466

417467
@Override
418468
public String toString() {
419469
StringBuilder sb = new StringBuilder();
420470
sb.append("class NewPet {\n");
421471
sb.append(" id: ").append(toIndentedString(id)).append("\n");
472+
sb.append(" categoryRefToInlineAllofString: ").append(toIndentedString(categoryRefToInlineAllofString)).append("\n");
473+
sb.append(" categoryInlineAllofString: ").append(toIndentedString(categoryInlineAllofString)).append("\n");
422474
sb.append(" categoryInlineAllof: ").append(toIndentedString(categoryInlineAllof)).append("\n");
423475
sb.append(" categoryAllOfRef: ").append(toIndentedString(categoryAllOfRef)).append("\n");
424476
sb.append(" categoryAllOfRefDescription: ").append(toIndentedString(categoryAllOfRefDescription)).append("\n");
@@ -451,6 +503,8 @@ private String toIndentedString(Object o) {
451503
// a set of all properties/fields (JSON key names)
452504
openapiFields = new HashSet<String>();
453505
openapiFields.add("id");
506+
openapiFields.add("category_ref_to_inline_allof_string");
507+
openapiFields.add("category_inline_allof_string");
454508
openapiFields.add("category_inline_allof");
455509
openapiFields.add("category_allOf_ref");
456510
openapiFields.add("category_allOf_ref_description");
@@ -486,6 +540,12 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
486540
}
487541
}
488542
JsonObject jsonObj = jsonElement.getAsJsonObject();
543+
if ((jsonObj.get("category_ref_to_inline_allof_string") != null && !jsonObj.get("category_ref_to_inline_allof_string").isJsonNull()) && !jsonObj.get("category_ref_to_inline_allof_string").isJsonPrimitive()) {
544+
throw new IllegalArgumentException(String.format("Expected the field `category_ref_to_inline_allof_string` to be a primitive type in the JSON string but got `%s`", jsonObj.get("category_ref_to_inline_allof_string").toString()));
545+
}
546+
if ((jsonObj.get("category_inline_allof_string") != null && !jsonObj.get("category_inline_allof_string").isJsonNull()) && !jsonObj.get("category_inline_allof_string").isJsonPrimitive()) {
547+
throw new IllegalArgumentException(String.format("Expected the field `category_inline_allof_string` to be a primitive type in the JSON string but got `%s`", jsonObj.get("category_inline_allof_string").toString()));
548+
}
489549
// validate the optional field `category_inline_allof`
490550
if (jsonObj.get("category_inline_allof") != null && !jsonObj.get("category_inline_allof").isJsonNull()) {
491551
NewPetCategoryInlineAllof.validateJsonElement(jsonObj.get("category_inline_allof"));

0 commit comments

Comments
 (0)