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

Wrong schema generation on endpoint consuming multipart form data combined with JsonView #1829

Closed
PiotrKaaminski opened this issue Sep 2, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@PiotrKaaminski
Copy link

Describe the bug
When using method consuming MediaType.MULTIPART_FORM_DATA_VALUE, schemas for RequestPart objects are generated based on JsonView from method level, which should be related only to response schema, instead of parameter level

To Reproduce
Steps to reproduce the behavior:

  • spring-boot version: 2.7.3
  • springodc-openapi-ui version: 1.6.11

HelloController:

@PostMapping(value = "/foo", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@JsonView(ViewB.class)
public Foo postFoo(@RequestPart("input") @JsonView(ViewA.class) Foo foo) {
    return null;
}

Foo:

@Data
public class Foo {
    @JsonView(ViewA.class)
    private String a;
    @JsonView(ViewB.class)
    private String b;
}

/v3/api-docs:

paths": {
    "/foo": {
      "post": {
        "tags": [
          "hello-controller"
        ],
        "operationId": "postFoo",
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "required": [
                  "input"
                ],
                "type": "object",
                "properties": {
                  "input": {
                    "$ref": "#/components/schemas/Foo_ViewB"
         }}}}}},
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "*/*": {
                "schema": {
                  "$ref": "#/components/schemas/Foo_ViewB"
        }}}}}}}},
  "components": {
    "schemas": {
      "Foo_ViewB": {
        "type": "object",
        "properties": {
          "b": {
            "type": "string"
   }}}}}

Analogically, when no JsonView annotation is present on method level then the only generated shema is Foo with properties a and b and both input and response objects refer to that schema, ignoring JsonView(ViewA.class) on parameter

Expected behavior

  • 2 schemas are generated: Foo_ViewA with property a and Foo_ViewB with property b
  • response schema should refer to Foo_ViewB and input object should refer to Foo_ViewA
@mdjimy
Copy link

mdjimy commented Sep 8, 2022

I have similar issue, since version 1.6.10 all query Parameters on endpoint that consumes multipart/form-data are incorrectly reported as Request body parts.

Example:

@PostMapping(value = "/bitmap", consumes = "multipart/form-data", produces = "application/json")
public ImageInfoDTO uploadImage(
    @Parameter(description = "Uploaded image", required = true) @RequestPart MultipartFile file,
    @Parameter(description = "File access restriction (default is OWNER_ONLY)") @RequestParam Access access
) { ... }

Produces:

{
  "paths": {
    "/bitmap": {
      "post": {
        "tags": [
          "Image"
        ],
        "operationId": "uploadImage",
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "required": [
                  "file"
                ],
                "type": "object",
                "properties": {
                  "file": {
                    "type": "string",
                    "description": "Uploaded image",
                    "format": "binary"
                  },
                  "access": {
                    "description": "File access restriction (default is OWNER_ONLY)"
                  }
                }
              }
            }
          }
        },
        "responses": {...}
      }
    }
  }
}

Expected behaviour:
Query parameter access should be listed within parameters and not as a part of requestBody schema

@PiotrKaaminski
Copy link
Author

PiotrKaaminski commented Sep 11, 2022

Same still on 1.6.11, I have discovered that just yesterday

@bnasslahsen
Copy link
Contributor

Workaround is to use:

springdoc.default-support-form-data=false

This will be the default behavior, starting from 1.6.12

@bnasslahsen bnasslahsen added the bug Something isn't working label Oct 14, 2022
bnasslahsen added a commit that referenced this issue Oct 14, 2022
@PiotrKaaminski
Copy link
Author

Issue reported in comment by mdjimy is indeed fixed, but what about my issue with wrong schema when using JsonView?
I tried suggested workaround and upgraded to version 1.6.13 but both seem to not fix this issue, result is still the same

Actual controller that I'm using contains also MultipartFile, so that would be more precise reporduction:

@PostMapping(value = "/foo", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@JsonView(ViewB.class)
public Foo postFoo(@RequestPart("input") @JsonView(ViewA.class) Foo foo, 
                                @RequestPart("file") MultipartFile file) {
    return null;
}

Maybe your fix doesn't cover that case so it would be my fault by providing inaccurate reproduction step so sorry for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants