Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File and FileUpload result in different OpenAPI spec, upload button missing #39950

Closed
BrianSetz opened this issue Apr 8, 2024 · 7 comments · Fixed by smallrye/smallrye-open-api#1797

Comments

@BrianSetz
Copy link

Describe the bug

The following:

@POST
@Path("/file")
fun postFile(@RestForm file: File): String {
    // Do nothing
    return "File"
}

@POST
@Path("/fileupload")
fun postFileUpload(@RestForm fileUpload: FileUpload): String {
    // Do nothing
    return "FileUpload"
}

Results in different OpenAPI specs for each endpoint, the upload button is missing in the SwaggerUI for the FileUpload endpoint.

File endpoint:

image

FileUpload endpoint:

image

I am not sure if this is intended behavior?

Expected behavior

I expected to be able to use File and FileUpload interchangeably (from OpenAPI point of view).

Actual behavior

File and FileUpload endpoints have vastly different OpenAPI specs. When using FileUpload there is no upload button in the SwaggerUI. When using File there is a file upload button.

How to Reproduce?

Reproducer: https://github.com/BrianSetz/quarkus-file-fileupload-reproducer (or use the above endpoint definitions in a Quarkus project with Reactive REST and OpenAPI)

Steps to reproduce the behavior:

  1. Run Quarkus app
  2. Open Quarkus Dev UI
  3. Navigate to Swagger UI
  4. Compare endpoints / OpenAPI spec

Output of uname -a or ver

Linux p15v 6.5.0-26-generic #26-Ubuntu SMP PREEMPT_DYNAMIC Tue Mar 5 21:19:28 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Output of java -version

Java 17

Quarkus version or git rev

3.9.2

Build tool (ie. output of mvnw --version or gradlew --version)

Gradle 8.6

Additional information

OpenAPI spec:

---
openapi: 3.0.3
info:
  title: demo API
  version: 1.0-SNAPSHOT
paths:
  /hello:
    get:
      tags:
      - Example Resource
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type: string
  /hello/file:
    post:
      tags:
      - Example Resource
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              required:
              - file
              type: object
              properties:
                file:
                  format: binary
                  type: string
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type: string
  /hello/fileupload:
    post:
      tags:
      - Example Resource
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                fileUpload:
                  $ref: '#/components/schemas/FileUpload'
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type: string
components:
  schemas:
    FileUpload:
      type: object
Copy link

quarkus-bot bot commented Apr 8, 2024

/cc @EricWittmann (openapi), @MikeEdgar (openapi), @geoand (kotlin), @phillip-kruger (openapi)

@ramiswailem
Copy link

You need something like this:

@Schema(type = SchemaType.STRING, format = "binary")
public interface UploadItemSchema {
}

public Response upload(@restform(FileUpload.ALL) @Schema(implementation = UploadItemSchema.class) List uploads) throws IOException {....

@geoand
Copy link
Contributor

geoand commented Apr 8, 2024

Indeed FileUpload should be treated like File

@BrianSetz
Copy link
Author

I can confirm that the workaround at least fixes the incorrect type/format.

@POST
@Path("/fileupload")
fun postFileUpload(@RestForm @Schema(implementation = BinaryStringSchema::class) fileUpload: FileUpload): String {
    // Do nothing
    return "FileUpload"
}

@Schema(type = SchemaType.STRING, format = "binary")
interface BinaryStringSchema

Results in

---
openapi: 3.0.3
info:
  title: demo API
  version: 1.0-SNAPSHOT
paths:
  /hello:
    get:
      tags:
      - Example Resource
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type: string
  /hello/file:
    post:
      tags:
      - Example Resource
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              required:
              - file
              type: object
              properties:
                file:
                  format: binary
                  type: string
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type: string
  /hello/fileupload:
    post:
      tags:
      - Example Resource
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                fileUpload:
                  $ref: '#/components/schemas/BinaryStringSchema'
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type: string
components:
  schemas:
    BinaryStringSchema:
      format: binary
      type: string
    FileUpload:
      type: object

Which at least adds the upload button in the Swagger UI. But the following is missing

required:
- fileUpload

And there is an unused (?) schema component:

FileUpload:
  type: object

@phillip-kruger
Copy link
Member

This has been fixed in upstream smallrye, @MikeEdgar please close here once new version of SmallRye openapi is included in Quarkus. Thanks :)

@MikeEdgar
Copy link
Contributor

The fix for this was included in #41037

@geoand
Copy link
Contributor

geoand commented Sep 29, 2024

🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants