Skip to content

Commit f76978c

Browse files
authored
Merge pull request #172 from sebgroup/develop
fix open api inconsistent handling of mocks
2 parents df3da2e + 28be244 commit f76978c

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

src/openapiGenerator/mockGenerator/formatResponse.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ export const extractResponses = (obj: OpenAPI.Document): ResponsesType => {
2828
const response: OpenAPIV3.ParameterObject | OpenAPIV2.ParameterObject = responses[statusCode];
2929
const defaultReturn: string = response.description;
3030
let mock: any = response.schema || defaultReturn;
31+
let isExample: boolean = false;
3132
if (response.content && response.content[APPLICATION_JSON]?.example) {
3233
mock = response.content[APPLICATION_JSON].example;
34+
isExample = true;
3335
} else if (response.content && response.content[APPLICATION_JSON]?.schema) {
3436
mock = response.content[APPLICATION_JSON].schema;
35-
mock = typeof(mock) === "string" ? mock : generateData("", mock, modelSchemas);
3637
}
38+
mock = typeof(mock) === "string" || isExample ? mock : generateData("", mock, modelSchemas);
3739
extracted[key][statusCode] = mock;
3840
});
3941
});
4042
});
4143
return extracted;
42-
};
44+
};

src/openapiGenerator/templates/ts-angular/apiInner.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class {{classname}} extends APIHandler {
3434
const formData: FormData = new FormData();
3535
formData.append("file", params.{{paramName}} as any);{{/isFile}}{{/formParams}}
3636
const body: any = {{#formParams}}{{#-first}}{ {{/-first}}{{#isFile}}formData{{/isFile}}{{^isFile}}{{paramName}}: params.{{paramName}}{{/isFile}}{{#-last}} }{{/-last}}{{^-last}}, {{/-last}}{{/formParams}}{{^formParams}}{{#bodyParam}}params.{{paramName}}{{/bodyParam}}{{/formParams}}{{^formParams}}{{^bodyParam}}null{{/bodyParam}}{{/formParams}};
37-
return this.makeCall<{{#imports}}{{#.}}{{.}}{{^-last}} | {{/-last}}{{/.}}{{/imports}}{{^imports}}any{{/imports}}>("{{httpMethod}}".toLowerCase(), apiURL, options, body);
37+
return this.makeCall<{{#imports}}{{#.}}{{.}}{{^-last}} | {{/-last}}{{/.}}{{/imports}}{{^imports}}any{{/imports}}>("{{httpMethod}}".toLowerCase(), apiURL, "{{nickname}}", options, body);
3838
}
3939

4040
{{/operation}}

src/openapiGenerator/templates/ts-angular/baseApi.mustache

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ export class VoidAuth implements Authentication {
175175
* simulate data from mock
176176
*/
177177
export function simulateData<T>(subscriber: Subscriber<HttpResponse<T>>, body: T, status: number, timeout: number): NodeJS.Timeout {
178-
return setTimeout(() => { subscriber.next(new HttpResponse<T>({ status, body })); }, timeout);
178+
return setTimeout(() => {
179+
subscriber.next(new HttpResponse<T>({ status, body }));
180+
subscriber.complete();
181+
}, timeout);
179182
}
180183

181184
export class APIHandler {
@@ -342,11 +345,16 @@ export class APIHandler {
342345
* @param options The http options to be sent
343346
* @param body The body of the request, if any
344347
*/
345-
protected makeCall<T>(method: string, url: string, options: HttpOptions, body?: any): Observable<any> {
348+
protected makeCall<T>(method: string, url: string, operationId: string, options: HttpOptions, body?: any): Observable<any> {
346349
if (configs.useMock) {
347350
return new Observable((subscriber: Subscriber<HttpResponse<T>>) => {
348-
simulateData(subscriber, mocks[this.className][method][200] || mocks[this.className][method]["default"] || mocks[this.className][method], 200, configs.delay)
349-
subscriber.complete();
351+
let mockedData: any = {};
352+
try {
353+
mockedData = mocks[operationId][200] || mocks[operationId]["default"] || mocks[operationId];
354+
} catch (error) {
355+
console.error(error);
356+
}
357+
simulateData(subscriber, mockedData, 200, configs.delay)
350358
});
351359
} else {
352360
switch (method) {

src/openapiGenerator/templates/typescript-axios/apiInner.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class {{classname}} extends APIHandler {
7979
const formData: FormData = new FormData();
8080
formData.append("file", params.{{paramName}} as any);{{/isFile}}{{/formParams}}
8181
const body: any = {{#formParams}}{{#-first}}{ {{/-first}}{{#isFile}}formData{{/isFile}}{{^isFile}}{{paramName}}: params.{{paramName}}{{/isFile}}{{#-last}} }{{/-last}}{{^-last}}, {{/-last}}{{/formParams}}{{^formParams}}{{#bodyParam}}params.{{paramName}}{{/bodyParam}}{{/formParams}}{{^formParams}}{{^bodyParam}}null{{/bodyParam}}{{/formParams}};
82-
return this.makeCall<{{#imports}}{{#.}}{{.}}{{^-last}} | {{/-last}}{{/.}}{{/imports}}>("{{httpMethod}}".toLowerCase(), apiURL, params.options, body, "{{nickname}}");
82+
return this.makeCall<{{#imports}}{{#.}}{{.}}{{^-last}} | {{/-last}}{{/.}}{{/imports}}>("{{httpMethod}}".toLowerCase(), apiURL, "{{nickname}}", params.options, body, "{{nickname}}");
8383
}
8484

8585
{{/operation}}

src/openapiGenerator/templates/typescript-axios/baseApi.mustache

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,15 @@ export class APIHandler {
192192
* @param body The body of the request, if any
193193
* @param methodName The name of the api method making this call
194194
*/
195-
protected makeCall<T>(method: string, url: string, options: AxiosRequestConfig, body: any = null, methodName: string): AxiosPromise<T> {
195+
protected makeCall<T>(method: string, url: string, operationId: string, options: AxiosRequestConfig, body: any = null, methodName: string): AxiosPromise<T> {
196196
if (configs.useMock) {
197-
return new Promise((resolve) => { simulateData(resolve, mocks[this.className][methodName][200] || mocks[this.className][methodName]["default"] || mocks[this.className][methodName], 200, {{#configPath}}{{configName}}{{/configPath}}{{^configPath}}configs{{/configPath}}.delay); });
197+
let mockedData: any = {};
198+
try {
199+
mockedData = mocks[operationId][200] || mocks[operationId]["default"] || mocks[operationId];
200+
} catch (error) {
201+
console.error(error);
202+
}
203+
return new Promise((resolve) => { simulateData(resolve, mockedData, 200, {{#configPath}}{{configName}}{{/configPath}}{{^configPath}}configs{{/configPath}}.delay); });
198204
} else {
199205
switch (method) {
200206
case "get": return this.axiosInstance.get<T>(url, options);

0 commit comments

Comments
 (0)