Bug Report Checklist
Description
Generating the client with queryParamObjectFormat set to json breaks the query string resulting in request urls like:
https://example.com/pizzas?undefined={someJSON} instead of https://example.com/pizzas?filter={someJSON}.
openapi-generator version
v5.0.0-beta2
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: 'Sample-API'
version: '1.0.0'
paths:
/pizzas:
get:
parameters:
- name: filter
in: query
content:
application/json:
schema:
$ref: '#/components/schemas/PizzaFilter'
responses:
200:
description: Delicious pizzas
content:
application/json:
schema:
type: array
items:
type: string
components:
schemas:
PizzaFilter:
type: object
additionalProperties: true
Generation Details
Steps to reproduce
npx @openapitools/openapi-generator-cli version-manager set 5.0.0-beta2
npx @openapitools/openapi-generator-cli generate -i spec.yaml -g typescript-angular -o ./generated --additional-properties=queryParamObjectFormat=json
Related issues/PRs
Suggest a fix
I think the Problem is that the key parameter is not passed here:
|
httpParams = this.addToHttpParamsRecursive(httpParams, value); |
Therefore it is undefined here:
|
httpParams = httpParams.append(key, JSON.stringify(value)); |
A possible fix could be to move the distinction of the query param format to addToHttpParams instead of addToHttpParamsRecursive
private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
if (typeof value === "object" && value instanceof Date === false) {
{{#isQueryParamObjectFormatJson}}
httpParams = httpParams.append(key, JSON.stringify(value));
{{/isQueryParamObjectFormatJson}}
{{^isQueryParamObjectFormatJson}}
httpParams = this.addToHttpParamsRecursive(httpParams, value);
{{/isQueryParamObjectFormatJson}}
} else {
httpParams = this.addToHttpParamsRecursive(httpParams, value, key);
}
return httpParams;
}
private addToHttpParamsRecursive(httpParams: HttpParams, value?: any, key?: string): HttpParams {
if (value == null) {
return httpParams;
}
if (typeof value === "object") {
if (Array.isArray(value)) {
(value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
} else if (value instanceof Date) {
if (key != null) {
httpParams = httpParams.append(key,
(value as Date).toISOString(){{^isDateTime}}.substr(0, 10)){{/isDateTime}};
} else {
throw Error("key may not be null if value is Date");
}
} else {
Object.keys(value).forEach( k => httpParams = this.addToHttpParamsRecursive(
httpParams, value[k], key != null ? `${key}{{#isQueryParamObjectFormatDot}}.{{/isQueryParamObjectFormatDot}}{{#isQueryParamObjectFormatKey}}[{{/isQueryParamObjectFormatKey}}${k}{{#isQueryParamObjectFormatKey}}]{{/isQueryParamObjectFormatKey}}` : k));
}
} else if (key != null) {
httpParams = httpParams.append(key, value);
} else {
throw Error("key may not be null if value is not object or array");
}
return httpParams;
}
Bug Report Checklist
Description
Generating the client with
queryParamObjectFormatset tojsonbreaks the query string resulting in request urls like:https://example.com/pizzas?undefined={someJSON}instead ofhttps://example.com/pizzas?filter={someJSON}.openapi-generator version
v5.0.0-beta2
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
npx @openapitools/openapi-generator-cli version-manager set 5.0.0-beta2 npx @openapitools/openapi-generator-cli generate -i spec.yaml -g typescript-angular -o ./generated --additional-properties=queryParamObjectFormat=jsonRelated issues/PRs
Suggest a fix
I think the Problem is that the key parameter is not passed here:
openapi-generator/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache
Line 97 in d8ba49b
Therefore it is undefined here:
openapi-generator/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache
Line 111 in d8ba49b
A possible fix could be to move the distinction of the query param format to
addToHttpParamsinstead ofaddToHttpParamsRecursive