Skip to content

Commit

Permalink
use map/array model class only if it is generated (OpenAPITools#17612)
Browse files Browse the repository at this point in the history
* fix

* tests

* generate samples

* refactor
  • Loading branch information
martin-mfg committed Jan 29, 2024
1 parent 5055eba commit 9afea50
Show file tree
Hide file tree
Showing 210 changed files with 10,550 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7358,7 +7358,20 @@ protected void addBodyModelSchema(CodegenParameter codegenParameter, String name
}

protected void updateRequestBodyForMap(CodegenParameter codegenParameter, Schema schema, String name, Set<String> imports, String bodyParameterName) {
if (StringUtils.isNotBlank(name) && !(ModelUtils.isFreeFormObject(schema) && !ModelUtils.shouldGenerateFreeFormObjectModel(name, this))) {
boolean useModel = true;
if (StringUtils.isBlank(name)) {
useModel = false;
} else {
if (ModelUtils.isFreeFormObject(schema)) {
useModel = ModelUtils.shouldGenerateFreeFormObjectModel(name, this);
} else if (ModelUtils.isMapSchema(schema)) {
useModel = ModelUtils.shouldGenerateMapModel(schema);
} else if (ModelUtils.isArraySchema(schema)) {
useModel = ModelUtils.shouldGenerateArrayModel(schema);
}
}

if (useModel) {
this.addBodyModelSchema(codegenParameter, name, schema, imports, bodyParameterName, true);
} else {
Schema inner = ModelUtils.getAdditionalProperties(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,13 @@ void generateModels(List<File> files, List<ModelMap> allModels, List<String> unu
continue;
}
} else if (ModelUtils.isMapSchema(schema)) { // check to see if it's a "map" model
// A composed schema (allOf, oneOf, anyOf) is considered a Map schema if the additionalproperties attribute is set
// for that composed schema. However, in the case of a composed schema, the properties are defined or referenced
// in the inner schemas, and the outer schema does not have properties.
if (!ModelUtils.isGenerateAliasAsModel(schema) && !ModelUtils.isComposedSchema(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
if (!ModelUtils.shouldGenerateMapModel(schema)) {
// schema without property, i.e. alias to map
LOGGER.info("Model {} not generated since it's an alias to map (without property) and `generateAliasAsModel` is set to false (default)", name);
continue;
}
} else if (ModelUtils.isArraySchema(schema)) { // check to see if it's an "array" model
if (!ModelUtils.isGenerateAliasAsModel(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
if (!ModelUtils.shouldGenerateArrayModel(schema)) {
// schema without property, i.e. alias to array
LOGGER.info("Model {} not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)", name);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,17 @@ public static boolean shouldGenerateFreeFormObjectModel(String name, CodegenConf
return unaliasedSchema.get$ref() != null;
}

public static boolean shouldGenerateMapModel(Schema schema) {
// A composed schema (allOf, oneOf, anyOf) is considered a Map schema if the additionalproperties attribute is set
// for that composed schema. However, in the case of a composed schema, the properties are defined or referenced
// in the inner schemas, and the outer schema does not have properties.
return ModelUtils.isGenerateAliasAsModel(schema) || ModelUtils.isComposedSchema(schema) || !(schema.getProperties() == null || schema.getProperties().isEmpty());
}

public static boolean shouldGenerateArrayModel(Schema schema) {
return ModelUtils.isGenerateAliasAsModel(schema) || !(schema.getProperties() == null || schema.getProperties().isEmpty());
}

/**
* If a Schema contains a reference to another Schema with '$ref', returns the referenced Schema if it is found or the actual Schema in the other cases.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1811,6 +1828,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1942,6 +1959,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1834,6 +1851,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1794,6 +1811,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1912,6 +1929,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1774,6 +1791,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1785,6 +1802,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1793,6 +1810,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,23 @@ paths:
$ref: '#/components/schemas/FreeFormObject'
description: request body
required: true
/fake/stringMap-reference:
post:
tags:
- fake
summary: test referenced string map
description: ''
operationId: testStringMapReference
responses:
'200':
description: successful operation
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MapOfString'
description: request body
required: true
/fake/inline-additionalProperties:
post:
tags:
Expand Down Expand Up @@ -1759,6 +1776,11 @@ components:
type: object
description: A schema consisting only of additional properties
additionalProperties: true
MapOfString:
type: object
description: A schema consisting only of additional properties of type string
additionalProperties:
type: string
OuterEnum:
nullable: true
type: string
Expand Down
Loading

0 comments on commit 9afea50

Please sign in to comment.