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 b2d60b0bfcf1..961aa9c2761c 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 0c8c995de1e4..5432a14dd0c2 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"); + } + @Test(description = "Optional nullable fields should deserialize to null, not undefined (fix #5670)") public void testOptionalNullableFieldDeserializesToNull() throws Exception { File output = generate( 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"] + } + } + } + } + } + } +}