Fix typescript-fetch broken files when mixing basic types and refs in oneOf (#21057)

* feat: add test-array endpoint and TestArrayResponse schema to oneOf.yaml

* feat: enhance oneOf handling in TypeScript code generators with string and array support

* fix: correct type checks for string and object arrays in modelOneOf.mustache

* refactor: remove oneOfStringEnums handling and simplify oneOf logic in TypeScript code generators

* chore: update samples

* refactor: remove unnecessary string oneOf checks in TypeScriptFetchClientCodegen
This commit is contained in:
Gregory Merlet 2025-04-14 14:14:25 +02:00 committed by GitHub
parent 79b1cc5d5e
commit a94b8f90ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 213 additions and 17 deletions

View File

@ -782,16 +782,30 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
} }
} }
} }
List<CodegenProperty> oneOfsList = Optional.ofNullable(cm.getComposedSchemas())
.map(CodegenComposedSchemas::getOneOf)
.orElse(Collections.emptyList());
cm.oneOfModels = oneOfsList.stream()
.filter(CodegenProperty::getIsModel)
.map(CodegenProperty::getBaseType)
.filter(Objects::nonNull)
.collect(Collectors.toCollection(TreeSet::new));
cm.oneOfArrays = oneOfsList.stream()
.filter(CodegenProperty::getIsArray)
.map(CodegenProperty::getComplexType)
.filter(Objects::nonNull)
.collect(Collectors.toCollection(TreeSet::new));
if (!cm.oneOf.isEmpty()) { if (!cm.oneOf.isEmpty()) {
// For oneOfs only import $refs within the oneOf // For oneOfs only import $refs within the oneOf
TreeSet<String> oneOfRefs = new TreeSet<>(); cm.imports = cm.imports.stream()
for (String im : cm.imports) { .filter(im -> cm.oneOfModels.contains(im) || cm.oneOfArrays.contains(im))
if (cm.oneOf.contains(im)) { .collect(Collectors.toCollection(TreeSet::new));
oneOfRefs.add(im);
}
}
cm.imports = oneOfRefs;
} }
return cm; return cm;
} }
@ -1474,6 +1488,12 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
public class ExtendedCodegenModel extends CodegenModel { public class ExtendedCodegenModel extends CodegenModel {
@Getter @Setter @Getter @Setter
public Set<String> modelImports = new TreeSet<String>(); public Set<String> modelImports = new TreeSet<String>();
@Getter @Setter
public Set<String> oneOfModels = new TreeSet<>();
@Getter @Setter
public Set<String> oneOfArrays = new TreeSet<>();
public boolean isEntity; // Is a model containing an "id" property marked as isUniqueId public boolean isEntity; // Is a model containing an "id" property marked as isUniqueId
public String returnPassthrough; public String returnPassthrough;
public boolean hasReturnPassthroughVoid; public boolean hasReturnPassthroughVoid;
@ -1570,6 +1590,7 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
this.setItems(cm.getItems()); this.setItems(cm.getItems());
this.setAdditionalProperties(cm.getAdditionalProperties()); this.setAdditionalProperties(cm.getAdditionalProperties());
this.setIsModel(cm.getIsModel()); this.setIsModel(cm.getIsModel());
this.setComposedSchemas(cm.getComposedSchemas());
} }
@Override @Override

View File

@ -1,5 +1,5 @@
{{#hasImports}} {{#hasImports}}
{{#oneOf}} {{#imports}}
import type { {{{.}}} } from './{{.}}{{importFileExtension}}'; import type { {{{.}}} } from './{{.}}{{importFileExtension}}';
import { import {
instanceOf{{{.}}}, instanceOf{{{.}}},
@ -7,7 +7,7 @@ import {
{{{.}}}FromJSONTyped, {{{.}}}FromJSONTyped,
{{{.}}}ToJSON, {{{.}}}ToJSON,
} from './{{.}}{{importFileExtension}}'; } from './{{.}}{{importFileExtension}}';
{{/oneOf}} {{/imports}}
{{/hasImports}} {{/hasImports}}
{{>modelOneOfInterfaces}} {{>modelOneOfInterfaces}}
@ -31,11 +31,30 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
} }
{{/discriminator}} {{/discriminator}}
{{^discriminator}} {{^discriminator}}
{{#oneOf}} {{#oneOfModels}}
{{#-first}}
if (typeof json !== 'object') {
return json;
}
{{/-first}}
if (instanceOf{{{.}}}(json)) { if (instanceOf{{{.}}}(json)) {
return {{{.}}}FromJSONTyped(json, true); return {{{.}}}FromJSONTyped(json, true);
} }
{{/oneOf}} {{/oneOfModels}}
{{#oneOfArrays}}
{{#-first}}
if (Array.isArray(json)) {
if (json.every(item => typeof item === 'object')) {
{{/-first}}
if (json.every(item => instanceOf{{{.}}}(item))) {
return json.map(value => {{{.}}}FromJSONTyped(value, true));
}
{{#-last}}
}
return json;
}
{{/-last}}
{{/oneOfArrays}}
return {} as any; return {} as any;
{{/discriminator}} {{/discriminator}}
@ -59,13 +78,31 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis
return json; return json;
} }
{{/discriminator}} {{/discriminator}}
{{^discriminator}} {{^discriminator}}
{{#oneOf}} {{#oneOfModels}}
{{#-first}}
if (typeof value !== 'object') {
return value;
}
{{/-first}}
if (instanceOf{{{.}}}(value)) { if (instanceOf{{{.}}}(value)) {
return {{{.}}}ToJSON(value as {{{.}}}); return {{{.}}}ToJSON(value as {{{.}}});
} }
{{/oneOf}} {{/oneOfModels}}
{{#oneOfArrays}}
{{#-first}}
if (Array.isArray(value)) {
if (value.every(item => typeof item === 'object')) {
{{/-first}}
if (value.every(item => instanceOf{{{.}}}(item))) {
return value.map(value => {{{.}}}ToJSON(value as {{{.}}}));
}
{{#-last}}
}
return value;
}
{{/-last}}
{{/oneOfArrays}}
return {}; return {};
{{/discriminator}} {{/discriminator}}

View File

@ -15,12 +15,34 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/TestResponse' $ref: '#/components/schemas/TestResponse'
/test-array:
get:
operationId: testArray
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/TestArrayResponse'
components: components:
schemas: schemas:
TestArrayResponse:
oneOf:
- type: array
items:
$ref: "#/components/schemas/TestA"
- type: array
items:
$ref: "#/components/schemas/TestB"
- type: array
items:
type: string
TestResponse: TestResponse:
oneOf: oneOf:
- $ref: "#/components/schemas/TestA" - $ref: "#/components/schemas/TestA"
- $ref: "#/components/schemas/TestB" - $ref: "#/components/schemas/TestB"
- type: string
TestA: TestA:
type: object type: object
properties: properties:

View File

@ -2,6 +2,7 @@ apis/DefaultApi.ts
apis/index.ts apis/index.ts
index.ts index.ts
models/TestA.ts models/TestA.ts
models/TestArrayResponse.ts
models/TestB.ts models/TestB.ts
models/TestResponse.ts models/TestResponse.ts
models/index.ts models/index.ts

View File

@ -15,9 +15,12 @@
import * as runtime from '../runtime'; import * as runtime from '../runtime';
import type { import type {
TestArrayResponse,
TestResponse, TestResponse,
} from '../models/index'; } from '../models/index';
import { import {
TestArrayResponseFromJSON,
TestArrayResponseToJSON,
TestResponseFromJSON, TestResponseFromJSON,
TestResponseToJSON, TestResponseToJSON,
} from '../models/index'; } from '../models/index';
@ -51,4 +54,28 @@ export class DefaultApi extends runtime.BaseAPI {
return await response.value(); return await response.value();
} }
/**
*/
async testArrayRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<TestArrayResponse>> {
const queryParameters: any = {};
const headerParameters: runtime.HTTPHeaders = {};
const response = await this.request({
path: `/test-array`,
method: 'GET',
headers: headerParameters,
query: queryParameters,
}, initOverrides);
return new runtime.JSONApiResponse(response, (jsonValue) => TestArrayResponseFromJSON(jsonValue));
}
/**
*/
async testArray(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<TestArrayResponse> {
const response = await this.testArrayRaw(initOverrides);
return await response.value();
}
} }

View File

@ -0,0 +1,82 @@
/* tslint:disable */
/* eslint-disable */
/**
* testing oneOf without discriminator
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
import type { TestA } from './TestA';
import {
instanceOfTestA,
TestAFromJSON,
TestAFromJSONTyped,
TestAToJSON,
} from './TestA';
import type { TestB } from './TestB';
import {
instanceOfTestB,
TestBFromJSON,
TestBFromJSONTyped,
TestBToJSON,
} from './TestB';
/**
* @type TestArrayResponse
*
* @export
*/
export type TestArrayResponse = Array<TestA> | Array<TestB> | Array<string>;
export function TestArrayResponseFromJSON(json: any): TestArrayResponse {
return TestArrayResponseFromJSONTyped(json, false);
}
export function TestArrayResponseFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestArrayResponse {
if (json == null) {
return json;
}
if (Array.isArray(json)) {
if (json.every(item => typeof item === 'object')) {
if (json.every(item => instanceOfTestA(item))) {
return json.map(value => TestAFromJSONTyped(value, true));
}
if (json.every(item => instanceOfTestB(item))) {
return json.map(value => TestBFromJSONTyped(value, true));
}
}
return json;
}
return {} as any;
}
export function TestArrayResponseToJSON(json: any): any {
return TestArrayResponseToJSONTyped(json, false);
}
export function TestArrayResponseToJSONTyped(value?: TestArrayResponse | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}
if (Array.isArray(value)) {
if (value.every(item => typeof item === 'object')) {
if (value.every(item => instanceOfTestA(item))) {
return value.map(value => TestAToJSON(value as TestA));
}
if (value.every(item => instanceOfTestB(item))) {
return value.map(value => TestBToJSON(value as TestB));
}
}
return value;
}
return {};
}

View File

@ -32,7 +32,7 @@ import {
* *
* @export * @export
*/ */
export type TestResponse = TestA | TestB; export type TestResponse = TestA | TestB | string;
export function TestResponseFromJSON(json: any): TestResponse { export function TestResponseFromJSON(json: any): TestResponse {
return TestResponseFromJSONTyped(json, false); return TestResponseFromJSONTyped(json, false);
@ -42,6 +42,9 @@ export function TestResponseFromJSONTyped(json: any, ignoreDiscriminator: boolea
if (json == null) { if (json == null) {
return json; return json;
} }
if (typeof json !== 'object') {
return json;
}
if (instanceOfTestA(json)) { if (instanceOfTestA(json)) {
return TestAFromJSONTyped(json, true); return TestAFromJSONTyped(json, true);
} }
@ -60,7 +63,9 @@ export function TestResponseToJSONTyped(value?: TestResponse | null, ignoreDiscr
if (value == null) { if (value == null) {
return value; return value;
} }
if (typeof value !== 'object') {
return value;
}
if (instanceOfTestA(value)) { if (instanceOfTestA(value)) {
return TestAToJSON(value as TestA); return TestAToJSON(value as TestA);
} }

View File

@ -1,5 +1,6 @@
/* tslint:disable */ /* tslint:disable */
/* eslint-disable */ /* eslint-disable */
export * from './TestA'; export * from './TestA';
export * from './TestArrayResponse';
export * from './TestB'; export * from './TestB';
export * from './TestResponse'; export * from './TestResponse';