feature: implement deepObject query params as per documentation.

Closes OpenAPITools/openapi-generator#19342.
This commit is contained in:
Phil Thomas
2025-04-17 20:15:42 +02:00
parent b4378a6277
commit f5aa790e04
2 changed files with 12 additions and 3 deletions

View File

@@ -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.

View File

@@ -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}}