mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-29 20:20:53 +00:00
Fix typescript-fetch missing imports for oneof field with discriminator (#21477)
* fix: add support for discriminator in oneOf schemas and update model imports * chore: generate samples * fix: enhance model imports for oneOf arrays in mustache templates * fix: correct import formatting in mustache templates and TypeScript files
This commit is contained in:
parent
b6b71cd4da
commit
d11d008e71
@ -1,5 +1,5 @@
|
|||||||
{{#hasImports}}
|
{{#hasImports}}
|
||||||
{{#imports}}
|
{{#oneOfArrays}}
|
||||||
import type { {{{.}}} } from './{{.}}{{importFileExtension}}';
|
import type { {{{.}}} } from './{{.}}{{importFileExtension}}';
|
||||||
import {
|
import {
|
||||||
instanceOf{{{.}}},
|
instanceOf{{{.}}},
|
||||||
@ -7,7 +7,16 @@ import {
|
|||||||
{{{.}}}FromJSONTyped,
|
{{{.}}}FromJSONTyped,
|
||||||
{{{.}}}ToJSON,
|
{{{.}}}ToJSON,
|
||||||
} from './{{.}}{{importFileExtension}}';
|
} from './{{.}}{{importFileExtension}}';
|
||||||
{{/imports}}
|
{{/oneOfArrays}}
|
||||||
|
{{#oneOfModels}}
|
||||||
|
import type { {{{.}}} } from './{{.}}{{importFileExtension}}';
|
||||||
|
import {
|
||||||
|
instanceOf{{{.}}},
|
||||||
|
{{{.}}}FromJSON,
|
||||||
|
{{{.}}}FromJSONTyped,
|
||||||
|
{{{.}}}ToJSON,
|
||||||
|
} from './{{.}}{{importFileExtension}}';
|
||||||
|
{{/oneOfModels}}
|
||||||
|
|
||||||
{{/hasImports}}
|
{{/hasImports}}
|
||||||
{{>modelOneOfInterfaces}}
|
{{>modelOneOfInterfaces}}
|
||||||
|
@ -25,6 +25,16 @@ paths:
|
|||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/components/schemas/TestArrayResponse'
|
$ref: '#/components/schemas/TestArrayResponse'
|
||||||
|
/test-discriminator:
|
||||||
|
get:
|
||||||
|
operationId: testDiscriminator
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: OK
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/TestDiscriminatorResponse'
|
||||||
components:
|
components:
|
||||||
schemas:
|
schemas:
|
||||||
TestArrayResponse:
|
TestArrayResponse:
|
||||||
@ -38,6 +48,15 @@ components:
|
|||||||
- type: array
|
- type: array
|
||||||
items:
|
items:
|
||||||
type: string
|
type: string
|
||||||
|
TestDiscriminatorResponse:
|
||||||
|
discriminator:
|
||||||
|
propertyName: discriminatorField
|
||||||
|
mapping:
|
||||||
|
optionOne: "#/components/schemas/OptionOne"
|
||||||
|
optionTwo: "#/components/schemas/OptionTwo"
|
||||||
|
oneOf:
|
||||||
|
- $ref: "#/components/schemas/OptionOne"
|
||||||
|
- $ref: "#/components/schemas/OptionTwo"
|
||||||
TestResponse:
|
TestResponse:
|
||||||
oneOf:
|
oneOf:
|
||||||
- $ref: "#/components/schemas/TestA"
|
- $ref: "#/components/schemas/TestA"
|
||||||
@ -57,3 +76,21 @@ components:
|
|||||||
type: string
|
type: string
|
||||||
required:
|
required:
|
||||||
- bar
|
- bar
|
||||||
|
OptionOne:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
discriminatorField:
|
||||||
|
enum:
|
||||||
|
- "optionOne"
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- discriminatorField
|
||||||
|
OptionTwo:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
discriminatorField:
|
||||||
|
enum:
|
||||||
|
- "optionTwo"
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- discriminatorField
|
@ -1,9 +1,12 @@
|
|||||||
apis/DefaultApi.ts
|
apis/DefaultApi.ts
|
||||||
apis/index.ts
|
apis/index.ts
|
||||||
index.ts
|
index.ts
|
||||||
|
models/OptionOne.ts
|
||||||
|
models/OptionTwo.ts
|
||||||
models/TestA.ts
|
models/TestA.ts
|
||||||
models/TestArrayResponse.ts
|
models/TestArrayResponse.ts
|
||||||
models/TestB.ts
|
models/TestB.ts
|
||||||
|
models/TestDiscriminatorResponse.ts
|
||||||
models/TestResponse.ts
|
models/TestResponse.ts
|
||||||
models/index.ts
|
models/index.ts
|
||||||
runtime.ts
|
runtime.ts
|
||||||
|
@ -16,11 +16,14 @@
|
|||||||
import * as runtime from '../runtime';
|
import * as runtime from '../runtime';
|
||||||
import type {
|
import type {
|
||||||
TestArrayResponse,
|
TestArrayResponse,
|
||||||
|
TestDiscriminatorResponse,
|
||||||
TestResponse,
|
TestResponse,
|
||||||
} from '../models/index';
|
} from '../models/index';
|
||||||
import {
|
import {
|
||||||
TestArrayResponseFromJSON,
|
TestArrayResponseFromJSON,
|
||||||
TestArrayResponseToJSON,
|
TestArrayResponseToJSON,
|
||||||
|
TestDiscriminatorResponseFromJSON,
|
||||||
|
TestDiscriminatorResponseToJSON,
|
||||||
TestResponseFromJSON,
|
TestResponseFromJSON,
|
||||||
TestResponseToJSON,
|
TestResponseToJSON,
|
||||||
} from '../models/index';
|
} from '../models/index';
|
||||||
@ -84,4 +87,31 @@ export class DefaultApi extends runtime.BaseAPI {
|
|||||||
return await response.value();
|
return await response.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
async testDiscriminatorRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<TestDiscriminatorResponse>> {
|
||||||
|
const queryParameters: any = {};
|
||||||
|
|
||||||
|
const headerParameters: runtime.HTTPHeaders = {};
|
||||||
|
|
||||||
|
|
||||||
|
let urlPath = `/test-discriminator`;
|
||||||
|
|
||||||
|
const response = await this.request({
|
||||||
|
path: urlPath,
|
||||||
|
method: 'GET',
|
||||||
|
headers: headerParameters,
|
||||||
|
query: queryParameters,
|
||||||
|
}, initOverrides);
|
||||||
|
|
||||||
|
return new runtime.JSONApiResponse(response, (jsonValue) => TestDiscriminatorResponseFromJSON(jsonValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
async testDiscriminator(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<TestDiscriminatorResponse> {
|
||||||
|
const response = await this.testDiscriminatorRaw(initOverrides);
|
||||||
|
return await response.value();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,76 @@
|
|||||||
|
/* 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 { mapValues } from '../runtime';
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @interface OptionOne
|
||||||
|
*/
|
||||||
|
export interface OptionOne {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @type {string}
|
||||||
|
* @memberof OptionOne
|
||||||
|
*/
|
||||||
|
discriminatorField: OptionOneDiscriminatorFieldEnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @export
|
||||||
|
*/
|
||||||
|
export const OptionOneDiscriminatorFieldEnum = {
|
||||||
|
OptionOne: 'optionOne'
|
||||||
|
} as const;
|
||||||
|
export type OptionOneDiscriminatorFieldEnum = typeof OptionOneDiscriminatorFieldEnum[keyof typeof OptionOneDiscriminatorFieldEnum];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a given object implements the OptionOne interface.
|
||||||
|
*/
|
||||||
|
export function instanceOfOptionOne(value: object): value is OptionOne {
|
||||||
|
if (!('discriminatorField' in value) || value['discriminatorField'] === undefined) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OptionOneFromJSON(json: any): OptionOne {
|
||||||
|
return OptionOneFromJSONTyped(json, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OptionOneFromJSONTyped(json: any, ignoreDiscriminator: boolean): OptionOne {
|
||||||
|
if (json == null) {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
|
||||||
|
'discriminatorField': json['discriminatorField'],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OptionOneToJSON(json: any): OptionOne {
|
||||||
|
return OptionOneToJSONTyped(json, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OptionOneToJSONTyped(value?: OptionOne | null, ignoreDiscriminator: boolean = false): any {
|
||||||
|
if (value == null) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
|
||||||
|
'discriminatorField': value['discriminatorField'],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,76 @@
|
|||||||
|
/* 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 { mapValues } from '../runtime';
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @interface OptionTwo
|
||||||
|
*/
|
||||||
|
export interface OptionTwo {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @type {string}
|
||||||
|
* @memberof OptionTwo
|
||||||
|
*/
|
||||||
|
discriminatorField: OptionTwoDiscriminatorFieldEnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @export
|
||||||
|
*/
|
||||||
|
export const OptionTwoDiscriminatorFieldEnum = {
|
||||||
|
OptionTwo: 'optionTwo'
|
||||||
|
} as const;
|
||||||
|
export type OptionTwoDiscriminatorFieldEnum = typeof OptionTwoDiscriminatorFieldEnum[keyof typeof OptionTwoDiscriminatorFieldEnum];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a given object implements the OptionTwo interface.
|
||||||
|
*/
|
||||||
|
export function instanceOfOptionTwo(value: object): value is OptionTwo {
|
||||||
|
if (!('discriminatorField' in value) || value['discriminatorField'] === undefined) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OptionTwoFromJSON(json: any): OptionTwo {
|
||||||
|
return OptionTwoFromJSONTyped(json, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OptionTwoFromJSONTyped(json: any, ignoreDiscriminator: boolean): OptionTwo {
|
||||||
|
if (json == null) {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
|
||||||
|
'discriminatorField': json['discriminatorField'],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OptionTwoToJSON(json: any): OptionTwo {
|
||||||
|
return OptionTwoToJSONTyped(json, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function OptionTwoToJSONTyped(value?: OptionTwo | null, ignoreDiscriminator: boolean = false): any {
|
||||||
|
if (value == null) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
|
||||||
|
'discriminatorField': value['discriminatorField'],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,72 @@
|
|||||||
|
/* 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 { OptionOne } from './OptionOne';
|
||||||
|
import {
|
||||||
|
instanceOfOptionOne,
|
||||||
|
OptionOneFromJSON,
|
||||||
|
OptionOneFromJSONTyped,
|
||||||
|
OptionOneToJSON,
|
||||||
|
} from './OptionOne';
|
||||||
|
import type { OptionTwo } from './OptionTwo';
|
||||||
|
import {
|
||||||
|
instanceOfOptionTwo,
|
||||||
|
OptionTwoFromJSON,
|
||||||
|
OptionTwoFromJSONTyped,
|
||||||
|
OptionTwoToJSON,
|
||||||
|
} from './OptionTwo';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type TestDiscriminatorResponse
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
*/
|
||||||
|
export type TestDiscriminatorResponse = { discriminatorField: 'optionOne' } & OptionOne | { discriminatorField: 'optionTwo' } & OptionTwo;
|
||||||
|
|
||||||
|
export function TestDiscriminatorResponseFromJSON(json: any): TestDiscriminatorResponse {
|
||||||
|
return TestDiscriminatorResponseFromJSONTyped(json, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function TestDiscriminatorResponseFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestDiscriminatorResponse {
|
||||||
|
if (json == null) {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
switch (json['discriminatorField']) {
|
||||||
|
case 'optionOne':
|
||||||
|
return Object.assign({}, OptionOneFromJSONTyped(json, true), { discriminatorField: 'optionOne' } as const);
|
||||||
|
case 'optionTwo':
|
||||||
|
return Object.assign({}, OptionTwoFromJSONTyped(json, true), { discriminatorField: 'optionTwo' } as const);
|
||||||
|
default:
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function TestDiscriminatorResponseToJSON(json: any): any {
|
||||||
|
return TestDiscriminatorResponseToJSONTyped(json, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function TestDiscriminatorResponseToJSONTyped(value?: TestDiscriminatorResponse | null, ignoreDiscriminator: boolean = false): any {
|
||||||
|
if (value == null) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
switch (value['discriminatorField']) {
|
||||||
|
case 'optionOne':
|
||||||
|
return Object.assign({}, OptionOneToJSON(value), { discriminatorField: 'optionOne' } as const);
|
||||||
|
case 'optionTwo':
|
||||||
|
return Object.assign({}, OptionTwoToJSON(value), { discriminatorField: 'optionTwo' } as const);
|
||||||
|
default:
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,9 @@
|
|||||||
/* tslint:disable */
|
/* tslint:disable */
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
|
export * from './OptionOne';
|
||||||
|
export * from './OptionTwo';
|
||||||
export * from './TestA';
|
export * from './TestA';
|
||||||
export * from './TestArrayResponse';
|
export * from './TestArrayResponse';
|
||||||
export * from './TestB';
|
export * from './TestB';
|
||||||
|
export * from './TestDiscriminatorResponse';
|
||||||
export * from './TestResponse';
|
export * from './TestResponse';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user