Fix: "isAlias" of CodegenModel (#2758)

* Add test case to reproduce the issue

https://github.com/OpenAPITools/openapi-generator/issues/2574

* Fix: an alias of "an alias of simple OAS type" has an incorrect property `isAlias: false`

* Use ModelUtils instead of referring the "type" value directly

* Delete an unnecessary condition

* Tweak: the order of conditions

* Fix wrong "isAlias" value on ComposedSchema
This commit is contained in:
Akihito Nakano 2020-02-19 15:54:54 +09:00 committed by GitHub
parent 7882c614b9
commit 37556c2d96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 5 deletions

View File

@ -2047,7 +2047,8 @@ public class DefaultCodegen implements CodegenConfig {
if (schema.getExtensions() != null && !schema.getExtensions().isEmpty()) {
m.getVendorExtensions().putAll(schema.getExtensions());
}
m.isAlias = typeAliases.containsKey(name);
m.isAlias = (typeAliases.containsKey(name)
|| isAliasOfSimpleTypes(schema)); // check if the unaliased schema is an alias of simple OAS types
m.discriminator = createDiscriminator(name, schema);
if (schema.getXml() != null) {
@ -4204,11 +4205,10 @@ public class DefaultCodegen implements CodegenConfig {
Map<String, String> aliases = new HashMap<>();
for (Map.Entry<String, Schema> entry : schemas.entrySet()) {
String oasName = entry.getKey();
Schema schema = entry.getValue();
String schemaType = getPrimitiveType(schema);
if (schemaType != null && !schemaType.equals("object") && !schemaType.equals("array")
&& schema.getEnum() == null && !ModelUtils.isMapSchema(schema)) {
if (isAliasOfSimpleTypes(schema)) {
String oasName = entry.getKey();
String schemaType = getPrimitiveType(schema);
aliases.put(oasName, schemaType);
}
@ -4217,6 +4217,14 @@ public class DefaultCodegen implements CodegenConfig {
return aliases;
}
private static Boolean isAliasOfSimpleTypes(Schema schema) {
return (!ModelUtils.isObjectSchema(schema)
&& !ModelUtils.isArraySchema(schema)
&& !ModelUtils.isMapSchema(schema)
&& !ModelUtils.isComposedSchema(schema)
&& schema.getEnum() == null);
}
/**
* Remove characters not suitable for variable or method name from the input and camelize it
*

View File

@ -950,6 +950,28 @@ public class DefaultCodegenTest {
Assert.assertTrue(cm.isDouble);
}
@Test
public void testAlias() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/type_alias.yaml");
new InlineModelResolver().flatten(openAPI);
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
CodegenModel typeAliasModel = codegen.fromModel(
"MyParameterTextField",
openAPI.getComponents().getSchemas().get("MyParameterTextField")
);
Assert.assertTrue(typeAliasModel.isAlias);
Assert.assertEquals("string", typeAliasModel.dataType);
CodegenModel composedModel = codegen.fromModel(
"ComposedModel",
openAPI.getComponents().getSchemas().get("ComposedModel")
);
Assert.assertFalse(composedModel.isAlias);
}
private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
CodegenDiscriminator test = new CodegenDiscriminator();
test.setPropertyName("DollarUnderscoretype");

View File

@ -0,0 +1,44 @@
openapi: "3.0.1"
info:
version: 0.0.1
title: broken API
paths:
/test:
put:
summary: No description
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MyParameter'
responses:
200:
description: "OK"
components:
schemas:
MyParameter:
type: object
properties:
text_field:
$ref: '#/components/schemas/MyParameterTextField'
MyParameterTextField:
$ref: '#/components/schemas/TypeAliasToString'
TypeAliasToString:
type: string
minLength: 1
maxLength: 50
BaseModel:
type: object
discriminator: className
required:
- className
properties:
className:
type: string
ComposedModel:
allOf:
- $ref: '#/components/schemas/BaseModel'
- type: object
properties:
testProperty:
type: string