Skip to content

Commit 4aef428

Browse files
author
svolkov
committed
test: add examples with allOf, oneOf schemas
1 parent 884bb64 commit 4aef428

File tree

4 files changed

+311
-0
lines changed

4 files changed

+311
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
4+
/*
5+
* ---------------------------------------------------------------
6+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
7+
* ## ##
8+
* ## AUTHOR: acacode ##
9+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
10+
* ---------------------------------------------------------------
11+
*/
12+
13+
14+
export interface Pet {
15+
pet_type: string;
16+
}
17+
18+
export type Dog = Pet & { bark?: boolean, breed?: "Dingo" | "Husky" | "Retriever" | "Shepherd" }
19+
20+
export type Cat = Pet & { hunts?: boolean, age?: number }
21+
22+
export type RequestParams = Omit<RequestInit, "body" | "method"> & {
23+
secure?: boolean;
24+
}
25+
26+
type ApiConfig<SecurityDataType> = {
27+
baseUrl?: string,
28+
baseApiParams?: RequestParams,
29+
securityWorker?: (securityData: SecurityDataType) => RequestParams,
30+
}
31+
32+
33+
34+
export class Api<SecurityDataType> {
35+
36+
public baseUrl = "";
37+
public title = "Allof Example";
38+
public version = "1.0.0";
39+
40+
private securityData: SecurityDataType = (null as any);
41+
private securityWorker: ApiConfig<SecurityDataType>["securityWorker"] = (() => {}) as any
42+
43+
private baseApiParams: RequestParams = {
44+
credentials: 'same-origin',
45+
headers: {
46+
'Content-Type': 'application/json'
47+
},
48+
redirect: 'follow',
49+
referrerPolicy: 'no-referrer',
50+
}
51+
52+
constructor({ baseUrl,baseApiParams,securityWorker, }: ApiConfig<SecurityDataType> = {}) {
53+
this.baseUrl = baseUrl || this.baseUrl;
54+
this.baseApiParams = baseApiParams || this.baseApiParams;
55+
this.securityWorker = securityWorker || this.securityWorker;
56+
}
57+
58+
public setSecurityData = (data: SecurityDataType) => {
59+
this.securityData = data
60+
}
61+
62+
63+
private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams {
64+
return {
65+
...this.baseApiParams,
66+
...params,
67+
...(securityParams || {}),
68+
headers: {
69+
...(this.baseApiParams.headers || {}),
70+
...(params.headers || {}),
71+
...((securityParams && securityParams.headers) || {})
72+
}
73+
}
74+
}
75+
76+
private safeParseResponse = <T = any>(response: Response): Promise<T> =>
77+
response.json()
78+
.then(data => data)
79+
.catch(e => response.text);
80+
81+
public request = <T = any>(
82+
path: string,
83+
method: string,
84+
{ secure, ...params }: RequestParams = {},
85+
body?: any,
86+
secureByDefault?: boolean,
87+
): Promise<T> =>
88+
fetch(`${this.baseUrl}${path}`, {
89+
// @ts-ignore
90+
...this.mergeRequestOptions(params, (secureByDefault || secure) && this.securityWorker(this.securityData)),
91+
method,
92+
body: body ? JSON.stringify(body) : null,
93+
}).then(async response => {
94+
const data = await this.safeParseResponse<T>(response);
95+
if (!response.ok) throw data
96+
return data
97+
})
98+
99+
100+
101+
pets = {
102+
103+
104+
/**
105+
* @name patch
106+
* @request PATCH:/pets
107+
*/
108+
patch: (data: Cat | Dog, params?: RequestParams) =>
109+
this.request<any>(`/pets`, "PATCH", params, data),
110+
}
111+
112+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
4+
/*
5+
* ---------------------------------------------------------------
6+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
7+
* ## ##
8+
* ## AUTHOR: acacode ##
9+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
10+
* ---------------------------------------------------------------
11+
*/
12+
13+
14+
export interface Dog {
15+
bark?: boolean;
16+
breed?: "Dingo" | "Husky" | "Retriever" | "Shepherd";
17+
}
18+
19+
export interface Cat {
20+
hunts?: boolean;
21+
age?: number;
22+
}
23+
24+
export type RequestParams = Omit<RequestInit, "body" | "method"> & {
25+
secure?: boolean;
26+
}
27+
28+
type ApiConfig<SecurityDataType> = {
29+
baseUrl?: string,
30+
baseApiParams?: RequestParams,
31+
securityWorker?: (securityData: SecurityDataType) => RequestParams,
32+
}
33+
34+
35+
36+
export class Api<SecurityDataType> {
37+
38+
public baseUrl = "";
39+
public title = "Oneof Example";
40+
public version = "1.0.0";
41+
42+
private securityData: SecurityDataType = (null as any);
43+
private securityWorker: ApiConfig<SecurityDataType>["securityWorker"] = (() => {}) as any
44+
45+
private baseApiParams: RequestParams = {
46+
credentials: 'same-origin',
47+
headers: {
48+
'Content-Type': 'application/json'
49+
},
50+
redirect: 'follow',
51+
referrerPolicy: 'no-referrer',
52+
}
53+
54+
constructor({ baseUrl,baseApiParams,securityWorker, }: ApiConfig<SecurityDataType> = {}) {
55+
this.baseUrl = baseUrl || this.baseUrl;
56+
this.baseApiParams = baseApiParams || this.baseApiParams;
57+
this.securityWorker = securityWorker || this.securityWorker;
58+
}
59+
60+
public setSecurityData = (data: SecurityDataType) => {
61+
this.securityData = data
62+
}
63+
64+
65+
private mergeRequestOptions(params: RequestParams, securityParams?: RequestParams): RequestParams {
66+
return {
67+
...this.baseApiParams,
68+
...params,
69+
...(securityParams || {}),
70+
headers: {
71+
...(this.baseApiParams.headers || {}),
72+
...(params.headers || {}),
73+
...((securityParams && securityParams.headers) || {})
74+
}
75+
}
76+
}
77+
78+
private safeParseResponse = <T = any>(response: Response): Promise<T> =>
79+
response.json()
80+
.then(data => data)
81+
.catch(e => response.text);
82+
83+
public request = <T = any>(
84+
path: string,
85+
method: string,
86+
{ secure, ...params }: RequestParams = {},
87+
body?: any,
88+
secureByDefault?: boolean,
89+
): Promise<T> =>
90+
fetch(`${this.baseUrl}${path}`, {
91+
// @ts-ignore
92+
...this.mergeRequestOptions(params, (secureByDefault || secure) && this.securityWorker(this.securityData)),
93+
method,
94+
body: body ? JSON.stringify(body) : null,
95+
}).then(async response => {
96+
const data = await this.safeParseResponse<T>(response);
97+
if (!response.ok) throw data
98+
return data
99+
})
100+
101+
102+
103+
pets = {
104+
105+
106+
/**
107+
* @name patch
108+
* @request PATCH:/pets
109+
*/
110+
patch: (data: Cat | Dog, params?: RequestParams) =>
111+
this.request<any>(`/pets`, "PATCH", params, data),
112+
}
113+
114+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: Allof Example
4+
version: 1.0.0
5+
paths:
6+
/pets:
7+
patch:
8+
requestBody:
9+
content:
10+
application/json:
11+
schema:
12+
oneOf:
13+
- $ref: '#/components/schemas/Cat'
14+
- $ref: '#/components/schemas/Dog'
15+
discriminator:
16+
propertyName: pet_type
17+
responses:
18+
'200':
19+
description: Updated
20+
components:
21+
schemas:
22+
Pet:
23+
type: object
24+
required:
25+
- pet_type
26+
properties:
27+
pet_type:
28+
type: string
29+
discriminator:
30+
propertyName: pet_type
31+
Dog: # "Dog" is a value for the pet_type property (the discriminator value)
32+
allOf: # Combines the main `Pet` schema with `Dog`-specific properties
33+
- $ref: '#/components/schemas/Pet'
34+
- type: object
35+
# all other properties specific to a `Dog`
36+
properties:
37+
bark:
38+
type: boolean
39+
breed:
40+
type: string
41+
enum: [Dingo, Husky, Retriever, Shepherd]
42+
Cat: # "Cat" is a value for the pet_type property (the discriminator value)
43+
allOf: # Combines the main `Pet` schema with `Cat`-specific properties
44+
- $ref: '#/components/schemas/Pet'
45+
- type: object
46+
# all other properties specific to a `Cat`
47+
properties:
48+
hunts:
49+
type: boolean
50+
age:
51+
type: integer
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: Oneof Example
4+
version: 1.0.0
5+
paths:
6+
/pets:
7+
patch:
8+
requestBody:
9+
content:
10+
application/json:
11+
schema:
12+
oneOf:
13+
- $ref: '#/components/schemas/Cat'
14+
- $ref: '#/components/schemas/Dog'
15+
responses:
16+
'200':
17+
description: Updated
18+
components:
19+
schemas:
20+
Dog:
21+
type: object
22+
properties:
23+
bark:
24+
type: boolean
25+
breed:
26+
type: string
27+
enum: [Dingo, Husky, Retriever, Shepherd]
28+
Cat:
29+
type: object
30+
properties:
31+
hunts:
32+
type: boolean
33+
age:
34+
type: integer

0 commit comments

Comments
 (0)