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 2 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 @@ -1628,7 +1628,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 @@ -3590,11 +3591,10 @@ static 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 @@ -3603,6 +3603,15 @@ static Map<String, String> getAllAliases(Map<String, Schema> schemas) {
return aliases;
}

private static Boolean isAliasOfSimpleTypes(Schema schema) {
String schemaType = getPrimitiveType(schema);
return (schemaType != null
&& !schemaType.equals("object")
&& !schemaType.equals("array")
wing328 marked this conversation as resolved.
Show resolved Hide resolved
&& schema.getEnum() == null
&& !ModelUtils.isMapSchema(schema));
}

/**
* 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 @@ -570,6 +570,23 @@ public void testNullableProperty() {
Assert.assertTrue(property.isNullable);
}

@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 model = codegen.fromModel(
"MyParameterTextField",
openAPI.getComponents().getSchemas().get("MyParameterTextField")
);

Assert.assertTrue(model.isAlias);
Assert.assertEquals("string", model.dataType);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some of generators (e.g. Kotlin which supports Type Alias), it may expected that the dataType property has a specific model type for better type alias support.

ref #2574

}

private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
CodegenDiscriminator test = new CodegenDiscriminator();
test.setPropertyName("DollarUnderscoretype");
Expand Down
29 changes: 29 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,29 @@
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