Skip to content

[BUG][KOTLIN-SPRING] When using models in query params, the RequestParam should not be generated #8080

Closed

Description

@sburnicki

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

You can encapsulate multiple query parameters in models that are easier to handle on the code side.
Spring supports this by using a Bean (data class in kotline) as the parameter type and leaving out the @RequestParam annotation.

kotlin-spring-generator does generate the @RequestParam annotation also for models, resulting in an endpoint that does not work. If I modify the generated code and remove the annotation, everything works as expect. Note that the spring-generator correctly leaves the annotation out.

openapi-generator version

All versions, verified with latest master (033d985) via docker

OpenAPI declaration file content or url

Example spec where parameters parameterOne and parameterTwo should be encapsulated in the Filter model:

openapi: 3.0.0
info:
  version: '1'
  title: kotlin-spring openapi generator bug example
tags:
  - name: test
    description: test API
paths:
  /test:
    get:
      tags:
        - test
      summary: Endpoint should take both filter parameters in one model
      operationId: test
      parameters:
        - name: filter
          in: query
          description: filter parameters
          required: false
          schema:
            $ref: '#/components/schemas/Filter'
      responses:
        '200':
          description: successful operation
components:
  schemas:
    Filter:
      title: Filter parameters
      type: object
      properties:
        parameterOne:
          type: integer
          format: int64
        parameterTwo:
          type: string
Generation Details

I reproduced this with the example being in /tmp/kotlin-spring-bug.yaml and running the CLI via docker

docker run --rm -v /tmp/kotlin-spring-out:/out -v /tmp/kotlin-spring-bug.yaml:/tmp/apispec.yml openapitools/openapi-generator-cli:latest generate -g kotlin-spring -i /tmp/apispec.yml -o /out
Actual output

The file /tmp/kotlin-spring-out/src/main/kotlin/org/openapitools/api/TestApi.kt contains this code:

    fun test( @RequestParam(value = "filter", required = false) filter: Filter?): ResponseEntity<Unit> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }

which doesn't correctly map the parameters parameterOne and parameterTwo, but will always be called with filter being null.

Expected output

An endpoint without the @RequestParam annotation, simply:

    fun test(filter: Filter): ResponseEntity<Unit> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }

Spring will correctly create an instance of Filter with parameterOne and parameterTwo being mapped.

Suggest a fix

Leave out the @RequestParam annotation when generating the endpoint for query params being models.
The spring-generator handles models as query params correctly.

Activity

  1. auto-labeler commented on Dec 3, 2020

    @auto-labeler

    👍 Thanks for opening this issue!
    🏷 I have applied any labels matching special text in your issue.

    The team will review the labels and make any necessary changes.

  2. sburnicki commented on Dec 22, 2020

    @sburnicki
    ContributorAuthor

    For java/spring similar issues existed: #2655, #3248.
    They were fixed by #2710 and #5131.

    I will create a fix based on the same logic for kotlin/spring.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions