Skip to content

Commit fef84d9

Browse files
authored
[typescript-fetch] Fix code generation for oneOf cases without discriminator (#19219)
* Add samples using `oneOf` without discriminator * Fix typescript-fetch code gen for `oneOf` without discriminator * Return `{}` instead of original `json`
1 parent 3e263ce commit fef84d9

File tree

14 files changed

+759
-0
lines changed

14 files changed

+759
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
generatorName: typescript-fetch
2+
outputDir: samples/client/petstore/typescript-fetch/builds/oneOf
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/typescript-fetch/oneOf.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/typescript-fetch

modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
3636
return {{{.}}}FromJSONTyped(json, true);
3737
}
3838
{{/oneOf}}
39+
40+
return {} as any;
3941
{{/discriminator}}
4042
}
4143

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: testing oneOf without discriminator
5+
servers:
6+
- url: http://localhost:3000
7+
paths:
8+
/test:
9+
get:
10+
operationId: test
11+
responses:
12+
200:
13+
description: OK
14+
content:
15+
application/json:
16+
schema:
17+
$ref: '#/components/schemas/TestResponse'
18+
components:
19+
schemas:
20+
TestResponse:
21+
oneOf:
22+
- $ref: "#/components/schemas/TestA"
23+
- $ref: "#/components/schemas/TestB"
24+
TestA:
25+
type: object
26+
properties:
27+
foo:
28+
type: string
29+
required:
30+
- foo
31+
TestB:
32+
type: object
33+
properties:
34+
bar:
35+
type: string
36+
required:
37+
- bar
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apis/DefaultApi.ts
2+
apis/index.ts
3+
index.ts
4+
models/TestA.ts
5+
models/TestB.ts
6+
models/TestResponse.ts
7+
models/index.ts
8+
runtime.ts
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.8.0-SNAPSHOT
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
/**
4+
* testing oneOf without discriminator
5+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
6+
*
7+
* The version of the OpenAPI document: 1.0.0
8+
*
9+
*
10+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11+
* https://openapi-generator.tech
12+
* Do not edit the class manually.
13+
*/
14+
15+
16+
import * as runtime from '../runtime';
17+
import type {
18+
TestResponse,
19+
} from '../models/index';
20+
import {
21+
TestResponseFromJSON,
22+
TestResponseToJSON,
23+
} from '../models/index';
24+
25+
/**
26+
*
27+
*/
28+
export class DefaultApi extends runtime.BaseAPI {
29+
30+
/**
31+
*/
32+
async testRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<TestResponse>> {
33+
const queryParameters: any = {};
34+
35+
const headerParameters: runtime.HTTPHeaders = {};
36+
37+
const response = await this.request({
38+
path: `/test`,
39+
method: 'GET',
40+
headers: headerParameters,
41+
query: queryParameters,
42+
}, initOverrides);
43+
44+
return new runtime.JSONApiResponse(response, (jsonValue) => TestResponseFromJSON(jsonValue));
45+
}
46+
47+
/**
48+
*/
49+
async test(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<TestResponse> {
50+
const response = await this.testRaw(initOverrides);
51+
return await response.value();
52+
}
53+
54+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
export * from './DefaultApi';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
export * from './runtime';
4+
export * from './apis/index';
5+
export * from './models/index';
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
/**
4+
* testing oneOf without discriminator
5+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
6+
*
7+
* The version of the OpenAPI document: 1.0.0
8+
*
9+
*
10+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11+
* https://openapi-generator.tech
12+
* Do not edit the class manually.
13+
*/
14+
15+
import { mapValues } from '../runtime';
16+
/**
17+
*
18+
* @export
19+
* @interface TestA
20+
*/
21+
export interface TestA {
22+
/**
23+
*
24+
* @type {string}
25+
* @memberof TestA
26+
*/
27+
foo: string;
28+
}
29+
30+
/**
31+
* Check if a given object implements the TestA interface.
32+
*/
33+
export function instanceOfTestA(value: object): value is TestA {
34+
if (!('foo' in value) || value['foo'] === undefined) return false;
35+
return true;
36+
}
37+
38+
export function TestAFromJSON(json: any): TestA {
39+
return TestAFromJSONTyped(json, false);
40+
}
41+
42+
export function TestAFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestA {
43+
if (json == null) {
44+
return json;
45+
}
46+
return {
47+
48+
'foo': json['foo'],
49+
};
50+
}
51+
52+
export function TestAToJSON(value?: TestA | null): any {
53+
if (value == null) {
54+
return value;
55+
}
56+
return {
57+
58+
'foo': value['foo'],
59+
};
60+
}
61+

0 commit comments

Comments
 (0)