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

[Kotlin][Spring] Fix RequestPart handling for multipart request #19058

Merged

Conversation

zakupower
Copy link
Contributor

fix for #15251

When generating a Multipart request where model is used instead of binary or basic field, it should be annotated with @Multipart instead of @RequestParam.

This was already fixed for the Java-Spring generator with #14357, but is still present in the Kotlin-Spring generator.

Link to Spring Documentation supporting this

Example Spec:

openapi: '3.0.1'
info:
  version: 1.0.0
  title: MultipartFile test
paths:
  /multipart-mixed:
    post:
      tags:
        - multipart
      description: Mixed MultipartFile test
      operationId: multipartMixed
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              required:
                - status
                - file
              properties:
                status:
                  $ref: '#/components/schemas/MultipartMixedStatus'
                marker:
                  description: "additional object"
                  type: object
                  properties:
                    name:
                      type: string
                file:
                  description: "a file"
                  type: string
                  format: binary
      responses:
        '204':
          description: Successful operation
components:
  schemas:
    MultipartMixedStatus:
      description: "additional field as Enum"
      type: string
      enum:
        - ALLOWED
        - IN_PROGRESS
        - REJECTED
      example: REJECTED

Expected Output:

@RestController
@Validated
@RequestMapping("\${api.base-path:}")
class MultipartMixedApiController() {

    @Operation(
        summary = "",
        operationId = "multipartMixed",
        description = """Mixed MultipartFile test""",
        responses = [
            ApiResponse(responseCode = "204", description = "Successful operation") ]
    )
    @RequestMapping(
        method = [RequestMethod.POST],
        value = ["/multipart-mixed"],
        consumes = ["multipart/form-data"]
    )
    fun multipartMixed(@Parameter(description = "", required = true, schema = Schema(allowableValues = ["ALLOWED", "IN_PROGRESS", "REJECTED"])) @RequestParam(value = "status", required = true) status: MultipartMixedStatus ,@Parameter(description = "a file") @Valid @RequestPart("file", required = true) file: org.springframework.core.io.Resource,@Parameter(description = "") @RequestPart(value = "marker", required = false) marker: MultipartMixedRequestMarker? ): ResponseEntity<Unit> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }
}

Actual output with version 7.7.0:

@RestController
@Validated
@RequestMapping("\${api.base-path:}")
class MultipartMixedApiController() {

    @Operation(
        summary = "",
        operationId = "multipartMixed",
        description = """Mixed MultipartFile test""",
        responses = [
            ApiResponse(responseCode = "204", description = "Successful operation") ]
    )
    @RequestMapping(
        method = [RequestMethod.POST],
        value = ["/multipart-mixed"],
        consumes = ["multipart/form-data"]
    )
    fun multipartMixed(@Parameter(description = "", required = true, schema = Schema(allowableValues = ["ALLOWED", "IN_PROGRESS", "REJECTED"])) @RequestParam(value = "status", required = true) status: MultipartMixedStatus ,@Parameter(description = "a file") @Valid @RequestPart("file", required = true) file: org.springframework.core.io.Resource,@Parameter(description = "") @RequestParam(value = "marker", required = false) marker: MultipartMixedRequestMarker? ): ResponseEntity<Unit> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }
}

Specifically look at the marker parameter generation

PR checklist

  • Read the contribution guidelines.
  • Pll Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.6.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@jimschubert @dr4ke616 @karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier

@zakupower
Copy link
Contributor Author

zakupower commented Jul 18, 2024

Any reason this is not being merged? (Don't want to be rude or impatient, just want to know if I can do something to get this to be merged before next version)

@wing328
Copy link
Member

wing328 commented Jul 18, 2024

@zakupower thanks for the PR. Can you please add a test by first adding a config file similar to ./bin/configs/kotlin-spring-boot-bigdecimal-default.yaml (e.g. kotlin-spring-multipart-request-model.yaml`) ?

Then create a test spec similar to modules/openapi-generator/src/test/resources/3_0/issue_14584_bigdecimal_default.yaml

Then generate the samples (run the ./bin/generate-samples.sh command twice)

And update the .github/workflows/samples-kotlin-server.yaml with the path to the new sample folder (e.g. samples/server/petstore/kotlin-springboot-multipart-request-model)

@zakupower zakupower requested a review from jimschubert as a code owner July 18, 2024 10:40
@zakupower
Copy link
Contributor Author

@wing328 I think I made all the changes that you requested.

@wing328
Copy link
Member

wing328 commented Jul 18, 2024

https://github.com/OpenAPITools/openapi-generator/actions/runs/9990043197/job/27617120054?pr=19058 not related to this PR

i'll merge later today if no one else has feedback on this PR.

thanks again for the PR

@zakupower
Copy link
Contributor Author

Thank you as well for your quick responses and hard work on the project!

@wing328 wing328 merged commit f44bc30 into OpenAPITools:master Jul 18, 2024
31 of 32 checks passed
@wing328 wing328 added this to the 7.8.0 milestone Jul 18, 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 this pull request may close these issues.

2 participants