[Typescript] Fix array model of alias to array (#6888)

* Add failing test

* Add model unaliasing

* Regenerate samples

Not really related to this bug fix, but probably necessary for ci
checks.
This commit is contained in:
Bodo Graumann 2020-07-16 16:31:41 +02:00 committed by GitHub
parent fce74884f8
commit 7cb8766a5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 7 deletions

View File

@ -839,10 +839,10 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo
Schema inner; Schema inner;
if (ModelUtils.isArraySchema(p)) { if (ModelUtils.isArraySchema(p)) {
inner = ((ArraySchema) p).getItems(); inner = ((ArraySchema) p).getItems();
return this.getSchemaType(p) + "<" + this.getTypeDeclaration(inner) + ">"; return this.getSchemaType(p) + "<" + this.getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner)) + ">";
} else if (ModelUtils.isMapSchema(p)) { } else if (ModelUtils.isMapSchema(p)) {
inner = (Schema) p.getAdditionalProperties(); inner = (Schema) p.getAdditionalProperties();
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }"; return "{ [key: string]: " + this.getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner)) + "; }";
} else if (ModelUtils.isFileSchema(p)) { } else if (ModelUtils.isFileSchema(p)) {
return "HttpFile"; return "HttpFile";
} else if (ModelUtils.isBinarySchema(p)) { } else if (ModelUtils.isBinarySchema(p)) {

View File

@ -0,0 +1,44 @@
package org.openapitools.codegen.typescript.fetch;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.*;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.TypeScriptClientCodegen;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TypeScriptClientCodegenTest {
@Test
public void getTypeDeclarationTest() {
Schema<?> childSchema = new ArraySchema().items(new StringSchema());
OpenAPI api = TestUtils.createOpenAPI();
api.getComponents().addSchemas("Child", childSchema);
TypeScriptClientCodegen codegen = new TypeScriptClientCodegen();
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; }");
}
}

View File

@ -73,7 +73,7 @@ export class RequestContext {
* *
*/ */
public setUrl(url: string) { public setUrl(url: string) {
this.url = new URLParse(url, true); this.url = new URLParse(url, true);
} }
/** /**

View File

@ -93,7 +93,7 @@ export class RequestContext {
* *
*/ */
public setUrl(url: string) { public setUrl(url: string) {
this.url = new URLParse(url, true); this.url = new URLParse(url, true);
} }
/** /**

View File

@ -73,7 +73,7 @@ export class RequestContext {
* *
*/ */
public setUrl(url: string) { public setUrl(url: string) {
this.url = new URLParse(url, true); this.url = new URLParse(url, true);
} }
/** /**

View File

@ -68,7 +68,7 @@ export class RequestContext {
* *
*/ */
public setUrl(url: string) { public setUrl(url: string) {
this.url = new URLParse(url, true); this.url = new URLParse(url, true);
} }
/** /**

View File

@ -73,7 +73,7 @@ export class RequestContext {
* *
*/ */
public setUrl(url: string) { public setUrl(url: string) {
this.url = new URLParse(url, true); this.url = new URLParse(url, true);
} }
/** /**