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

Primitive RequestPart parameters generate incorrect Swagger Model #2717

Closed
ghost opened this issue Oct 6, 2018 · 8 comments · Fixed by #2722
Closed

Primitive RequestPart parameters generate incorrect Swagger Model #2717

ghost opened this issue Oct 6, 2018 · 8 comments · Fixed by #2722
Labels

Comments

@ghost
Copy link

ghost commented Oct 6, 2018

Method parameters of the following form will generate incorrect parameters in Swagger 2. Non-validating parsers will not immediately choke on it, but code generators like swagger-codegen or openapi-codegen will.

  public SomeReturnValue webMethod(
    @RequestPart("file") MultipartFile file,
    @RequestPart("description") String description) {
...

The generated spec will look like this:

                    {
                        "name": "file",
                        "in": "formData",
                        "description": "file",
                        "required": true,
                        "type": "file"
                    },
                    {
                        "in": "formData",
                        "name": "description",
                        "description": "description",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }

This is a problem because parameters with in=formData can't have a schema, only a type is allowed. The spec doesn't allow formData parameters to be anything other than a primitive.

The expected JSON should be:

                    {
                        "name": "file",
                        "in": "formData",
                        "description": "file",
                        "required": true,
                        "type": "file"
                    },
                    {
                        "in": "formData",
                        "name": "description",
                        "description": "description",
                        "required": true,
                        "type": "string"
                    }
@shartte
Copy link

shartte commented Oct 6, 2018

Alright, I see that a contract test is failing that relates to #1965 apparently.
Using https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject as the reference, I do not believe that set of test data is actually compliant with the spec.

The spec says that only name, in, description, and required are "fixed" fields.
schema is only valid for parameters with in=body, which cannot coexist with in=formData.

It also specifically says the following:
The type of the parameter. Since the parameter is not located at the request body, it is limited to simple types (that is, not an object).

It only seems valid to model a JSON document sent via a formData multipart as type=string in Swagger 2.

@dilipkrish dilipkrish added the bug label Oct 23, 2018
@dilipkrish
Copy link
Member

@shartte @ghost Thanks for reporting!

@jowko
Copy link

jowko commented Jan 22, 2019

I also encountered this problem. It worked correctly on 2.7.0 version and broke on 2.8.0 version (problem was discovered when I wanted to upgrade this library).
Probably this problem is related: #2855
Invalid model was generated for every endpoint with has RequestPart annotation.

Currently I'm forced to stay on old version.
Probably fixes for this task: #2097 broke this functionality.

@hovorkap
Copy link

Hi, I am having the same problem

  @PostMapping(path = ["/verify/probe-image"], produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
    fun usingProbeImage(@RequestPart(name = "probe") @ApiParam(value = "File", required = true) probe: MultipartFile,
                        @RequestParam(name = "reference") reference: String)

generates this:

/api/v1/configuration/tools/face-verifier/verify/probe-image:
    post:
      tags:
        - face-verifier-controller
      summary: usingProbeImage
      operationId: usingProbeImageUsingPOST
      consumes:
        - multipart/form-data
      produces:
        - application/json
      parameters:
        - name: probe
          in: formData
          description: File
          required: true
          type: file
        - in: formData
          name: reference
          description: reference
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/Score'
      deprecated: false

Is there any workaround to avoid having
schema:
type: string
instead of just type: string ?

@msangals
Copy link

I'm running springfox at version 2.9.2

When you build a custom ApiDescription with the ParametersBuilder as follow:

new ParameterBuilder()
    .name("password")
    .parameterType("formData")
    .required(true)
    .modelRef(new ModelRef("string"))
    .build()

Generates the same broken JSON for formData:

                   {
                        "in": "formData",
                        "name": "password",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }

The expected valid JSON is:

                   {
                        "in": "formData",
                        "name": "password",
                        "required": true,
                        "type": "string"
                    }

@spylightadmin
Copy link

We have the same problem in v2.9.2, any update on this?

@kremers
Copy link
Contributor

kremers commented Nov 4, 2019

Same Problem with 2.9.2!

@joistick11
Copy link

Same

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

Successfully merging a pull request may close this issue.

8 participants