Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix allOf with a single item in inline model resolver #17683

Merged
merged 4 commits into from
Jan 23, 2024
Merged
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
2 changes: 1 addition & 1 deletion bin/utils/test_file_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/ClientTest.java"
sha256: db505f7801fef62c13a08a8e9ca1fc4c5c947ab46b46f12943139d353feacf17
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java"
sha256: 6db714e9744c150c8982c3cb18e4f37a9c1ecd8f72f6d58943986e781ab4a344
sha256: 7b9514ac0b3730685590d6ef273b2c2a1fb72d968529c2423a139ee9b0b92a65
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/api/PetApiTest.java"
sha256: 0d64cdc11809a7b5b952ccdad2bd91bd0045b3894d6fabf3e368fa0be12b8217
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/PetTest.java"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -851,13 +851,21 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
} else {
LOGGER.debug("Schema not yet handled in model resolver: {}", inner);
}
} else if (ModelUtils.isComposedSchema(property)) { // oneOf, anyOf, etc
String propertyModelName = resolveModelName(property.getTitle(), path + "_" + key);
gatherInlineModels(property, propertyModelName);
propertyModelName = addSchemas(propertyModelName, property);
Schema schema = new Schema().$ref(propertyModelName);
schema.setRequired(property.getRequired());
propsToUpdate.put(key, schema);
} else if (ModelUtils.isComposedSchema(property)) { // oneOf, anyOf, allOf etc
if (property.getAllOf() != null && property.getAllOf().size() == 1 // allOf with a single item
&& (property.getOneOf() == null || property.getOneOf().isEmpty()) // not oneOf
&& (property.getAnyOf() == null || property.getAnyOf().isEmpty()) // not anyOf
&& (property.getProperties() == null || property.getProperties().isEmpty())) { // no property
// don't do anything if it's allOf with a single item
LOGGER.debug("allOf with a single item (which can be handled by default codegen) skipped by inline model resolver: {}", property);
} else {
String propertyModelName = resolveModelName(property.getTitle(), path + "_" + key);
gatherInlineModels(property, propertyModelName);
propertyModelName = addSchemas(propertyModelName, property);
Schema schema = new Schema().$ref(propertyModelName);
schema.setRequired(property.getRequired());
propsToUpdate.put(key, schema);
}
} else {
LOGGER.debug("Schema not yet handled in model resolver: {}", property);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2544,3 +2544,41 @@ components:
oneOf:
- $ref: '#/components/schemas/Pet'
- $ref: '#/components/schemas/Order'
PetUsingAllOf:
type: object
required:
- name
- photoUrls
properties:
id:
type: integer
format: int64
x-is-unique: true
category:
allOf:
- $ref: '#/components/schemas/Category'
name:
type: string
example: doggie
photoUrls:
type: array
xml:
name: photoUrl
wrapped: true
items:
type: string
tags:
type: array
xml:
name: tag
wrapped: true
items:
allOf:
- $ref: '#/components/schemas/Tag'
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ docs/OuterEnumIntegerDefaultValue.md
docs/ParentPet.md
docs/Pet.md
docs/PetApi.md
docs/PetUsingAllOf.md
docs/PetWithRequiredTags.md
docs/Pig.md
docs/PropertyNameCollision.md
Expand Down Expand Up @@ -210,6 +211,7 @@ src/main/java/org/openapitools/client/model/OuterEnumInteger.java
src/main/java/org/openapitools/client/model/OuterEnumIntegerDefaultValue.java
src/main/java/org/openapitools/client/model/ParentPet.java
src/main/java/org/openapitools/client/model/Pet.java
src/main/java/org/openapitools/client/model/PetUsingAllOf.java
src/main/java/org/openapitools/client/model/PetWithRequiredTags.java
src/main/java/org/openapitools/client/model/Pig.java
src/main/java/org/openapitools/client/model/PropertyNameCollision.java
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/java/okhttp-gson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ Class | Method | HTTP request | Description
- [OuterEnumIntegerDefaultValue](docs/OuterEnumIntegerDefaultValue.md)
- [ParentPet](docs/ParentPet.md)
- [Pet](docs/Pet.md)
- [PetUsingAllOf](docs/PetUsingAllOf.md)
- [PetWithRequiredTags](docs/PetWithRequiredTags.md)
- [Pig](docs/Pig.md)
- [PropertyNameCollision](docs/PropertyNameCollision.md)
Expand Down
38 changes: 38 additions & 0 deletions samples/client/petstore/java/okhttp-gson/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2527,6 +2527,44 @@ components:
attributes:
$ref: '#/components/schemas/AllOfModelArrayAnyOf_allOf_attributes'
type: object
PetUsingAllOf:
properties:
id:
format: int64
type: integer
x-is-unique: true
category:
allOf:
- $ref: '#/components/schemas/Category'
name:
example: doggie
type: string
photoUrls:
items:
type: string
type: array
xml:
name: photoUrl
wrapped: true
tags:
items:
allOf:
- $ref: '#/components/schemas/Tag'
type: array
xml:
name: tag
wrapped: true
status:
description: pet status in the store
enum:
- available
- pending
- sold
type: string
required:
- name
- photoUrls
type: object
_foo_get_default_response:
example:
string:
Expand Down
28 changes: 28 additions & 0 deletions samples/client/petstore/java/okhttp-gson/docs/PetUsingAllOf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


# PetUsingAllOf


## Properties

| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**id** | **Long** | | [optional] |
|**category** | [**Category**](Category.md) | | [optional] |
|**name** | **String** | | |
|**photoUrls** | **List&lt;String&gt;** | | |
|**tags** | [**List&lt;Tag&gt;**](Tag.md) | | [optional] |
|**status** | [**StatusEnum**](#StatusEnum) | pet status in the store | [optional] |



## Enum: StatusEnum

| Name | Value |
|---- | -----|
| AVAILABLE | &quot;available&quot; |
| PENDING | &quot;pending&quot; |
| SOLD | &quot;sold&quot; |



Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.OuterComposite.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ParentPet.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Pet.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.PetUsingAllOf.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.PetWithRequiredTags.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Pig.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.PropertyNameCollision.CustomTypeAdapterFactory());
Expand Down
Loading
Loading