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 array model of alias to array model missing inner type argument #4981

Merged
merged 4 commits into from
Mar 3, 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 @@ -1734,12 +1734,23 @@ public String getSchemaType(Schema schema) {
}

protected Schema<?> getSchemaItems(ArraySchema schema) {
if (schema.getItems() != null) {
return schema.getItems();
} else {
Schema<?> items = schema.getItems();
if (items == null) {
LOGGER.error("Undefined array inner type for `{}`. Default to String.", schema.getName());
return new StringSchema().description("TODO default missing array inner type to string");
items = new StringSchema().description("TODO default missing array inner type to string");
schema.setItems(items);
}
return items;
}

protected Schema<?> getSchemaAdditionalProperties(Schema schema) {
Schema<?> inner = ModelUtils.getAdditionalProperties(schema);
if (inner == null) {
LOGGER.error("`{}` (map property) does not have a proper inner type defined. Default to type:string", schema.getName());
inner = new StringSchema().description("TODO default missing map inner type to string");
schema.setAdditionalProperties(inner);
}
return inner;
}

/**
Expand Down Expand Up @@ -2534,9 +2545,6 @@ public CodegenProperty fromProperty(String name, Schema p) {
// default to string if inner item is undefined
ArraySchema arraySchema = (ArraySchema) p;
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getSchemaItems(arraySchema), importMapping);
if (arraySchema.getItems() == null) {
arraySchema.setItems(innerSchema);
}
} else if (ModelUtils.isMapSchema(p)) {
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getAdditionalProperties(p),
importMapping);
Expand Down Expand Up @@ -2616,9 +2624,6 @@ public CodegenProperty fromProperty(String name, Schema p) {
}
ArraySchema arraySchema = (ArraySchema) p;
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getSchemaItems(arraySchema), importMapping);
if (arraySchema.getItems() == null) {
arraySchema.setItems(innerSchema);
}
CodegenProperty cp = fromProperty(itemName, innerSchema);
updatePropertyForArray(property, cp);
} else if (ModelUtils.isMapSchema(p)) {
Expand Down Expand Up @@ -3499,9 +3504,6 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
if (ModelUtils.isArraySchema(parameterSchema)) { // for array parameter
final ArraySchema arraySchema = (ArraySchema) parameterSchema;
Schema inner = getSchemaItems(arraySchema);
if (arraySchema.getItems() == null) {
arraySchema.setItems(inner);
}

collectionFormat = getCollectionFormat(parameter);
// default to csv:
Expand Down Expand Up @@ -5098,9 +5100,6 @@ public List<CodegenParameter> fromRequestBodyToFormParameters(RequestBody body,
if (ModelUtils.isArraySchema(s)) {
final ArraySchema arraySchema = (ArraySchema) s;
Schema inner = getSchemaItems(arraySchema);
if (arraySchema.getItems() == null) {
arraySchema.setItems(inner);
}

codegenParameter = fromFormProperty(entry.getKey(), inner, imports);
CodegenProperty codegenProperty = fromProperty("inner", inner);
Expand Down Expand Up @@ -5300,9 +5299,6 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
} else if (ModelUtils.isArraySchema(schema)) {
final ArraySchema arraySchema = (ArraySchema) schema;
Schema inner = getSchemaItems(arraySchema);
if (arraySchema.getItems() == null) {
arraySchema.setItems(inner);
}
CodegenProperty codegenProperty = fromProperty("property", arraySchema);
imports.add(codegenProperty.baseType);
CodegenProperty innerCp = codegenProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,7 @@ public String getTypeDeclaration(Schema p) {
Schema<?> items = getSchemaItems((ArraySchema) p);
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);
}
Schema<?> inner = getSchemaAdditionalProperties(p);
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 @@ -378,13 +378,12 @@ public String toModelFilename(String name) {
@Override
public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) {
ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems();
return getSchemaType(p) + "<" + getTypeDeclaration(inner) + ">";
Schema<?> items = getSchemaItems((ArraySchema) p);
return getSchemaType(p) + "<" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, items)) + ">";
} else if (ModelUtils.isMapSchema(p)) {
Schema inner = ModelUtils.getAdditionalProperties(p);
Schema<?> inner = getSchemaAdditionalProperties(p);
String nullSafeSuffix = getNullSafeAdditionalProps() ? " | undefined" : "";
return "{ [key: string]: " + getTypeDeclaration(inner) + nullSafeSuffix + "; }";
return "{ [key: string]: " + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner)) + nullSafeSuffix + "; }";
} else if (ModelUtils.isFileSchema(p)) {
return "any";
} else if (ModelUtils.isBinarySchema(p)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public boolean isDataTypeFile(final String dataType) {

@Override
public String getTypeDeclaration(Schema p) {
if (ModelUtils.isBinarySchema(p)) {
if (ModelUtils.isFileSchema(p)) {
return "Blob";
} else if (ModelUtils.isBinarySchema(p)) {
return "Blob";
}
return super.getTypeDeclaration(p);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.openapitools.codegen.typescript.fetch;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.*;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.TypeScriptFetchClientCodegen;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -69,4 +71,35 @@ public void toVarName() {
Assert.assertEquals(codegen.toVarName("valid_var"), "valid_var");
}

@Test
public void getTypeDeclarationTest() {
Schema<?> childSchema = new ArraySchema().items(new StringSchema());

OpenAPI api = TestUtils.createOpenAPI();
api.getComponents().addSchemas("Child", childSchema);

TypeScriptFetchClientCodegen codegen = new TypeScriptFetchClientCodegen();
codegen.setOpenAPI(api);

// Cf. issue #4968: Array of Alias of Array
Schema<?> parentSchema = new ArraySchema().items(
new Schema().$ref("#/components/schemas/Child")
);

ModelUtils.setGenerateAliasAsModel(false);
Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "Array<Array<string>>");

ModelUtils.setGenerateAliasAsModel(true);
Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "Array<Child>");

// Same for Map
parentSchema = new MapSchema().additionalProperties(new Schema().$ref("#/components/schemas/Child"));

ModelUtils.setGenerateAliasAsModel(false);
Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "{ [key: string]: Array<string>; }");

ModelUtils.setGenerateAliasAsModel(true);
Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "{ [key: string]: Child; }");
}

}