Skip to content

Commit 39b0eaf

Browse files
[typescript-nestjs-server] #22928 exclude inline union strings from generating imports
1 parent a53ebb3 commit 39b0eaf

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void processOpts() {
173173
additionalProperties.put(NEST_VERSION, nestVersion);
174174

175175
if (additionalProperties.containsKey(NPM_NAME)) {
176-
if(!additionalProperties.containsKey(NPM_VERSION)) {
176+
if (!additionalProperties.containsKey(NPM_VERSION)) {
177177
additionalProperties.put(NPM_VERSION, "0.0.0");
178178
}
179179

@@ -274,7 +274,13 @@ private String applyLocalTypeMapping(String type) {
274274
}
275275

276276
private boolean isLanguagePrimitive(String type) {
277-
return languageSpecificPrimitives.contains(type);
277+
return languageSpecificPrimitives.contains(type) || isInlineUnion(type);
278+
}
279+
280+
private boolean isInlineUnion(String type) {
281+
return Arrays.stream(type.split("\\|"))
282+
.map(String::trim)
283+
.allMatch(value -> value.matches("([\"'].*[\"'])"));
278284
}
279285

280286
private boolean isLanguageGenericType(String type) {
@@ -343,8 +349,8 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap operations, L
343349
// Collect imports from parameters
344350
if (operation.allParams != null) {
345351
for (CodegenParameter param : operation.allParams) {
346-
if(param.dataType != null) {
347-
if(isLanguageGenericType(param.dataType)) {
352+
if (param.dataType != null) {
353+
if (isLanguageGenericType(param.dataType)) {
348354
// Extract generic type and add to imports if its not a primitive
349355
String genericType = extractGenericType(param.dataType);
350356
if (genericType != null && !isLanguagePrimitive(genericType) && !isRecordType(genericType)) {
@@ -366,10 +372,10 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap operations, L
366372
if (isLanguageGenericType(operation.returnType)) {
367373
// Extract generic type and add to imports if it's not a primitive
368374
String genericType = extractGenericType(operation.returnType);
369-
if (genericType != null && !isLanguagePrimitive(genericType) && !isRecordType(genericType)) {
375+
if (needToImport(operation.returnType) && genericType != null && !isLanguagePrimitive(genericType) && !isRecordType(genericType)) {
370376
allImports.add(genericType);
371377
}
372-
} else {
378+
} else if (needToImport(operation.returnType)) {
373379
allImports.add(operation.returnType);
374380
}
375381
}
@@ -397,10 +403,10 @@ private String extractGenericType(String type) {
397403
return null;
398404
}
399405
String genericType = type.substring(startAngleBracketIndex + 1, endAngleBracketIndex);
400-
if(isLanguageGenericType(genericType)) {
406+
if (isLanguageGenericType(genericType)) {
401407
return extractGenericType(type);
402408
}
403-
if(genericType.contains("|")) {
409+
if (genericType.contains("|")) {
404410
return null;
405411
}
406412
return genericType;
@@ -429,7 +435,9 @@ private Set<String> parseImports(CodegenModel cm) {
429435
for (String name : cm.imports) {
430436
if (name.indexOf(" | ") >= 0) {
431437
String[] parts = name.split(" \\| ");
432-
Collections.addAll(newImports, parts);
438+
if (needToImport(parts[0])) {
439+
Collections.addAll(newImports, parts);
440+
}
433441
} else {
434442
newImports.add(name);
435443
}

0 commit comments

Comments
 (0)