From 9c4f6aa0f001fa9329c0c580d15db74c47df4384 Mon Sep 17 00:00:00 2001 From: rk Date: Sat, 24 Oct 2026 12:24:14 +0500 Subject: [PATCH] fix(typescript-angular): generate union type for anyOf of primitives --- .../typescript-angular/model.mustache | 2 +- .../typescript-angular/modelAnyOf.mustache | 6 +++++ .../TypeScriptAngularClientCodegenTest.java | 26 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/main/resources/typescript-angular/modelAnyOf.mustache diff --git a/modules/openapi-generator/src/main/resources/typescript-angular/model.mustache b/modules/openapi-generator/src/main/resources/typescript-angular/model.mustache index fb2f624d5180..332834df8b90 100644 --- a/modules/openapi-generator/src/main/resources/typescript-angular/model.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-angular/model.mustache @@ -11,6 +11,6 @@ import { {{classname}} } from '{{filename}}'; * {{{.}}} */ {{/description}} -{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#isAlias}}{{>modelAlias}}{{/isAlias}}{{^isAlias}}{{#taggedUnions}}{{>modelTaggedUnion}}{{/taggedUnions}}{{^taggedUnions}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/taggedUnions}}{{/isAlias}}{{/isEnum}} +{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#isAlias}}{{>modelAlias}}{{/isAlias}}{{^isAlias}}{{#taggedUnions}}{{>modelTaggedUnion}}{{/taggedUnions}}{{^taggedUnions}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^oneOf}}{{#hasVars}}{{>modelGeneric}}{{/hasVars}}{{^hasVars}}{{#anyOf}}{{#-first}}{{>modelAnyOf}}{{/-first}}{{/anyOf}}{{^anyOf}}{{>modelGeneric}}{{/anyOf}}{{/hasVars}}{{/oneOf}}{{/taggedUnions}}{{/isAlias}}{{/isEnum}} {{/model}} {{/models}} diff --git a/modules/openapi-generator/src/main/resources/typescript-angular/modelAnyOf.mustache b/modules/openapi-generator/src/main/resources/typescript-angular/modelAnyOf.mustache new file mode 100644 index 000000000000..5174a26f99b7 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/typescript-angular/modelAnyOf.mustache @@ -0,0 +1,6 @@ +/** + * @type {{classname}}{{#description}} + * {{{.}}}{{/description}} + * @export + */ +export type {{classname}} = {{#anyOf}}{{{.}}}{{^-last}} | {{/-last}}{{/anyOf}}; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java index dbece37188b2..66b659189c6e 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java @@ -724,4 +724,30 @@ public void testHttpResourceSkipsAnOperationWhoseNameIsTaken() throws IOExceptio TestUtils.assertFileContains(service, "public getPetResourceResource(options?: ApiHttpResourceOptions)"); TestUtils.assertFileNotContains(service, "public getPetResource(options?: ApiHttpResourceOptions)"); } + + @Test + public void testAnyOfOfPrimitivesGeneratesUnionType() throws IOException { + Path spec = Files.createTempFile("anyof-primitives", ".yaml"); + spec.toFile().deleteOnExit(); + Files.writeString(spec, String.join("\n", + "openapi: 3.0.0", + "info: {title: anyof, version: 1.0.0}", + "paths: {}", + "components:", + " schemas:", + " Foo:", + " type: object", + " properties:", + " bar: {anyOf: [{type: number}, {type: string}]}", + " pet: {anyOf: [{$ref: '#/components/schemas/Cat'}, {$ref: '#/components/schemas/Dog'}]}", + " Cat: {type: object, properties: {meow: {type: boolean}}}", + " Dog: {type: object, properties: {bark: {type: boolean}}}", + "")); + + File output = generateAngular(spec.toString(), new HashMap<>()); + + TestUtils.assertFileContains(Paths.get(output + "/model/fooBar.ts"), "export type FooBar = number | string;"); + // anyOf of objects keeps merged interface + TestUtils.assertFileContains(Paths.get(output + "/model/fooPet.ts"), "export interface FooPet {"); + } }