RicoSuter/NSwag

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

Open

#1,338 创建于 2018年5月22日

在 GitHub 查看
 (5 评论) (0 反应) (0 负责人)C# (6,291 star) (1,189 fork)batch import
help wantedproject: NSwag.CodeGeneration.TypeScripttype: enhancement

描述

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"
            })
        };

贡献者指南

Trouble with typescript generator for x-www-form-urlencoded endpoint · RicoSuter/NSwag#1338 | Good First Issue