[BUG][KOTLIN-SPRING] When using models in query params, the RequestParam should not be generated
Created by: 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 (033d985c) 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.