OpenAPITools / OpenAPITools/openapi-generator

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

Open
#18,419 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

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
  • Generator: kotlin-spring
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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the generated controller with the sample.yaml spec and the kotlin-spring generator command. Inspect modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinServerCodegen.java and compare the analogous Java Spring fix. Done means generated Int64 parameter defaults use "10" rather than "10L" in the controller annotations.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, spring
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.