Skip to content

Commit 4a6cc5a

Browse files
committed
Removed TypeScript utility type samples. Other reviews applied
1 parent 7826ba6 commit 4a6cc5a

File tree

22 files changed

+38
-1361
lines changed

22 files changed

+38
-1361
lines changed

bin/configs/typescript-fetch-utility-types-without-runtime.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

bin/configs/typescript-fetch-utility-types.yaml

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,13 @@ public AbstractTypeScriptClientCodegen() {
304304
// Typescript reserved words
305305
"abstract", "await", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield"));
306306

307-
defaultIncludes = new HashSet<>(Arrays.asList(
308-
//Utility types
307+
Set<String> utilityTypes = new HashSet<>(Arrays.asList(
309308
"Awaited","Partial","Required","Readonly","Record","Pick","Omit","Exclude","Extract","NonNullable",
310309
"Parameters","ConstructorParameters","ReturnType","InstanceType","NoInfer","ThisParameterType",
311-
"OmitThisParameter","ThisType","Uppercase","Lowercase","Capitalize","Uncapitalize"
312-
));
310+
"OmitThisParameter","ThisType","Uppercase","Lowercase","Capitalize","Uncapitalize")
311+
);
312+
defaultIncludes = new HashSet<>();
313+
defaultIncludes.addAll(utilityTypes);
313314

314315
languageSpecificPrimitives = new HashSet<>(Arrays.asList(
315316
"string",
@@ -330,12 +331,9 @@ public AbstractTypeScriptClientCodegen() {
330331
"Error",
331332
"Map",
332333
"object",
333-
"Set",
334-
//Utility types
335-
"Awaited","Partial","Required","Readonly","Record","Pick","Omit","Exclude","Extract","NonNullable",
336-
"Parameters","ConstructorParameters","ReturnType","InstanceType","NoInfer","ThisParameterType",
337-
"OmitThisParameter","ThisType","Uppercase","Lowercase","Capitalize","Uncapitalize"
334+
"Set"
338335
));
336+
languageSpecificPrimitives.addAll(utilityTypes);
339337

340338
languageGenericTypes = new HashSet<>(Collections.singletonList(
341339
"Array"
@@ -816,14 +814,8 @@ public String getSchemaType(Schema p) {
816814
return openAPIType;
817815
} else if (typeMapping.containsKey(openAPIType)) {
818816
type = typeMapping.get(openAPIType);
819-
String typeWithoutGeneric = null;
820-
int genericIndex = type.indexOf("<");
821-
if(genericIndex != -1) {
822-
typeWithoutGeneric = type.substring(0, genericIndex);
823-
}
824-
if (languageSpecificPrimitives.contains(type)) {
825-
return type;
826-
} else if(typeWithoutGeneric != null && languageSpecificPrimitives.contains(typeWithoutGeneric)) {
817+
String typeWithoutGeneric = typeWithoutGeneric(type);
818+
if (languageSpecificPrimitives.contains(typeWithoutGeneric)) {
827819
return type;
828820
}
829821
} else {
@@ -1177,9 +1169,12 @@ protected List<String> getTypesFromSchemas(List<Schema> schemas) {
11771169

11781170
@Override
11791171
protected boolean needToImport(String type) {
1172+
return super.needToImport(typeWithoutGeneric(type));
1173+
}
1174+
1175+
private String typeWithoutGeneric(String type) {
11801176
int genericIndex = type.indexOf("<");
1181-
String typeWithoutGeneric = genericIndex != -1 ? type.substring(0, genericIndex) : type;
1182-
return super.needToImport(typeWithoutGeneric);
1177+
return genericIndex == -1 ? type : type.substring(0, genericIndex);
11831178
}
11841179

11851180
@Override

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package org.openapitools.codegen.typescript;
22

3-
import lombok.Getter;
4-
import org.antlr.v4.runtime.*;
5-
import org.antlr.v4.runtime.tree.ParseTree;
6-
import org.antlr.v4.runtime.tree.ParseTreeWalker;
73
import org.apache.commons.io.FileUtils;
84
import org.apache.commons.lang3.StringUtils;
95
import org.openapitools.codegen.ClientOptInput;
@@ -26,7 +22,6 @@
2622
import java.util.List;
2723
import java.util.Map;
2824

29-
import static org.openapitools.codegen.CodegenConstants.*;
3025
import static org.openapitools.codegen.typescript.TypeScriptGroups.TYPESCRIPT;
3126

3227
@Test(groups = {TYPESCRIPT})
@@ -126,52 +121,64 @@ public void givenTypeMappingContainsGenericAndMappedTypeIsUtilityTypeThenTypeIsN
126121
File mainOutput = new File(output, "main");
127122

128123
Generator generator = new DefaultGenerator();
129-
CodegenConfigurator runtimeChecksConfigurator = new CodegenConfigurator()
124+
CodegenConfigurator configurator = new CodegenConfigurator()
130125
.setInputSpec("src/test/resources/3_1/issue_21317.yaml")
131126
.setGeneratorName("typescript-fetch")
132127
.addTypeMapping("object", "Record<string,unknown>")
133128
.setOutputDir(mainOutput.getAbsolutePath());
134-
ClientOptInput clientOptInput = runtimeChecksConfigurator.toClientOptInput();
129+
ClientOptInput clientOptInput = configurator.toClientOptInput();
135130
generator.opts(clientOptInput)
136131
.generate();
137132
String mainPath = mainOutput.getAbsolutePath();
138133
File userModel = new File(mainPath, "/models/User.ts");
139-
String userModelContent = Files.readString(userModel.toPath());
140134

141135
TestUtils.assertFileNotContains(userModel.toPath(), "Recordstringunknown");
142136
TestUtils.assertFileContains(userModel.toPath(), "Record<string,unknown>");
137+
}
143138

139+
@Test(description = "Issue #21317")
140+
public void givenTypeMappingContainsGenericAndMappedTypeIsUtilityAndWithoutRuntimeChecksTrueTypeThenTypeIsNotImportedAndTypeAppearsCorrectly() throws Exception {
141+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
142+
output.deleteOnExit();
144143

145-
File noRuntimeOutput = new File(output, "noruntime");
144+
Generator generator = new DefaultGenerator();
146145

147146
CodegenConfigurator noRuntimeConfigurator = new CodegenConfigurator()
148147
.setInputSpec("src/test/resources/3_1/issue_21317.yaml")
149148
.setGeneratorName("typescript-fetch")
150149
.addTypeMapping("object", "Record<string, unknown>")
151150
.addTypeMapping("UserSummary", "Pick<User, \"email\">")
152151
.addAdditionalProperty(TypeScriptFetchClientCodegen.WITHOUT_RUNTIME_CHECKS, true)
153-
.setOutputDir(noRuntimeOutput.getAbsolutePath());
152+
.setOutputDir(output.getAbsolutePath());
154153
ClientOptInput clientOptInput2 = noRuntimeConfigurator.toClientOptInput();
155154
generator.opts(clientOptInput2)
156155
.generate();
157-
String noRuntimePath = noRuntimeOutput.getAbsolutePath();
156+
String noRuntimePath = output.getAbsolutePath();
158157
File apiFile = new File(noRuntimePath, "/apis/DefaultApi.ts");
159-
String apiFileContent = Files.readString(apiFile.toPath());
158+
System.out.println(Files.readString(apiFile.toPath()));
160159

161160
TestUtils.assertFileContains(apiFile.toPath(), "Promise<Pick<User, \"email\">>");
162161
TestUtils.assertFileNotContains(apiFile.toPath(), "Promise<Pickuser");
162+
}
163163

164164

165+
@Test(description = "Issue #21317")
166+
public void givenTypeMappingContainsGenericAndMappedTypeIsUtilityTypeThenTypeIsNotImportedAndTypeAppearsCorrectlyAxios() throws Exception {
165167

166-
File axiosOutputPath = new File(output, "axios");
168+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
169+
output.deleteOnExit();
167170

168-
runtimeChecksConfigurator.setGeneratorName("typescript-axios")
171+
Generator generator = new DefaultGenerator();
172+
CodegenConfigurator configurator = new CodegenConfigurator()
173+
.setInputSpec("src/test/resources/3_1/issue_21317.yaml")
174+
.setGeneratorName("typescript-axios")
169175
.addTypeMapping("UserSummary", "Pick<User, \"email\">")
170-
.setOutputDir(axiosOutputPath.getAbsolutePath());
171-
generator.opts(runtimeChecksConfigurator.toClientOptInput())
176+
.addTypeMapping("object", "Record<string,unknown>")
177+
.setOutputDir(output.getAbsolutePath());
178+
generator.opts(configurator.toClientOptInput())
172179
.generate();
173180

174-
File axiosApiFile = new File(axiosOutputPath, "/api.ts");
181+
File axiosApiFile = new File(output, "/api.ts");
175182

176183
TestUtils.assertFileContains(axiosApiFile.toPath(), "AxiosPromise<Pick<User, \"email\">>");
177184
TestUtils.assertFileNotContains(axiosApiFile.toPath(), "AxiosPromise<UserSummary>");

samples/client/petstore/typescript-fetch/builds/utility-types-without-runtime/.openapi-generator-ignore

Lines changed: 0 additions & 23 deletions
This file was deleted.

samples/client/petstore/typescript-fetch/builds/utility-types-without-runtime/.openapi-generator/FILES

Lines changed: 0 additions & 5 deletions
This file was deleted.

samples/client/petstore/typescript-fetch/builds/utility-types-without-runtime/.openapi-generator/VERSION

Lines changed: 0 additions & 1 deletion
This file was deleted.

samples/client/petstore/typescript-fetch/builds/utility-types-without-runtime/apis/DefaultApi.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.

samples/client/petstore/typescript-fetch/builds/utility-types-without-runtime/apis/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

samples/client/petstore/typescript-fetch/builds/utility-types-without-runtime/index.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)