swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.37k stars 2.17k forks source link

Define encoding type for multipart/form-data file upload is not possible #3387

Open sameena-s opened 4 years ago

sameena-s commented 4 years ago

I would like to generate the openapi json in the following format.

Capture2

Used Maven dependencies.

MavenDependencies

Used plugin to generate the OpenApi defination:

plugin

Here's my expected YAML format.

Expected

But I can able to get this in the following format(without properties).

Actual

Tried with different possible scenario's, but no use. Following file contains all the scenario's which I have tried. IOpenApiRestService.txt

Can someone please explain which annotations have to use exactly to generate the YAML in the expected format?

Thanks in adavance.

zfkang-talend commented 3 years ago

my request is like same with you, my issue is the Content-Type is not my defined. below is my code: POJO:

public class ResourceRequest {
    @Schema(type = "object ", description = "Resource detail", name = "resource",
            implementation = CreateResourceRequest.class,
            required = true)
    private CreateResourceRequest resource;
    @Schema(type = "string", description = "Resource attachment", name = "file", required = true,
            format = "binary", title = "file")
    private File file;
}

Here: the file is must be File Object, I had tried other object, didn't display with the selected file. The API Definition:

@POST
@Consumes({"multipart/form-data"})
@Produces({"application/json"})
@Operation(summary = "Create a Resource", tags = "resources", requestBody = @RequestBody(
    content = @Content(mediaType = "multipart/form-data", schema = @Schema(type = "object",
        requiredProperties = {"resource", "file"}, implementation = ResourceRequest.class),
        encoding = {@Encoding(name = "resource", contentType = "application/json"),
        @Encoding(name = "file", contentType = "application/octet-stream")}
    )
))
Resource doSomeThing(
        @Multipart(value = "resource", type = "application/json") CreateResourceRequest resourceRequest,
        @Multipart(value = "file", type = "application/octet-stream") Attachment fileDetail
)

The yaml file:

openapi: 3.0.1
info:
  version: "2.5"
servers:
- url: /v2.5
  variables: {}
security:
- Authorization: []
paths:
  /resources:
    post:
      tags:
      - resources
      summary: Create a Resource
      operationId: createResource
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/ResourceRequest'
            encoding:
              resource:
                contentType: application/json
              file:
                contentType: application/octet-stream
components:
  schemas:
    CreateResourceRequest:
      type: object
      properties:
        name:
          type: string
        description:
          type: string
        workspaceId:
          type: string
        file:
          type: boolean
    ResourceRequest:
      type: object
      properties:
        resource:
          $ref: '#/components/schemas/CreateResourceRequest'
        file:
          type: string
          format: binary
          writeOnly: true
  securitySchemes:
    Authorization:
      type: http
      in: header
      scheme: bearer
      bearerFormat: jwt

Used this file to swagger editor, and execute it. the Content-Type is not my defined. and execute in sever, throw the 415, not supported, because the parameter Content-Type is not correct.

curl -X 'POST' \
  'https://editor.swagger.io/v2.5/resources' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'resource={
    "name":"new_file_U",
    "description":"simple file_U"
}' \
  -F 'file=@aa.txt;type=text/plain'