Skip to content

Commit 7cb8766

Browse files
authored
[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.
1 parent fce7488 commit 7cb8766

File tree

7 files changed

+51
-7
lines changed

7 files changed

+51
-7
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,10 @@ public String getTypeDeclaration(Schema p) {
839839
Schema inner;
840840
if (ModelUtils.isArraySchema(p)) {
841841
inner = ((ArraySchema) p).getItems();
842-
return this.getSchemaType(p) + "<" + this.getTypeDeclaration(inner) + ">";
842+
return this.getSchemaType(p) + "<" + this.getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner)) + ">";
843843
} else if (ModelUtils.isMapSchema(p)) {
844844
inner = (Schema) p.getAdditionalProperties();
845-
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
845+
return "{ [key: string]: " + this.getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner)) + "; }";
846846
} else if (ModelUtils.isFileSchema(p)) {
847847
return "HttpFile";
848848
} else if (ModelUtils.isBinarySchema(p)) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.openapitools.codegen.typescript.fetch;
2+
3+
import io.swagger.v3.oas.models.OpenAPI;
4+
import io.swagger.v3.oas.models.media.*;
5+
import org.openapitools.codegen.TestUtils;
6+
import org.openapitools.codegen.languages.TypeScriptClientCodegen;
7+
import org.openapitools.codegen.utils.ModelUtils;
8+
import org.testng.Assert;
9+
import org.testng.annotations.Test;
10+
11+
12+
public class TypeScriptClientCodegenTest {
13+
@Test
14+
public void getTypeDeclarationTest() {
15+
Schema<?> childSchema = new ArraySchema().items(new StringSchema());
16+
17+
OpenAPI api = TestUtils.createOpenAPI();
18+
api.getComponents().addSchemas("Child", childSchema);
19+
20+
TypeScriptClientCodegen codegen = new TypeScriptClientCodegen();
21+
codegen.setOpenAPI(api);
22+
23+
// Cf. issue #4968: Array of Alias of Array
24+
Schema<?> parentSchema = new ArraySchema().items(
25+
new Schema().$ref("#/components/schemas/Child")
26+
);
27+
28+
ModelUtils.setGenerateAliasAsModel(false);
29+
Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "Array<Array<string>>");
30+
31+
ModelUtils.setGenerateAliasAsModel(true);
32+
Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "Array<Child>");
33+
34+
// Same for Map
35+
parentSchema = new MapSchema().additionalProperties(new Schema().$ref("#/components/schemas/Child"));
36+
37+
ModelUtils.setGenerateAliasAsModel(false);
38+
Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "{ [key: string]: Array<string>; }");
39+
40+
ModelUtils.setGenerateAliasAsModel(true);
41+
Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "{ [key: string]: Child; }");
42+
}
43+
44+
}

samples/openapi3/client/petstore/typescript/builds/default/http/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class RequestContext {
7373
*
7474
*/
7575
public setUrl(url: string) {
76-
this.url = new URLParse(url, true);
76+
this.url = new URLParse(url, true);
7777
}
7878

7979
/**

samples/openapi3/client/petstore/typescript/builds/deno/http/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class RequestContext {
9393
*
9494
*/
9595
public setUrl(url: string) {
96-
this.url = new URLParse(url, true);
96+
this.url = new URLParse(url, true);
9797
}
9898

9999
/**

samples/openapi3/client/petstore/typescript/builds/inversify/http/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class RequestContext {
7373
*
7474
*/
7575
public setUrl(url: string) {
76-
this.url = new URLParse(url, true);
76+
this.url = new URLParse(url, true);
7777
}
7878

7979
/**

samples/openapi3/client/petstore/typescript/builds/jquery/http/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class RequestContext {
6868
*
6969
*/
7070
public setUrl(url: string) {
71-
this.url = new URLParse(url, true);
71+
this.url = new URLParse(url, true);
7272
}
7373

7474
/**

samples/openapi3/client/petstore/typescript/builds/object_params/http/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class RequestContext {
7373
*
7474
*/
7575
public setUrl(url: string) {
76-
this.url = new URLParse(url, true);
76+
this.url = new URLParse(url, true);
7777
}
7878

7979
/**

0 commit comments

Comments
 (0)