swagger-api / swagger-api/swagger-codegen

[JAVA][Multipart-Request] Bug in serialising multipart request body with component model as parameter scheme

Open
#10,205 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have request multipart request which deals with file along with some other json values as other parameters. So, i modelled those json values as components(Because it's used in multiple places). Then, i generated the java client using swagger-codegen command line(Also tried with swagger-codegen jar).

When i create make request with java client code, json parameters in the multipart body is not serialised from gson format to json string. Instead gson string is added in multipart body.

Expected multipart request body:

--d553d5e9-8781-4eed-8059-ba81b1966c5f.
Content-Disposition: form-data; name="metadata".
Content-Length: 40.
.
{ "foo": "Bar" }.
--d553d5e9-8781-4eed-8059-ba81b1966c5f.
Content-Disposition: form-data; name="file"; filename="19c3ec4b-0aa2-4034-942e-51819fb85d95-1586513366095.odt".
Content-Type: application/octet-stream.
Content-Length: 21708.
.
----Document Bytes----

Actual Multipart body:

--d553d5e9-8781-4eed-8059-ba81b1966c5f.
Content-Disposition: form-data; name="metadata".
Content-Length: 40.
.
class RequestMetadata {
foo: Bar
}.

--d553d5e9-8781-4eed-8059-ba81b1966c5f.
Content-Disposition: form-data; name="file"; filename="19c3ec4b-0aa2-4034-942e-51819fb85d95-1586513366095.odt".
Content-Type: application/octet-stream.
Content-Length: 21708.
.
----Document Bytes----

Refer below attached screenshot for better understanding of issue that i am talking about.

Screenshot 2020-04-25 at 9 42 20 AM
Swagger-codegen version

swagger-codegen version 3.0.11

Swagger declaration file content
openapi: 3.0.0
info:
  version: 1.0.0
  title: Multipart Request
  description: "Multipart Testing"
  termsOfService: 'terms.jsp'
  contact:
    name: Multipart Request
    email: abc@example.com
    url: ''
servers:
  - url: 'https://api-example.com'
paths:
  /test:
    post:
      summary: Upload a new image and associated metadata
      description: Long form description saying whatever we want
      operationId: upload
      requestBody:
        $ref: '#/components/requestBodies/MultiRequest'
      responses:
        200:
          description: The UUID of the newly uploaded image
        403:
          description: The provided credentials are insufficient to see this resource.
        415:
          description: The provided file type is not supported
        500:
          description: We messed up. Please let us know so we can fix it ASAP.
components:
  requestBodies:
    MultiRequest:
      description: "Multipart Test Object"
      content:
        multipart/form-data:
          schema:
            properties:
              file:
                type: string
                description: image file
                format: binary
              metadata:
                $ref: '#/components/schemas/RequestMetadata'
          encoding:
            file:
              contentType: application/pdf, image/png
  schemas:
    RequestMetadata:
      type: object
      description: image metadata
      properties:
        foo:
          type: string
security:
  - apiKey: []
Command line used for generation

I tried to generate from both command line and swagger GUI editor which runs in docker. Both results in same code & issue.
##Command line code
swagger-codegen generate -i openapi.yml -l java

Steps to reproduce
  • Create java client using above yml code
  • Set proper base path in defaultApi client
  • Make the post request for /test end points using generated java client code
Suggest a fix/enhancement

I have debugged a with generate java client code and found that, For Multipart requests, instance of "Date", "OffsetDateTime" and "LocalDate" values alone serialised. But, here the json parameter value also needs to be serialised to json string.

Screenshot 2020-04-25 at 9 45 26 AM

If we can serialise for parameter of type gson string in multipart params values, then it could solve this issue.

I don't know it's a bug or mistake in my yml syntax. Please point me, if i am missing any syntax or configuration to solve this issue.

Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided OpenAPI YAML and reproduce generation with swagger-codegen generate -i openapi.yml -l java. Inspect the generated Java client's multipart request handling, especially the metadata parameter and the existing serialization behavior for Date, OffsetDateTime, and LocalDate. Done means the generated multipart body contains the metadata as JSON while preserving the file part.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.