Skip to content

Commit

Permalink
Better handling of parameters in inline model resolver (#19460)
Browse files Browse the repository at this point in the history
* add tests for parameter ref of oneOf

* update samples

* better handlding of parameters in inline model resolver
  • Loading branch information
wing328 authored Aug 28, 2024
1 parent d6780e7 commit 9579122
Show file tree
Hide file tree
Showing 30 changed files with 3,082 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,15 @@ private void flattenPaths() {
}
}

// flatten path-level parameters
flattenParameters(pathname, path.getParameters(), null);

// flatten parameters for each operation
for (Map.Entry<HttpMethod, Operation> operationEntry : toFlatten) {
Operation operation = operationEntry.getValue();
String inlineSchemaName = this.getInlineSchemaName(operationEntry.getKey(), pathname);
flattenRequestBody(inlineSchemaName, operation);
flattenParameters(inlineSchemaName, operation);
flattenParameters(inlineSchemaName, operation.getParameters(), operation.getOperationId());
flattenResponses(inlineSchemaName, operation);
}
}
Expand Down Expand Up @@ -509,15 +513,20 @@ private void flattenRequestBody(String modelName, Operation operation) {
* Flatten inline models in parameters
*
* @param modelName model name
* @param operation target operation
* @param parameters list of parameters
* @param operationId operation Id (optional)
*/
private void flattenParameters(String modelName, Operation operation) {
List<Parameter> parameters = operation.getParameters();
private void flattenParameters(String modelName, List<Parameter> parameters, String operationId) {
//List<Parameter> parameters = operation.getParameters();
if (parameters == null) {
return;
}

for (Parameter parameter : parameters) {
if (StringUtils.isNotEmpty(parameter.get$ref())) {
parameter = ModelUtils.getReferencedParameter(openAPI, parameter);
}

if (parameter.getSchema() == null) {
continue;
}
Expand All @@ -528,7 +537,7 @@ private void flattenParameters(String modelName, Operation operation) {
continue;
}
String schemaName = resolveModelName(parameterSchema.getTitle(),
(operation.getOperationId() == null ? modelName : operation.getOperationId()) + "_" + parameter.getName() + "_parameter");
(operationId == null ? modelName : operationId) + "_" + parameter.getName() + "_parameter");
// Recursively gather/make inline models within this schema if any
gatherInlineModels(parameterSchema, schemaName);
if (isModelNeeded(parameterSchema)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,7 @@ private void normalizeParameters(List<Parameter> parameters) {
parameter = ModelUtils.getReferencedParameter(openAPI, parameter);
}

if (parameter.getSchema() == null) {
continue;
} else {
if (parameter.getSchema() != null) {
Schema newSchema = normalizeSchema(parameter.getSchema(), new HashSet<>());
parameter.setSchema(newSchema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
propertyHash.put(property.name, property);
final CodegenProperty parentVar = property.clone();
parentVar.isInherited = true;
LOGGER.info("adding parent variable {} to {}", property.name, codegenModel.name);
LOGGER.debug("adding parent variable {} to {}", property.name, codegenModel.name);
codegenModel.parentVars.add(parentVar);
Set<String> imports = parentVar.getImports(true, this.importBaseType, generatorMetadata.getFeatureSet()).stream().filter(Objects::nonNull).collect(Collectors.toSet());
for (String imp : imports) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ paths:
post:
tags:
- fake
summary: fake uploads an image with ref request bodies
summary: fake reference parameter
description: ''
operationId: fake-upload-ref-request-bodies
parameters:
Expand All @@ -1364,7 +1364,18 @@ paths:
- 'read:pets'
requestBody:
$ref: '#/components/requestBodies/upload_body'

'/fake/pet/{petId}/reference/parameter':
post:
tags:
- fake
summary: fake reference parameter
description: ''
operationId: fake-ref-parameter
parameters:
- $ref: '#/components/parameters/pet_id'
responses:
'200':
description: successful operation
/values:
get:
tags:
Expand Down Expand Up @@ -1423,6 +1434,16 @@ servers:
- url: https://127.0.0.1/no_variable
description: The local server without variables
components:
parameters:
pet_id:
name: petId
in: path
description: to test oneOf in parameter $ref
required: true
schema:
oneOf:
- type: string
- type: integer
requestBodies:
upload_body:
content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,28 @@ paths:
$ref: '#/components/responses/ref'
parameters:
- $ref: '#/components/parameters/ref_to_uuid'
/ref/ref_to_path_level_parameter_oneof:
get:
description: to test $ref to path level parameters
operationId: ref_to_ref_parameter_oneof
tags:
- fake
responses:
'200':
description: Successful Response
parameters:
- $ref: '#/components/parameters/ref_to_oneof'
/ref/ref_to_operation_level_parameter_oneof:
get:
description: to test $ref to operation level parameters
operationId: ref_to_ref_parameter_anyof
tags:
- fake
responses:
'200':
description: Successful Response
parameters:
- $ref: '#/components/parameters/ref_to_anyof'
"/fake/api/changeowner":
post:
summary: op1
Expand Down Expand Up @@ -720,6 +742,26 @@ components:
type: string
format: uuid
example: 61864654-6e6b-4152-a62f-795fdd606bc2
ref_to_oneof:
description: to test ref to parameter (oneof)
name: ref_to_oneof
in: header
required: true
schema:
oneOf:
- type: string
- type: integer
ref_to_anyof:
description: to test ref to parameter (anyof)
name: ref_to_anyof
in: header
required: true
schema:
oneOf:
- type: string
- type: array
items:
type: string
requestBodies:
UserArray:
content:
Expand Down Expand Up @@ -1023,4 +1065,4 @@ components:
allOf:
- $ref: '#/components/schemas/SimpleModelWithArrayProperty'
myObject:
type: object
type: object
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ docs/OneOfStringOrInt.md
docs/Order.md
docs/Pet.md
docs/PetApi.md
docs/RefRefToPathLevelParameterOneofRefToOneofParameter.md
docs/RefToRefParameterAnyofRefToAnyofParameter.md
docs/SimpleModelWithArrayProperty.md
docs/StoreApi.md
docs/StringOrInt.md
Expand Down Expand Up @@ -78,6 +80,8 @@ src/main/java/org/openapitools/client/model/ModelApiResponse.java
src/main/java/org/openapitools/client/model/OneOfStringOrInt.java
src/main/java/org/openapitools/client/model/Order.java
src/main/java/org/openapitools/client/model/Pet.java
src/main/java/org/openapitools/client/model/RefRefToPathLevelParameterOneofRefToOneofParameter.java
src/main/java/org/openapitools/client/model/RefToRefParameterAnyofRefToAnyofParameter.java
src/main/java/org/openapitools/client/model/SimpleModelWithArrayProperty.java
src/main/java/org/openapitools/client/model/StringOrInt.java
src/main/java/org/openapitools/client/model/Tag.java
Expand Down
4 changes: 4 additions & 0 deletions samples/client/petstore/java/okhttp-gson-3.1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ Class | Method | HTTP request | Description
*FakeApi* | [**op2**](docs/FakeApi.md#op2) | **POST** /fake/api/changename | op2
*FakeApi* | [**op3**](docs/FakeApi.md#op3) | **POST** /fake/api/query/enum | op3
*FakeApi* | [**refToRefParameter**](docs/FakeApi.md#refToRefParameter) | **GET** /ref/ref_to_parameter |
*FakeApi* | [**refToRefParameterAnyof**](docs/FakeApi.md#refToRefParameterAnyof) | **GET** /ref/ref_to_operation_level_parameter_oneof |
*FakeApi* | [**refToRefParameterOneof**](docs/FakeApi.md#refToRefParameterOneof) | **GET** /ref/ref_to_path_level_parameter_oneof |
*FakeApi* | [**responseNoRef**](docs/FakeApi.md#responseNoRef) | **GET** /no_ref |
*FakeApi* | [**responseRefToNoRef**](docs/FakeApi.md#responseRefToNoRef) | **GET** /ref/no_ref |
*FakeApi* | [**responseRefToRef**](docs/FakeApi.md#responseRefToRef) | **GET** /ref/ref |
Expand Down Expand Up @@ -162,6 +164,8 @@ Class | Method | HTTP request | Description
- [OneOfStringOrInt](docs/OneOfStringOrInt.md)
- [Order](docs/Order.md)
- [Pet](docs/Pet.md)
- [RefRefToPathLevelParameterOneofRefToOneofParameter](docs/RefRefToPathLevelParameterOneofRefToOneofParameter.md)
- [RefToRefParameterAnyofRefToAnyofParameter](docs/RefToRefParameterAnyofRefToAnyofParameter.md)
- [SimpleModelWithArrayProperty](docs/SimpleModelWithArrayProperty.md)
- [StringOrInt](docs/StringOrInt.md)
- [Tag](docs/Tag.md)
Expand Down
55 changes: 55 additions & 0 deletions samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,34 @@ paths:
- text/plain
parameters:
- $ref: '#/components/parameters/ref_to_uuid'
/ref/ref_to_path_level_parameter_oneof:
get:
description: to test $ref to path level parameters
operationId: ref_to_ref_parameter_oneof
parameters:
- $ref: '#/components/parameters/ref_to_oneof'
responses:
"200":
description: Successful Response
tags:
- fake
x-accepts:
- application/json
parameters:
- $ref: '#/components/parameters/ref_to_oneof'
/ref/ref_to_operation_level_parameter_oneof:
get:
description: to test $ref to operation level parameters
operationId: ref_to_ref_parameter_anyof
parameters:
- $ref: '#/components/parameters/ref_to_anyof'
responses:
"200":
description: Successful Response
tags:
- fake
x-accepts:
- application/json
/fake/api/changeowner:
post:
operationId: op1
Expand Down Expand Up @@ -805,6 +833,24 @@ components:
format: uuid
type: string
style: simple
ref_to_oneof:
description: to test ref to parameter (oneof)
explode: false
in: header
name: ref_to_oneof
required: true
schema:
$ref: '#/components/schemas/_ref_ref_to_path_level_parameter_oneof_ref_to_oneof_parameter'
style: simple
ref_to_anyof:
description: to test ref to parameter (anyof)
explode: false
in: header
name: ref_to_anyof
required: true
schema:
$ref: '#/components/schemas/ref_to_ref_parameter_anyof_ref_to_anyof_parameter'
style: simple
requestBodies:
UserArray:
content:
Expand Down Expand Up @@ -1134,6 +1180,15 @@ components:
description: file to upload
format: binary
type: string
_ref_ref_to_path_level_parameter_oneof_ref_to_oneof_parameter:
oneOf:
- type: string
- type: integer
ref_to_ref_parameter_anyof_ref_to_anyof_parameter:
oneOf:
- type: string
- items:
type: string
securitySchemes:
petstore_auth:
flows:
Expand Down
Loading

0 comments on commit 9579122

Please sign in to comment.