swagger-api / swagger-api/swagger-codegen

[Spring] OpenApi 3 File upload issue

Open
#9,010 3 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

Spring generated code for a openapi3 file upload wants a java.io.File for the parameter. In swagger 2 it generates a MultipartFile. The issue with this is when i curl a request to the server, spring reads the request and makes a MultipartFile and the request fails.

Swagger-codegen version

Used version 3.0.3 and 3.0.4-SNAPSHOT

Swagger declaration file content or url

Open Api 3 example yml

openapi: 3.0.2
info:
  description: Api Documentation for the Test service
  version: '2.0'
  title: Test Service
tags:
  - name: Test
    description: Test
paths:
  /Test:
    post:
      tags:
        - Test
      summary: Create the specified file
      operationId: createfile
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                orderId:
                  $ref: '#/components/schemas/metadataDto'
                fileName:
                  type: string
                  format: binary
      responses:
        '201':
          description: Successfuly created
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
components:
  schemas:
    metadataDto:
      type: object
      properties:
        thing:
          type: string
        thing2:
          type: string

Swagger 2 file

tags:
  - name: Testservice
info:
  version: 0.0.0
  title: title
  description: description
  termsOfService: terms
paths:
  /test/:
    post:
      tags:
        - Testservice
      consumes:
        - multipart/form-data
      parameters:
        - name: file
          in: formData
          required: true
          type: file
        - name: thing
          in: query
          required: false
          type: string
        - name: thing2
          in: query
          required: false
          type: string
      responses:
        '200':
          description: successful operation
Command line used for generation

java -jar swagger-codegen-cli-3.0.3.jar generate -i swagger2.yml -l spring -o srcDir

Steps to reproduce

Generate the spring server code.
Start the server
Curl the server with a file and object and the request will fail

Related issues/PRs
Suggest a fix/enhancement

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 spring server generation path invoked by swagger-codegen-cli with -l spring, then reproduce the OpenAPI 3 multipart/form-data example by generating and running the server. Compare the generated upload parameter handling with the Swagger 2 example; done means a curl request containing the file and object is accepted successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend, 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.