From 419493bf0c8ef7a3a45a274f69494f72109b5377 Mon Sep 17 00:00:00 2001 From: Marko Stojanovic Date: Mon, 6 Jul 2026 20:55:37 +0200 Subject: [PATCH] fix(typescript-fetch): skip scalar enum check for array-typed properties in instanceOf The instanceOf guard was emitting an invalid scalar !== comparison for required properties typed as Array, introduced as a side effect of PR #23497. The enum-literal check block now only fires for non-container (scalar) properties --- .../typescript-fetch/modelGeneric.mustache | 2 ++ .../TypeScriptFetchClientCodegenTest.java | 15 +++++++++++ .../array_of_single_value_enum.json | 25 +++++++++++++++++++ .../server/petstore/jaxrs-spec-enum/pom.xml | 12 ++++++--- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/typescript-fetch/array_of_single_value_enum.json diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache index 4046fed3b286..2c3e6c8b150f 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache @@ -32,6 +32,7 @@ export function instanceOf{{classname}}(value: object): value is {{classname}} { if (!('{{name}}' in value) || value['{{name}}'] === undefined) return false; {{/hasSanitizedName}} {{#isEnum}} + {{^isContainer}} {{#allowableValues}} {{#values}} {{#-first}} @@ -48,6 +49,7 @@ export function instanceOf{{classname}}(value: object): value is {{classname}} { {{/-first}} {{/values}} {{/allowableValues}} + {{/isContainer}} {{/isEnum}} {{/required}} {{/vars}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java index eafa46e33ce4..c5e4bde71e41 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java @@ -653,6 +653,21 @@ public void testNestedOneOfGeneratesTypeAliasForOneOfParent() throws IOException TestUtils.assertFileContains(outerPlain, "export interface OuterPlain {"); } + @Test(description = "instanceOf guard must not emit scalar enum comparison for array-typed enum properties") + public void testInstanceOfArrayEnumNoScalarComparison() throws Exception { + File output = generate( + Collections.emptyMap(), + "src/test/resources/3_0/typescript-fetch/array_of_single_value_enum.json" + ); + + Path modelPath = Paths.get(output + "/models/TestSchema.ts"); + // Must NOT emit a scalar !== comparison for an array-typed enum property + TestUtils.assertFileNotContains(modelPath, "value['types'] !== 'boatbooker_activities'"); + TestUtils.assertFileNotContains(modelPath, "value['types'] !== boatbooker_activities"); + // Must still check for presence of the field + TestUtils.assertFileContains(modelPath, "'types' in value"); + } + private static File generate( Map properties ) throws IOException { diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/array_of_single_value_enum.json b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/array_of_single_value_enum.json new file mode 100644 index 000000000000..f3d86539797a --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/array_of_single_value_enum.json @@ -0,0 +1,25 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "Array of Single-Value Enum Test", + "version": "1.0.0" + }, + "paths": {}, + "components": { + "schemas": { + "TestSchema": { + "required": ["types"], + "type": "object", + "properties": { + "types": { + "type": "array", + "items": { + "type": "string", + "enum": ["boatbooker_activities"] + } + } + } + } + } + } +} diff --git a/samples/server/petstore/jaxrs-spec-enum/pom.xml b/samples/server/petstore/jaxrs-spec-enum/pom.xml index 6d963d6dc72a..6f8ea7bbc4d2 100644 --- a/samples/server/petstore/jaxrs-spec-enum/pom.xml +++ b/samples/server/petstore/jaxrs-spec-enum/pom.xml @@ -37,9 +37,13 @@ false + + maven-surefire-plugin + 3.5.6 + maven-failsafe-plugin - 2.6 + 3.5.6 @@ -91,8 +95,8 @@ 3.0.2 - junit - junit + org.junit.jupiter + junit-jupiter-engine ${junit-version} test @@ -135,7 +139,7 @@ ${java.version} UTF-8 2.19.2 - 4.13.2 + 5.14.4 2.10.13 1.3.2 2.0.2