Skip to content

[BUG][typescript-angular] 7.12.0 refactoring changes behaviour of http params #20998

@gratzl-dev

Description

@gratzl-dev

Description

JS Objects used to be flattened into individual parameters. Now they just get JSON.stringified.

Example:
Page Object:

{
   page: 1,
   items: 10
}

7.11.0

/api/request?page=1&items=10

7.12.0

/api/request?page={page:1,items:10}

There is no config to set the behaviour to how it was previously.

openapi-generator version

7.11.0

    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().substring(0, 10));
                } 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}.${k}` : 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;
    }

7.12.0

   protected addToHttpParamsRecursive(httpParams: HttpParams, value?: any, key?: string): HttpParams {
        if (value === null || value === undefined) {
            return httpParams;
        }
        if (typeof value === 'object') {
            // If JSON format is preferred, key must be provided.
            if (key != null) {
                return httpParams.append(key, JSON.stringify(value));
            }
            // Otherwise, if it's an array, add each element.
            if (Array.isArray(value)) {
                value.forEach(elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
            } else if (value instanceof Date) {
                if (key != null) {
                    httpParams = httpParams.append(key, value.toISOString());
                } else {
                    throw Error("key may not be null if value is Date");
                }
            } else {
                Object.keys(value).forEach(k => {
                    const paramKey = key ? `${key}.${k}` : k;
                    httpParams = this.addToHttpParamsRecursive(httpParams, value[k], paramKey);
                });
            }
            return httpParams;
        } else if (key != null) {
            return httpParams.append(key, value);
        }
        throw Error("key may not be null if value is not object or array");
    }
Steps to reproduce

Run openapi-generator with the latest version

Related issues/PRs

This merge causes the issue:
#20681

Suggest a fix

I would suggest to either add a config to enable/disable json http params, or revert the addToHttpParamsRecursive method to how it was previously

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions