openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open mwatel42 opened this issue 5 years ago • 2 comments

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.

mwatel42 avatar Jun 15 '21 17:06 mwatel42

This was fixed in #9900 and this ticket can be closed.

sjoblomj avatar May 01 '22 17:05 sjoblomj

Tested on latest master (7.25.0-SNAPSHOT, built locally) with the spec from this issue, kotlin-spring:

@RequestMapping(
    method = [RequestMethod.OPTIONS],

Looks fixed to me, I think this can be closed.

SubhamAshok avatar Aug 04 '26 10:08 SubhamAshok