RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

Wrong body serialization for Typescript client with binary content #1856

Open Euler-KB opened 5 years ago

Euler-KB commented 5 years ago

I'm generating a Typescript client for an endpoint with binary type and format.

Unfortunately the body is being serialized to json...

` updateProfilePhoto(id: number, body: any): Promise { let url = this.baseUrl + "/api/farmers/update/profile/photo/{id}"; if (id === undefined || id === null) throw new Error("The parameter 'id' must be defined."); url = url.replace("{id}", encodeURIComponent("" + id)); url = url_.replace(/[?&]$/, "");

    **const content_ = JSON.stringify(body);**

    let options_ = <RequestInit>{
        body: content_,
        method: "PUT",
        headers: {
            "Content-Type": "application/octet-stream", 
            "Accept": "application/json"
        }
    };

    return this.http.fetch(url_, options_).then((_response: Response) => {
        return this.processUpdateProfilePhoto(_response);
    });
}`
Euler-KB commented 5 years ago

This line content_ = JSON.stringify(body); shouldn't be

RicoSuter commented 5 years ago

Can you post a sample swagger spec?

Euler-KB commented 5 years ago

"/api/farmers/update/profile/photo/{id}":{"put":{"tags":["Farmers"],"operationId":"Farmers_UpdateProfilePhoto","consumes":["application/octet-stream"],"produces":["application/json","text/json"],"parameters":[{"name":"id","in":"path","required":true,"type":"integer","format":"int32"},{"name":"body","in":"body","description":"The content of the file","required":true,"type":"binary","format":"binary"}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/MediaModel"}},"201":{"description":"Created","schema":{"$ref":"#/definitions/MediaModel"}},"404":{"description":"Invalid request response","schema":{"$ref":"#/definitions/BasicResponse"}},"500":{"description":"Internal server error","schema":{"$ref":"#/definitions/BasicResponse"}}}}}