Skip to content

[typescript-angular] URLSearchParams does not allow to pass '+' character  #6306

Closed
@taxpon

Description

@taxpon
Description

As reported in angular/angular#11058, URLSearchParams does not allow to pass '+' character. Currently I am developing a search interface with email and email can contain '+' character obviously like foo+bar@example.com. So this behavior is problem for me.

Swagger-codegen version

Latest master

Suggest a fix/enhancement

URLSearchParams can receive customized encoder as in the document. My suggestion is implement custom encoder as follows:

/*
 * CustomQueryEncoderHelper
 * Fix plus sign (+) not encoding, so sent as blank space
 * See: https://github.com/angular/angular/issues/11058#issuecomment-247367318
 */
import { QueryEncoder } from "@angular/http";

export class CustomQueryEncoderHelper extends QueryEncoder {
    encodeKey(k: string): string {
        k = super.encodeKey(k);
        return k.replace(/\+/gi, '%2B');
    }
    encodeValue(v: string): string {
        v = super.encodeValue(v);
        return v.replace(/\+/gi, '%2B');
    }
}

And update URLSearchParams in api.service.mustache as follows.

let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());

This code is cited from this comment in angular/angular#11058.

I will file a PR soon if there is no problem.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions