Skip to content

Commit

Permalink
[Java][Go] Unalias schema in getTypeDeclaration when not generating m…
Browse files Browse the repository at this point in the history
…odel for alias (#4527)

* [Java][Go] Unalias schema in getTypeDeclaration when not generating model for alias

* Add tests

* Don't use isGenerateAliasAsModel
  • Loading branch information
zippolyte authored and wing328 committed Nov 21, 2019
1 parent eebad5c commit db77e07
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) {
ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems();
return "[]" + getTypeDeclaration(inner);
return "[]" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner));
} else if (ModelUtils.isMapSchema(p)) {
Schema inner = ModelUtils.getAdditionalProperties(p);
return getSchemaType(p) + "[string]" + getTypeDeclaration(inner);
return getSchemaType(p) + "[string]" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner));
}
//return super.getTypeDeclaration(p);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,15 +689,15 @@ public String toModelFilename(String name) {
public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) {
Schema<?> items = getSchemaItems((ArraySchema) p);
return getSchemaType(p) + "<" + getTypeDeclaration(items) + ">";
return getSchemaType(p) + "<" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, items)) + ">";
} else if (ModelUtils.isMapSchema(p)) {
Schema inner = ModelUtils.getAdditionalProperties(p);
if (inner == null) {
LOGGER.error("`{}` (map property) does not have a proper inner type defined. Default to type:string", p.getName());
inner = new StringSchema().description("TODO default missing map inner type to string");
p.setAdditionalProperties(inner);
}
return getSchemaType(p) + "<String, " + getTypeDeclaration(inner) + ">";
return getSchemaType(p) + "<String, " + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner)) + ">";
}
return super.getTypeDeclaration(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@

package org.openapitools.codegen.go;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.java.AbstractJavaCodegenTest;
import org.openapitools.codegen.languages.AbstractGoCodegen;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -54,6 +62,37 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
}

@Test
public void getTypeDeclarationTest() {
final AbstractGoCodegen codegen = new P_AbstractGoCodegen();

// Create an alias to an array schema
Schema<?> nestedArraySchema = new ArraySchema().items(new IntegerSchema().format("int32"));
codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("NestedArray", nestedArraySchema)));

// Create an array schema with item type set to the array alias
Schema<?> schema = new ArraySchema().items(new Schema().$ref("#/components/schemas/NestedArray"));

ModelUtils.setGenerateAliasAsModel(false);
String defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "[][]int32");

ModelUtils.setGenerateAliasAsModel(true);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "[]NestedArray");

// Create a map schema with additionalProperties type set to array alias
schema = new MapSchema().additionalProperties(new Schema().$ref("#/components/schemas/NestedArray"));

ModelUtils.setGenerateAliasAsModel(false);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "map[string][]int32");

ModelUtils.setGenerateAliasAsModel(true);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "map[string]NestedArray");
}

private static class P_AbstractGoCodegen extends AbstractGoCodegen {
@Override
public CodegenType getTag() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

package org.openapitools.codegen.java;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.*;

import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.AbstractJavaCodegen;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -420,6 +420,32 @@ public void getTypeDeclarationTest() {
Schema<?> schema = createObjectSchemaWithMinItems();
String defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "Object");

// Create an alias to an array schema
Schema<?> nestedArraySchema = new ArraySchema().items(new IntegerSchema().format("int32"));
codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("NestedArray", nestedArraySchema)));

// Create an array schema with item type set to the array alias
schema = new ArraySchema().items(new Schema().$ref("#/components/schemas/NestedArray"));

ModelUtils.setGenerateAliasAsModel(false);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<List<Integer>>");

ModelUtils.setGenerateAliasAsModel(true);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<NestedArray>");

// Create a map schema with additionalProperties type set to array alias
schema = new MapSchema().additionalProperties(new Schema().$ref("#/components/schemas/NestedArray"));

ModelUtils.setGenerateAliasAsModel(false);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "Map<String, List<Integer>>");

ModelUtils.setGenerateAliasAsModel(true);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "Map<String, NestedArray>");
}

@Test
Expand Down

0 comments on commit db77e07

Please sign in to comment.