Skip to content

Commit b86213b

Browse files
authored
[typescript-node] Fixes generation when parent contains TypeScript primitive (#22401)
* fixes parents when schema has additional properties * exclude primitive types from parent property
1 parent ce21b9e commit b86213b

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
184184
for (ModelMap mo : entry.getModels()) {
185185
CodegenModel cm = mo.getModel();
186186

187+
// Filter out primitive types from parent property
188+
if (cm.parent != null && isPrimitiveType(cm.parent)) {
189+
cm.parent = null;
190+
}
191+
187192
// Add additional filename information for imports
188193
mo.put("tsImports", toTsImports(cm, cm.imports));
189194
}
@@ -289,6 +294,26 @@ private boolean isLanguagePrimitive(String type) {
289294
return languageSpecificPrimitives.contains(type);
290295
}
291296

297+
/**
298+
* Check if a type is a primitive TypeScript type (string, boolean, number).
299+
* This is used to filter out primitive types from the parent property.
300+
*
301+
* @param type the type to check
302+
* @return true if the type is a primitive type
303+
*/
304+
private boolean isPrimitiveType(String type) {
305+
if (type == null) {
306+
return false;
307+
}
308+
// Check for primitive types (case-insensitive)
309+
String lowerType = type.toLowerCase(Locale.ROOT);
310+
return "string".equals(lowerType) ||
311+
"boolean".equals(lowerType) ||
312+
"number".equals(lowerType) ||
313+
"any".equals(lowerType) ||
314+
"array".equals(lowerType);
315+
}
316+
292317
// Determines if the given type is a generic/templated type (ie. ArrayList<String>)
293318
private boolean isLanguageGenericType(String type) {
294319
for (String genericType : languageGenericTypes) {
@@ -323,6 +348,15 @@ private String removeModelPrefixSuffix(String name) {
323348
return result;
324349
}
325350

351+
@Override
352+
protected void addParentFromContainer(CodegenModel model, Schema schema) {
353+
super.addParentFromContainer(model, schema);
354+
// Filter out primitive types from parent property
355+
if (model.parent != null && isPrimitiveType(model.parent)) {
356+
model.parent = null;
357+
}
358+
}
359+
326360
@Override
327361
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
328362
super.addAdditionPropertiesToCodeGenModel(codegenModel, schema);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,35 @@ public void mappedFromModelTest() {
354354
Assert.assertEquals(cm.name, "ApiResponse");
355355
Assert.assertEquals(cm.classFilename, mappedName);
356356
}
357+
358+
@Test(description = "should exclude TypeScript primitive types from parent property")
359+
public void modelShouldExcludePrimitiveTypesFromParentPropertyTest() {
360+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/model_with_additional_properties.yaml");
361+
final DefaultCodegen codegen = new TypeScriptNodeClientCodegen();
362+
codegen.setOpenAPI(openAPI);
363+
364+
// Check that all models with primitive additionalProperties don't have parent property set
365+
for (String modelName : openAPI.getComponents().getSchemas().keySet()) {
366+
final Schema schema = openAPI.getComponents().getSchemas().get(modelName);
367+
final CodegenModel model = codegen.fromModel(modelName, schema);
368+
Assert.assertNull(model.parent, "Model " + modelName + " should not have a parent property");
369+
}
370+
}
371+
372+
@Test(description = "should extend from parent")
373+
public void shouldExtendFromParentTest() {
374+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/allOf.yaml");
375+
final DefaultCodegen codegen = new TypeScriptNodeClientCodegen();
376+
codegen.setOpenAPI(openAPI);
377+
378+
// Check that Child model extends Person
379+
final Schema childSchema = openAPI.getComponents().getSchemas().get("Child");
380+
final CodegenModel childModel = codegen.fromModel("Child", childSchema);
381+
Assert.assertEquals(childModel.parent, "Person");
382+
383+
// Check that Adult model extends Person
384+
final Schema adultSchema = openAPI.getComponents().getSchemas().get("Adult");
385+
final CodegenModel adultModel = codegen.fromModel("Adult", adultSchema);
386+
Assert.assertEquals(adultModel.parent, "Person");
387+
}
357388
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: OpenAPI Petstore
5+
license:
6+
name: Apache-2.0
7+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
8+
paths:
9+
/pet:
10+
get:
11+
responses:
12+
'200':
13+
description: OK
14+
content:
15+
application/json:
16+
schema:
17+
$ref: '#/components/schemas/Pet'
18+
components:
19+
schemas:
20+
Pet:
21+
title: a Pet with string additionalProperties
22+
type: object
23+
properties:
24+
id:
25+
type: integer
26+
format: int64
27+
additionalProperties:
28+
type: string
29+
PetBoolean:
30+
title: a Pet with boolean additionalProperties
31+
type: object
32+
properties:
33+
id:
34+
type: integer
35+
format: int64
36+
additionalProperties:
37+
type: boolean
38+
PetNumber:
39+
title: a Pet with number additionalProperties
40+
type: object
41+
properties:
42+
id:
43+
type: integer
44+
format: int64
45+
additionalProperties:
46+
type: number
47+
PetAny:
48+
title: a Pet with any additionalProperties
49+
type: object
50+
properties:
51+
id:
52+
type: integer
53+
format: int64
54+
additionalProperties: true
55+
PetArray:
56+
title: a Pet with array additionalProperties
57+
type: object
58+
properties:
59+
id:
60+
type: integer
61+
format: int64
62+
additionalProperties:
63+
type: array
64+
items:
65+
type: string

0 commit comments

Comments
 (0)