Skip to content

Commit 4e35b66

Browse files
authored
fix(typescript-fetch): skip scalar enum check for array-typed properties in instanceOf (#24217)
The instanceOf guard was emitting an invalid scalar !== comparison for required properties typed as Array<SingleValueEnum>, introduced as a side effect of PR #23497. The enum-literal check block now only fires for non-container (scalar) properties
1 parent 19c5943 commit 4e35b66

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function instanceOf{{classname}}(value: object): value is {{classname}} {
3232
if (!('{{name}}' in value) || value['{{name}}'] === undefined) return false;
3333
{{/hasSanitizedName}}
3434
{{#isEnum}}
35+
{{^isContainer}}
3536
{{#allowableValues}}
3637
{{#values}}
3738
{{#-first}}
@@ -48,6 +49,7 @@ export function instanceOf{{classname}}(value: object): value is {{classname}} {
4849
{{/-first}}
4950
{{/values}}
5051
{{/allowableValues}}
52+
{{/isContainer}}
5153
{{/isEnum}}
5254
{{/required}}
5355
{{/vars}}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,21 @@ public void testNestedOneOfGeneratesTypeAliasForOneOfParent() throws IOException
653653
TestUtils.assertFileContains(outerPlain, "export interface OuterPlain {");
654654
}
655655

656+
@Test(description = "instanceOf guard must not emit scalar enum comparison for array-typed enum properties")
657+
public void testInstanceOfArrayEnumNoScalarComparison() throws Exception {
658+
File output = generate(
659+
Collections.emptyMap(),
660+
"src/test/resources/3_0/typescript-fetch/array_of_single_value_enum.json"
661+
);
662+
663+
Path modelPath = Paths.get(output + "/models/TestSchema.ts");
664+
// Must NOT emit a scalar !== comparison for an array-typed enum property
665+
TestUtils.assertFileNotContains(modelPath, "value['types'] !== 'boatbooker_activities'");
666+
TestUtils.assertFileNotContains(modelPath, "value['types'] !== boatbooker_activities");
667+
// Must still check for presence of the field
668+
TestUtils.assertFileContains(modelPath, "'types' in value");
669+
}
670+
656671
@Test(description = "Optional nullable fields should deserialize to null, not undefined (fix #5670)")
657672
public void testOptionalNullableFieldDeserializesToNull() throws Exception {
658673
File output = generate(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Array of Single-Value Enum Test",
5+
"version": "1.0.0"
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"TestSchema": {
11+
"required": ["types"],
12+
"type": "object",
13+
"properties": {
14+
"types": {
15+
"type": "array",
16+
"items": {
17+
"type": "string",
18+
"enum": ["boatbooker_activities"]
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)