Skip to content

[BUG] [kotlin-spring] defaultValue for RequestHeader is not generated #20504

@ablil

Description

@ablil
Description

Given an optional request header with a default value, the current version of generator does NOT set a defaultValue on RequestHeader annotation, and does NOT set the generated type to an optional type (eg String? instead of String).

I've check the current mustache template, and the logic is indeed missing please check suggested fix below

openapi-generator version

7.10.0 through Gradle plugin

OpenAPI declaration file content or url
// ...

paths:
  /v1/configurations:
    get:
      summary: list all enabled configurations
      tags: ['configuration']
      operationId: getAllConfigurations
      parameters:
        - $ref: '#/components/parameters/Page'
        - $ref: '#/components/parameters/PageSize'
        - in: header
          name: x-tenant
          schema:
            type: string
            enum: ['de', 'at']
            default: 'de'
      responses:
        200:
          description: list all enabled configurations
          content:
            application/json:
              schema:
              // ...
Generation Details

The current generator version, generate the followign Kotlin code

    @Operation(
        tags = ["configuration"],
        summary = "list all enabled configurations",
        operationId = "getAllConfigurations",
        description = """""",
        responses = [
            ApiResponse(
                responseCode = "200",
                description = "list all enabled configurations",
                content = [Content(schema = Schema(implementation = GetAllConfigurations200ResponseDto::class))]
            )
        ]
    )
    @RequestMapping(
        method = [RequestMethod.GET],
        value = ["/v1/configurations"],
        produces = ["application/json"]
    )
    fun getAllConfigurations(
        @Parameter(
            description = "page number",
            schema = Schema(defaultValue = "1")
        ) @Valid @RequestParam(value = "page", required = false, defaultValue = "1") page: kotlin.Int,
        @Max(200) @Parameter(
            description = "page size",
            schema = Schema(defaultValue = "100")
        ) @Valid @RequestParam(
            value = "size",
            required = false,
            defaultValue = "100"
        ) size: kotlin.Int,
        @Parameter(
            description = "",
            `in` = ParameterIn.HEADER,
            schema = Schema(allowableValues = ["de", "at"], defaultValue = "de")
        ) @RequestHeader(value = "x-tenant", required = false) xTenant: kotlin.String
    ): ResponseEntity<GetAllConfigurations200ResponseDto>

Notice the generatd RequestHeader is missing a default value and type is not set to String?

@RequestHeader(value = "x-tenant", required = false) xTenant: kotlin.String
Suggest a fix

The current Java spring generator already has this logic, it need to be copied also for the Kotlin generator

Java mustache template file: modules/openapi-generator/src/main/resources/JavaSpring/headerParams.mustache

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