forked from loafle/openapi-generator-original
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:
parent
79b1cc5d5e
commit
a94b8f90ca
@ -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()) {
|
||||
// For oneOfs only import $refs within the oneOf
|
||||
TreeSet<String> oneOfRefs = new TreeSet<>();
|
||||
for (String im : cm.imports) {
|
||||
if (cm.oneOf.contains(im)) {
|
||||
oneOfRefs.add(im);
|
||||
}
|
||||
}
|
||||
cm.imports = oneOfRefs;
|
||||
cm.imports = cm.imports.stream()
|
||||
.filter(im -> cm.oneOfModels.contains(im) || cm.oneOfArrays.contains(im))
|
||||
.collect(Collectors.toCollection(TreeSet::new));
|
||||
}
|
||||
|
||||
return cm;
|
||||
}
|
||||
|
||||
@ -1474,6 +1488,12 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
|
||||
public class ExtendedCodegenModel extends CodegenModel {
|
||||
@Getter @Setter
|
||||
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 String returnPassthrough;
|
||||
public boolean hasReturnPassthroughVoid;
|
||||
@ -1570,6 +1590,7 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
|
||||
this.setItems(cm.getItems());
|
||||
this.setAdditionalProperties(cm.getAdditionalProperties());
|
||||
this.setIsModel(cm.getIsModel());
|
||||
this.setComposedSchemas(cm.getComposedSchemas());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,5 +1,5 @@
|
||||
{{#hasImports}}
|
||||
{{#oneOf}}
|
||||
{{#imports}}
|
||||
import type { {{{.}}} } from './{{.}}{{importFileExtension}}';
|
||||
import {
|
||||
instanceOf{{{.}}},
|
||||
@ -7,7 +7,7 @@ import {
|
||||
{{{.}}}FromJSONTyped,
|
||||
{{{.}}}ToJSON,
|
||||
} from './{{.}}{{importFileExtension}}';
|
||||
{{/oneOf}}
|
||||
{{/imports}}
|
||||
|
||||
{{/hasImports}}
|
||||
{{>modelOneOfInterfaces}}
|
||||
@ -31,11 +31,30 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
|
||||
}
|
||||
{{/discriminator}}
|
||||
{{^discriminator}}
|
||||
{{#oneOf}}
|
||||
{{#oneOfModels}}
|
||||
{{#-first}}
|
||||
if (typeof json !== 'object') {
|
||||
return json;
|
||||
}
|
||||
{{/-first}}
|
||||
if (instanceOf{{{.}}}(json)) {
|
||||
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;
|
||||
{{/discriminator}}
|
||||
@ -59,13 +78,31 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis
|
||||
return json;
|
||||
}
|
||||
{{/discriminator}}
|
||||
|
||||
{{^discriminator}}
|
||||
{{#oneOf}}
|
||||
{{#oneOfModels}}
|
||||
{{#-first}}
|
||||
if (typeof value !== 'object') {
|
||||
return value;
|
||||
}
|
||||
{{/-first}}
|
||||
if (instanceOf{{{.}}}(value)) {
|
||||
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 {};
|
||||
{{/discriminator}}
|
||||
|
@ -15,12 +15,34 @@ paths:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/TestResponse'
|
||||
/test-array:
|
||||
get:
|
||||
operationId: testArray
|
||||
responses:
|
||||
200:
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/TestArrayResponse'
|
||||
components:
|
||||
schemas:
|
||||
TestArrayResponse:
|
||||
oneOf:
|
||||
- type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/TestA"
|
||||
- type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/TestB"
|
||||
- type: array
|
||||
items:
|
||||
type: string
|
||||
TestResponse:
|
||||
oneOf:
|
||||
- $ref: "#/components/schemas/TestA"
|
||||
- $ref: "#/components/schemas/TestB"
|
||||
- type: string
|
||||
TestA:
|
||||
type: object
|
||||
properties:
|
||||
@ -34,4 +56,4 @@ components:
|
||||
bar:
|
||||
type: string
|
||||
required:
|
||||
- bar
|
||||
- bar
|
@ -2,6 +2,7 @@ apis/DefaultApi.ts
|
||||
apis/index.ts
|
||||
index.ts
|
||||
models/TestA.ts
|
||||
models/TestArrayResponse.ts
|
||||
models/TestB.ts
|
||||
models/TestResponse.ts
|
||||
models/index.ts
|
||||
|
@ -15,9 +15,12 @@
|
||||
|
||||
import * as runtime from '../runtime';
|
||||
import type {
|
||||
TestArrayResponse,
|
||||
TestResponse,
|
||||
} from '../models/index';
|
||||
import {
|
||||
TestArrayResponseFromJSON,
|
||||
TestArrayResponseToJSON,
|
||||
TestResponseFromJSON,
|
||||
TestResponseToJSON,
|
||||
} from '../models/index';
|
||||
@ -51,4 +54,28 @@ export class DefaultApi extends runtime.BaseAPI {
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 {};
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ import {
|
||||
*
|
||||
* @export
|
||||
*/
|
||||
export type TestResponse = TestA | TestB;
|
||||
export type TestResponse = TestA | TestB | string;
|
||||
|
||||
export function TestResponseFromJSON(json: any): TestResponse {
|
||||
return TestResponseFromJSONTyped(json, false);
|
||||
@ -42,6 +42,9 @@ export function TestResponseFromJSONTyped(json: any, ignoreDiscriminator: boolea
|
||||
if (json == null) {
|
||||
return json;
|
||||
}
|
||||
if (typeof json !== 'object') {
|
||||
return json;
|
||||
}
|
||||
if (instanceOfTestA(json)) {
|
||||
return TestAFromJSONTyped(json, true);
|
||||
}
|
||||
@ -60,7 +63,9 @@ export function TestResponseToJSONTyped(value?: TestResponse | null, ignoreDiscr
|
||||
if (value == null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value !== 'object') {
|
||||
return value;
|
||||
}
|
||||
if (instanceOfTestA(value)) {
|
||||
return TestAToJSON(value as TestA);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
export * from './TestA';
|
||||
export * from './TestArrayResponse';
|
||||
export * from './TestB';
|
||||
export * from './TestResponse';
|
||||
|
Loading…
x
Reference in New Issue
Block a user