OpenAPITools / OpenAPITools/openapi-generator

[BUG][kotlin-spring] Version >= 5.0.0 generates invalid code on HTTP OPTIONS

Open
#9,773 2 comments 7 reactions 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

Description

Starting version 5.0.0, it is impossible to generate compiling code when using HTTP OPTIONS in an API. It used to work just fine as the generated code was using @RequestMapping, defining the RequestMethod.

openapi-generator version

>= 5.0.0

OpenAPI declaration file content
openapi: 3.0.3
info:
  title: kotlin-spring generator bug report
  version: v1

paths:
  /world:
    options:
      operationId: options
      tags:
        - hello
      summary: This will generate code that cannot compile
      responses:
        200:
          description: Hello world!
          content:
            application/json:
              example:
                { }

Generation Details

Generated code looks as follow

/**
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) (5.0.0).
 * https://openapi-generator.tech
 * Do not edit the class manually.
*/
package com.bugreport.api

import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity

import org.springframework.web.bind.annotation.*
import org.springframework.validation.annotation.Validated
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.beans.factory.annotation.Autowired

import javax.validation.Valid
import javax.validation.constraints.DecimalMax
import javax.validation.constraints.DecimalMin
import javax.validation.constraints.Max
import javax.validation.constraints.Min
import javax.validation.constraints.NotNull
import javax.validation.constraints.Pattern
import javax.validation.constraints.Size

import kotlin.collections.List
import kotlin.collections.Map

@Validated
@RequestMapping("\${api.base-path:}")
interface WorldApi {


    @OptionsMapping(
            value = ["/world"],
            produces = ["application/json"]
    )
    fun options(): ResponseEntity<Unit> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }
}
Steps to reproduce

openapi-generator-cli generate -i bugreport.yml -g kotlin-spring

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/7189#discussion_r638934922

Suggest a fix

Rollback.

The previous version was covering all cases, at the price of a bit of verbosity on generated code, which is not an issue. The PR can only work for CRUD operations, but all other HTTP methods were lost as a consequence.

As reference, RequestMethod in Spring is defined as:

public enum RequestMethod {
	GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}

Existing Spring annotations are:

@GetMapping
@PostMapping
@PutMapping
@PatchMapping
@DeleteMapping

Anybody using HTTP HEAD, OPTIONS or TRACE will be unable to use the generator.

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

Run the provided bugreport.yml with the kotlin-spring generator and inspect the generated WorldApi for HTTP OPTIONS. Read the related PR discussion for the annotation change and compare the generated output with Spring's supported RequestMethod values. Done means HTTP OPTIONS, HEAD, and TRACE APIs generate compiling Kotlin code without losing existing CRUD mappings.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, spring
Domain
backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.