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: "isAlias" of CodegenModel #2758

Merged
merged 8 commits into from
Feb 19, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,8 @@ public CodegenModel fromModel(String name, Schema schema) {
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) {
Expand Down Expand Up @@ -4204,11 +4205,10 @@ Map<String, String> getAllAliases(Map<String, Schema> schemas) {

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);
}

Expand All @@ -4217,6 +4217,14 @@ Map<String, String> getAllAliases(Map<String, Schema> schemas) {
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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,28 @@ public void numberDoubleSchemaPropertyAndModelTest() {
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");
Expand Down
44 changes: 44 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/type_alias.yaml
Original file line number Diff line number Diff line change
@@ -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