OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.13k stars 6.4k forks source link

[BUG][Kotlin-Spring] Default values for Int64 parameters creates broken spring API #18419

Open m-artin-t opened 4 months ago

m-artin-t commented 4 months ago

Bug Report Checklist

Description

API parameters of type integer and format int64, have a trailing "L" appended to their default values in spring controllers. This creates broken controllers since spring parses this with Long.valueOf which does not allow for the trailing character.

This was added in https://github.com/OpenAPITools/openapi-generator/pull/13507/files to support default values in client models.

Actual Output:

fun getSample(@Parameter(description = "Sample number", schema = Schema(defaultValue = "10L")) @Valid @RequestParam(value = "sampleNum", required = false, defaultValue = "10L") sampleNum: kotlin.Long): ResponseEntity<GetSample200Response> {

Expected Output:

fun getSample(@Parameter(description = "Sample number", schema = Schema(defaultValue = "10")) @Valid @RequestParam(value = "sampleNum", required = false, defaultValue = "10") sampleNum: kotlin.Long): ResponseEntity<GetSample200Response> {
openapi-generator version

Introduced in 6.2.1, tested on current

OpenAPI declaration file content or url

openapi: 3.0.3
info:
  title: sample-api
  version: 1.0.0
  description: "A sample API"
servers:
  - url: "http://localhost:8080"
tags:
  - name: sample
    description: Sample
paths:
  "/sample":
    get:
      operationId: get-sample
      description: Get Sample
      parameters:
        - $ref: "#/components/parameters/sampleNum"
      tags:
        - sample
      responses:
        "200":
          description: Sample
          content:
            application/json:
              schema:
                type: object
                properties:
                  sample:
                    type: string
                    example: "sample"
components:
  parameters:
    sampleNum:
      description: Sample number
      name: sampleNum
      required: false
      in: query
      schema:
        type: integer
        format: int64
        default: 10
Generation Details
Steps to reproduce

Create a spec file called sample.yaml with the provided sample, and run generation with the kotlin-spring generator.

Check the generated controller function.

openapi-generator generate -i sample.yaml -g kotlin-spring
Related issues/PRs

The same issue was addressed in the java-spring generator.

Suggest a fix

Add a override in KotlinServerCodegen similar to https://github.com/OpenAPITools/openapi-generator/pull/4969/files to remove the suffix for parameters.

wing328 commented 4 months ago

thanks for reporting the issue. I wonder if you can file a PR with the suggested fix when you've time.