forked from loafle/openapi-generator-original
feature: implement deepObject query params as per documentation.
Closes OpenAPITools/openapi-generator#19342.
This commit is contained in:
@@ -28,21 +28,30 @@ export class BaseService {
|
||||
return consumes.indexOf('multipart/form-data') !== -1;
|
||||
}
|
||||
|
||||
protected addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
|
||||
protected addToHttpParams(httpParams: HttpParams, value: any, key?: string, isDeep?: boolean): HttpParams {
|
||||
// If the value is an object (but not a Date), recursively add its keys.
|
||||
if (typeof value === 'object' && !(value instanceof Date)) {
|
||||
if (isDeep) {
|
||||
return this.addToHttpParamsRecursive(httpParams, value, key, isDeep);
|
||||
}
|
||||
return this.addToHttpParamsRecursive(httpParams, value);
|
||||
}
|
||||
return this.addToHttpParamsRecursive(httpParams, value, key);
|
||||
}
|
||||
|
||||
protected addToHttpParamsRecursive(httpParams: HttpParams, value?: any, key?: string): HttpParams {
|
||||
protected addToHttpParamsRecursive(httpParams: HttpParams, value?: any, key?: string, isDeep?: boolean): HttpParams {
|
||||
if (value === null || value === undefined) {
|
||||
return httpParams;
|
||||
}
|
||||
if (typeof value === 'object') {
|
||||
// If JSON format is preferred, key must be provided.
|
||||
if (key != null) {
|
||||
if (isDeep) {
|
||||
return Object.entries(value as Record<string, any>).reduce(
|
||||
(hp, [k, v]) => hp.append(`${key}[${k}]`, v),
|
||||
httpParams,
|
||||
);
|
||||
}
|
||||
return httpParams.append(key, JSON.stringify(value));
|
||||
}
|
||||
// Otherwise, if it's an array, add each element.
|
||||
|
||||
@@ -130,7 +130,7 @@ export class {{classname}} extends BaseService {
|
||||
{{/isArray}}
|
||||
{{^isArray}}
|
||||
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
|
||||
<any>{{paramName}}, '{{baseName}}');
|
||||
<any>{{paramName}}, '{{baseName}}'{{#isDeepObject}}, true{{/isDeepObject}});
|
||||
{{/isArray}}
|
||||
{{/queryParams}}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user