RicoSuter / NSwag

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

NSwag studio incorrectly generates empty body #4927

Open wim11be opened 1 week ago

wim11be commented 1 week ago

I have a spec, where a PUT request has no request body, only a response body defined:

'/redacted':
    put:
      security:
        - BearerAuth: []
      parameters:
        - description: Redacted
          in: path
          name: user
          required: true
          schema:
            $ref: '#/components/schemas/ObjectId'
        - description: The ID of the segment
          in: path
          name: segment
          required: true
          schema:
            $ref: '#/components/schemas/ObjectId'
      responses:
        '200':
          content:
            application/json; charset=utf-8:
              schema:
                $ref: '#/components/schemas/ResponseMessage'
              example:
                message: user removed from segment
          description: OK

Nswag studio generates an empty body for the content with the content type of the response. The generated code looks like this: request_.Content = new System.Net.Http.StringContent(string.Empty, System.Text.Encoding.UTF8, "application/json; charset=utf-8"); This causes an exception since the Encoding and mediatype are to be separately defined in the StringContent constructor. Removing the charset=utf-8 manually solved the issue, but I worry that this error will sneak in again when a new spec is generated. Could you solve this issue?

Kind regards

idea-lei commented 1 week ago

same here https://github.com/RicoSuter/NSwag/issues/4919