Skip to content

[BUG][CSHARP] FormParameters in multipart/form-data are not sent correctly without a file #20347

Open
@lewishazell

Description

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I've been generating a C# SDK client which uses multipart/form-data requests in a few places, but ran into trouble when the method had no file parameters, which works in tools like Swashbuckle.

The C# generator produces code which sends the following payload when calling the operation in the example YAML:

POST /api/v1/post HTTP/1.1
Host: localhost:8080
User-Agent: OpenAPI-Generator%2F1.0.0%2Fcsharp
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
Accept-Encoding: gzip, deflate, br
Content-Type: multipart/form-data
Content-Length: 68

content=%22Hello%2C%20world%21%20This%20is%20an%20example%20post.%22

This is not valid multipart/form-data and the server refuses to parse it. A valid payload would be:

POST /api/v1/post HTTP/1.1
Host: localhost:8080
User-Agent: OpenAPI-Generator%2F1.0.0%2Fcsharp
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
Accept-Encoding: gzip, deflate, br
Content-Type: multipart/form-data; boundary="28018ef9-0d90-4a6d-9c1b-d53979972be9"
Content-Length: 211

--28018ef9-0d90-4a6d-9c1b-d53979972be9
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=content

Hello, world! This is an example post.
--28018ef9-0d90-4a6d-9c1b-d53979972be9--
openapi-generator version

7.9.0

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Post API
  version: '1.0'
paths:
  /api/v1/post:
    post:
      operationId: addPost
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
                - content
              type: object
              properties:
                content:
                  type: object
                  description: "This field contains the content of your post."
                  example: |-
                    Hello, world!
                    
                    This is an example post.
            encoding:
              content:
                style: form
                contentType: text/plain
      responses:
        '201':
          description: Created
Generation Details
  • Save example YAML to Post.yml
  • Generate with openapi-generator-cli generate -i Post.yml -g csharp and no additional options
Steps to reproduce
  • Call AddPost to some endpoint, capturing the request (through Wireshark or server)
    var api = new DefaultApi(new Configuration
    {
        BasePath = "http://localhost:8080"
    });
    
    api.AddPost("Hello, world! This is an example post.");
  • Observe the body of the request, it is a regular POST form with parameters and not multipart
Suggest a fix

If Content-Type is multipart/form-data then AlwaysMultipartFormData should be set to true on the RestRequest. Also, the parameters should not be serialized in multipart/form-data requests as this will cause issues. See #20351.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions