Better isAlias detection (#16943)

* better isAlias check for allOf with single item

* better code format
This commit is contained in:
William Cheng
2023-10-31 17:49:00 +08:00
committed by GitHub
parent 6cd73eba2b
commit bfe6157d07
3 changed files with 695 additions and 626 deletions

View File

@@ -5987,21 +5987,31 @@ public class DefaultCodegen implements CodegenConfig {
for (Map.Entry<String, Schema> entry : schemas.entrySet()) {
Schema schema = entry.getValue();
if (isAliasOfSimpleTypes(schema)) {
String oasName = entry.getKey();
String schemaType = getPrimitiveType(schema);
aliases.put(oasName, schemaType);
if (schema.getAllOf() != null && schema.getAllOf().size() == 1) { // allOf with a single item
Schema unaliasSchema = unaliasSchema(schema);
unaliasSchema = ModelUtils.getReferencedSchema(this.openAPI, unaliasSchema);
aliases.put(entry.getKey() /* schema name, e.g. Pet */, getPrimitiveType(unaliasSchema));
} else {
aliases.put(entry.getKey() /* schema name, e.g. Pet */, getPrimitiveType(schema));
}
}
}
return aliases;
}
private static Boolean isAliasOfSimpleTypes(Schema schema) {
private Boolean isAliasOfSimpleTypes(Schema schema) {
if (schema == null) {
return false;
}
// allOf with a single item
if (schema.getAllOf() != null && schema.getAllOf().size() == 1
&& schema.getAllOf().get(0) instanceof Schema) {
schema = unaliasSchema((Schema) schema.getAllOf().get(0));
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
}
return (!ModelUtils.isObjectSchema(schema)
&& !ModelUtils.isArraySchema(schema)
&& !ModelUtils.isMapSchema(schema)

View File

@@ -0,0 +1,36 @@
openapi: 3.0.0
info:
title: Foo
version: 1.0.0
paths:
/foo:
get:
responses:
'200':
description: example
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Payment'
components:
schemas:
DateTime:
type: string
format: date-time
example: "2019-01-01T00:00:00"
# the following schema shouldn't be generated as model
AllOfDatetime:
allOf:
- $ref: '#/components/schemas/DateTime'
example: "2019-01-01 00:00:00"
Payment:
type: object
properties:
date:
$ref: '#/components/schemas/AllOfDatetime'
amount:
type: number
format: float
example: 625.0