openapi-generator-maven-plugin 向枚举添加引号并且 swagger-ui 使用它们导致错误

openapi-generator-maven-plugin adds quotes to enumeration and swagger-ui uses them causing error

我们的服务定义如下:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: UVZ Reg
paths:
  /enumerationRequestApproval/{requestId}/{approvalStatus}:
    post:
      tags: 
        - EnumerationApprovalController
      parameters:
       - in: path
         name: requestId
         schema:
           type: integer
           format: int64
         required: true
       - in: path
         name: approvalStatus
         schema:
           $ref: "#/components/schemas/enumerationApprovalStatusEnum"
         required: true
      responses:
        200:
          description: status of enumeration approval changed
components:
  schemas:
    enumerationApprovalStatusEnum:
      type: string
      enum:
        - WAITING_APPROVAL
        - APPROVED
        - NOT_APPROVED

在 openapi-generator-maven-plugin 的帮助下,我们生成 spring 引导服务:

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2020-07-30T09:35:26.969160+02:00[Europe/Bratislava]")

@Validated
@Api(value = "enumerationRequestApproval", description = "the enumerationRequestApproval API")
public interface EnumerationRequestApprovalApi {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    /**
     * POST /enumerationRequestApproval/{requestId}/{approvalStatus}
     *
     * @param requestId  (required)
     * @param approvalStatus  (required)
     * @return status of enumeration approval changed (status code 200)
     */
    @ApiOperation(value = "", nickname = "enumerationRequestApprovalRequestIdApprovalStatusPost", notes = "", tags={ "EnumerationApprovalController", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "status of enumeration approval changed") })
    @RequestMapping(value = "/enumerationRequestApproval/{requestId}/{approvalStatus}",
        method = RequestMethod.POST)
    default ResponseEntity<Void> enumerationRequestApprovalRequestIdApprovalStatusPost(@ApiParam(value = "",required=true) @PathVariable("requestId") Long requestId,@ApiParam(value = "",required=true, allowableValues = "\"WAITING_APPROVAL\", \"APPROVED\", \"NOT_APPROVED\"") @PathVariable("approvalStatus") EnumerationApprovalStatusEnum approvalStatus) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

}

现在,当我们尝试从 swagger-ui (http://localhost:8080/swagger-ui) 调用服务时,它将失败:

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'sk.uvzsr.is.reg.be.rest.model.EnumerationApprovalStatusEnum'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiParam @org.springframework.web.bind.annotation.PathVariable sk.uvzsr.is.reg.be.rest.model.EnumerationApprovalStatusEnum] for value '"WAITING_APPROVAL"'; nested exception is java.lang.IllegalArgumentException: No enum constant sk.uvzsr.is.reg.be.rest.model.EnumerationApprovalStatusEnum."WAITING_APPROVAL"
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:133)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at sk.uvzsr.is.reg.config.CorsFilter.doFilter(CorsFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiParam @org.springframework.web.bind.annotation.PathVariable sk.uvzsr.is.reg.be.rest.model.EnumerationApprovalStatusEnum] for value '"WAITING_APPROVAL"'; nested exception is java.lang.IllegalArgumentException: No enum constant sk.uvzsr.is.reg.be.rest.model.EnumerationApprovalStatusEnum."WAITING_APPROVAL"
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:129)
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73)
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:53)
    at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:693)
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:125)
    ... 50 more
Caused by: java.lang.IllegalArgumentException: No enum constant sk.uvzsr.is.reg.be.rest.model.EnumerationApprovalStatusEnum."WAITING_APPROVAL"
    at java.base/java.lang.Enum.valueOf(Enum.java:266)
    at org.springframework.core.convert.support.StringToEnumConverterFactory$StringToEnum.convert(StringToEnumConverterFactory.java:52)
    at org.springframework.core.convert.support.StringToEnumConverterFactory$StringToEnum.convert(StringToEnumConverterFactory.java:38)
    at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:436)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
    ... 56 more

很明显问题出在枚举值周围的引号上。当我改变

allowableValues = "\"WAITING_APPROVAL\", \"APPROVED\", \"NOT_APPROVED\"")

allowableValues = "WAITING_APPROVAL, APPROVED, NOT_APPROVED")

一切正常,但由于它是生成的API我不能每次都手动更改它。

这是 API 在设计时的样子(无引号): 这是 API 的部署方式(已添加引号):

我用

测试了这个

这个问题是 openapi-generator-maven-plugin、swagger-ui 还是我的 OpenApi3 定义?我该如何解决这个问题?

为了解决这个问题,我们只需要在实现生成的接口的控制器中修复不正确的注解@ApiParam。因此,在重写和实现生成的方法的控制器中,我们指定具有正确值的@ApiParam:

@Override
public ResponseEntity<sk.uvzsr.is.reg.be.rest.model.EnumerationRequest> enumerationRequestApprovalRequestIdApprovalStatusPost(Long requestId, @ApiParam(value = "",required=true, allowableValues = "WAITING_APPROVAL, APPROVED, NOT_APPROVED") @PathVariable("approvalStatus") EnumerationApprovalStatusEnum approvalStatus) {
    // implementation
}

我们通过自定义 openapi-generator maven 插件 生成代码的方式来解决这个问题。我们只是用我们自己的副本替换了默认的 pathParams.mustache 模板文件,在这里我们只是删除了整个部分 {{#allowableValues}} ..... {{/allowableValues}}

这导致在生成的控制器方法中没有更多用于枚举参数的 allowableValues。在此之后,swagger-ui 正确显示它。

有关小胡子模板自定义的更多信息:openapi-generator customisation