RicoSuter/NSwag

Trouble with typescript generator for x-www-form-urlencoded endpoint

Open

#1,338 opened on May 22, 2018

View on GitHub
 (5 comments) (0 reactions) (0 assignees)C# (1,189 forks)batch import
help wantedproject: NSwag.CodeGeneration.TypeScripttype: enhancement

Repository metrics

Stars
 (6,291 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

For an OAuth compliant endpoint using formData parameters the data is expected to be in application/x-www-form-urlencoded format which looks like this:

grant_type=password&client_id=123456&username=test&password=test

However the typescript generator is using JSON.stringify which generates something like this:

{'grand_type':'password','client_id':'123456','username':'test','password':'test'}

https://github.com/RSuter/NSwag/blob/master/src/NSwag.CodeGeneration.TypeScript/Templates/Client.RequestBody.liquid#L26

How can I go about replacing the call to JSON.stringify() with something else? Can I provide a custom Client.RequestBody.liquid template to nswag?

I need to replace the JSON.stringify() with something like this:

    token(token: OAuth2Request): Observable<SimpleAccessTokenResult> {
        let url_ = this.baseUrl + "/api/public/v1/oauth2/token";

        //const content_ = JSON.stringify(token);

        const content_ = Object.keys(token).map((key) => {
            return encodeURIComponent(key) + '=' + encodeURIComponent(token[key]);
        }).join('&');        

        let options_ : any = {
            body: content_,
            observe: "response",
            responseType: "blob",
            headers: new HttpHeaders({
                "Content-Type": "application/x-www-form-urlencoded", 
                "Accept": "application/json"
            })
        };

Contributor guide