Skip to content

[typescript-fetch] Fix code generation for oneOf cases without discriminator #19219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/configs/typescript-fetch-oneOf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
generatorName: typescript-fetch
outputDir: samples/client/petstore/typescript-fetch/builds/oneOf
inputSpec: modules/openapi-generator/src/test/resources/3_0/typescript-fetch/oneOf.yaml
templateDir: modules/openapi-generator/src/main/resources/typescript-fetch
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
return {{{.}}}FromJSONTyped(json, true);
}
{{/oneOf}}

return {} as any;
{{/discriminator}}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
openapi: 3.0.0
info:
version: 1.0.0
title: testing oneOf without discriminator
servers:
- url: http://localhost:3000
paths:
/test:
get:
operationId: test
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/TestResponse'
components:
schemas:
TestResponse:
oneOf:
- $ref: "#/components/schemas/TestA"
- $ref: "#/components/schemas/TestB"
TestA:
type: object
properties:
foo:
type: string
required:
- foo
TestB:
type: object
properties:
bar:
type: string
required:
- bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apis/DefaultApi.ts
apis/index.ts
index.ts
models/TestA.ts
models/TestB.ts
models/TestResponse.ts
models/index.ts
runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.8.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* 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 * as runtime from '../runtime';
import type {
TestResponse,
} from '../models/index';
import {
TestResponseFromJSON,
TestResponseToJSON,
} from '../models/index';

/**
*
*/
export class DefaultApi extends runtime.BaseAPI {

/**
*/
async testRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<TestResponse>> {
const queryParameters: any = {};

const headerParameters: runtime.HTTPHeaders = {};

const response = await this.request({
path: `/test`,
method: 'GET',
headers: headerParameters,
query: queryParameters,
}, initOverrides);

return new runtime.JSONApiResponse(response, (jsonValue) => TestResponseFromJSON(jsonValue));
}

/**
*/
async test(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<TestResponse> {
const response = await this.testRaw(initOverrides);
return await response.value();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* tslint:disable */
/* eslint-disable */
export * from './DefaultApi';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* tslint:disable */
/* eslint-disable */
export * from './runtime';
export * from './apis/index';
export * from './models/index';
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* 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 TestA
*/
export interface TestA {
/**
*
* @type {string}
* @memberof TestA
*/
foo: string;
}

/**
* Check if a given object implements the TestA interface.
*/
export function instanceOfTestA(value: object): value is TestA {
if (!('foo' in value) || value['foo'] === undefined) return false;
return true;
}

export function TestAFromJSON(json: any): TestA {
return TestAFromJSONTyped(json, false);
}

export function TestAFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestA {
if (json == null) {
return json;
}
return {

'foo': json['foo'],
};
}

export function TestAToJSON(value?: TestA | null): any {
if (value == null) {
return value;
}
return {

'foo': value['foo'],
};
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* 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 TestB
*/
export interface TestB {
/**
*
* @type {string}
* @memberof TestB
*/
bar: string;
}

/**
* Check if a given object implements the TestB interface.
*/
export function instanceOfTestB(value: object): value is TestB {
if (!('bar' in value) || value['bar'] === undefined) return false;
return true;
}

export function TestBFromJSON(json: any): TestB {
return TestBFromJSONTyped(json, false);
}

export function TestBFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestB {
if (json == null) {
return json;
}
return {

'bar': json['bar'],
};
}

export function TestBToJSON(value?: TestB | null): any {
if (value == null) {
return value;
}
return {

'bar': value['bar'],
};
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* 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 TestResponse
*
* @export
*/
export type TestResponse = TestA | TestB;

export function TestResponseFromJSON(json: any): TestResponse {
return TestResponseFromJSONTyped(json, false);
}

export function TestResponseFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestResponse {
if (json == null) {
return json;
}
if (instanceOfTestA(json)) {
return TestAFromJSONTyped(json, true);
}
if (instanceOfTestB(json)) {
return TestBFromJSONTyped(json, true);
}

return {} as any;
}

export function TestResponseToJSON(value?: TestResponse | null): any {
if (value == null) {
return value;
}

if (instanceOfTestA(value)) {
return TestAToJSON(value as TestA);
}
if (instanceOfTestB(value)) {
return TestBToJSON(value as TestB);
}

return {};
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* tslint:disable */
/* eslint-disable */
export * from './TestA';
export * from './TestB';
export * from './TestResponse';
Loading
Loading