Skip to content

Commit 72a7b14

Browse files
committed
feat(api-client): generating response for "lowest" of HTML success codes
1 parent b7e9fbb commit 72a7b14

File tree

55 files changed

+1052
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1052
-197
lines changed

src/parser.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ function parseMethods({paths, security, parameters, responses = {}}: Swagger, sw
9494
return supportedMethods.indexOf(methodType.toUpperCase()) !== -1 && // skip unsupported methods
9595
(!swaggerTag || (op.tags && op.tags.includes(swaggerTag))); // if tag is defined take only paths including this tag
9696
}).map(([methodType, operation]: [string, Operation]) => {
97-
const okResponse: Response | Reference = operation.responses['200'] || operation.responses['201'];
97+
// select the lowest success (code 20x) response
98+
const successResponseCode =
99+
Object.keys(operation.responses).sort().filter(code => code.startsWith('2'))[0]
100+
|| 'missing';
101+
102+
const okResponse: Response | Reference = operation.responses[successResponseCode];
98103

99104
const responseType = determineResponseType(
100105
okResponse && isReference(okResponse)
@@ -120,7 +125,8 @@ function parseMethods({paths, security, parameters, responses = {}}: Swagger, sw
120125
(_: string, ...args: string[]): string => `\${args.${toCamelCase(args[0])}}`),
121126
responseTypeName: responseType.name,
122127
response: prefixImportedModels(responseType.type),
123-
description: replaceNewLines(operation.description, '$1 * '),
128+
// tslint:disable-next-line:max-line-length
129+
description: `${replaceNewLines(operation.description, '$1 * ')}${operation.description ? '\n * ' : ''}Response generated for [ ${successResponseCode} ] HTTP response code.`,
124130
.../^(File|Blob)\b/i.test(responseType.name) && {requestResponseType: 'blob' as 'blob'},
125131
};
126132
}
@@ -324,9 +330,8 @@ function determineResponseType(response: Response): {
324330
readonly type: string;
325331
readonly name: string;
326332
} {
327-
if (response == null) { // TODO: check non-200 response codes
328-
logWarn('200 or 201 response not specified; `any` will be used');
329-
return {name: 'any', type: 'any'};
333+
if (response == null ) {
334+
return {name: 'void', type: 'void'};
330335
}
331336

332337
const {schema} = response;

tests/custom/api/api-client.interface.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import * as models from './models';
66

77
export interface APIClientInterface {
88

9+
/**
10+
* Response generated for [ 200 ] HTTP response code.
11+
*/
912
getItems(
1013
args: {
1114
pageSize: number,
@@ -14,6 +17,9 @@ export interface APIClientInterface {
1417
requestHttpOptions?: HttpOptions
1518
): Observable<models.ItemList>;
1619

20+
/**
21+
* Response generated for [ 200 ] HTTP response code.
22+
*/
1723
getItemModels(
1824
args: {
1925
pageSize: number,
@@ -22,20 +28,29 @@ export interface APIClientInterface {
2228
requestHttpOptions?: HttpOptions
2329
): Observable<Object>;
2430

31+
/**
32+
* Response generated for [ 200 ] HTTP response code.
33+
*/
2534
getPetsId(
2635
args: {
2736
id: string,
2837
},
2938
requestHttpOptions?: HttpOptions
3039
): Observable<models.Pet[]>;
3140

41+
/**
42+
* Response generated for [ 200 ] HTTP response code.
43+
*/
3244
deletePetsId(
3345
args: {
3446
id: string,
3547
},
3648
requestHttpOptions?: HttpOptions
3749
): Observable<void>;
3850

51+
/**
52+
* Response generated for [ 200 ] HTTP response code.
53+
*/
3954
getCustomers(
4055
requestHttpOptions?: HttpOptions
4156
): Observable<models.Customer[] | null>;

tests/custom/api/api-client.service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export class APIClient implements APIClientInterface {
4242
};
4343
}
4444

45+
/**
46+
* Response generated for [ 200 ] HTTP response code.
47+
*/
4548
getItems(
4649
args: {
4750
pageSize: number,
@@ -64,6 +67,9 @@ export class APIClient implements APIClientInterface {
6467
return this.sendRequest<models.ItemList>('GET', path, options);
6568
}
6669

70+
/**
71+
* Response generated for [ 200 ] HTTP response code.
72+
*/
6773
getItemModels(
6874
args: {
6975
pageSize: number,
@@ -86,6 +92,9 @@ export class APIClient implements APIClientInterface {
8692
return this.sendRequest<Object>('GET', path, options);
8793
}
8894

95+
/**
96+
* Response generated for [ 200 ] HTTP response code.
97+
*/
8998
getPetsId(
9099
args: {
91100
id: string,
@@ -101,6 +110,9 @@ export class APIClient implements APIClientInterface {
101110
return this.sendRequest<models.Pet[]>('GET', path, options);
102111
}
103112

113+
/**
114+
* Response generated for [ 200 ] HTTP response code.
115+
*/
104116
deletePetsId(
105117
args: {
106118
id: string,
@@ -116,6 +128,9 @@ export class APIClient implements APIClientInterface {
116128
return this.sendRequest<void>('DELETE', path, options);
117129
}
118130

131+
/**
132+
* Response generated for [ 200 ] HTTP response code.
133+
*/
119134
getCustomers(
120135
requestHttpOptions?: HttpOptions
121136
): Observable<models.Customer[] | null> {

0 commit comments

Comments
 (0)