当前位置: 首页 > 知识库问答 >
问题:

openapi-generator-maven-plugin将引号添加到枚举中,swagger-ui使用引号导致错误

石正卿
2023-03-14

我们的服务定义如下:

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 Boot服务:

@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")

  • openapi-generator-maven-plugin版本4.2.3和4.3.1.
  • springfox-swagger-ui 2.9.2和3.0.(springfox-boot-starter maven artifact)

这个问题是openapi-generator-maven-plugin、swagger-ui还是我的OpenApi3定义?我怎么才能把它修好呢?

共有1个答案

倪振海
2023-03-14

为了解决这个问题,我们只需要在实现生成的接口的controller中修复不正确的注释@apiparam。因此,在所生成的方法被重写和实现的controller中,我们使用正确的值指定@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
}
 类似资料:
  • 问题内容: 是否可以使用反引号在Go中打印反引号: 问题答案: 原始字符串文字是反引号``之间的字符序列。在引号内,除反引号外,任何字符均合法。原始字符串文字的值是由引号之间未解释的字符组成的字符串。特别是,反斜杠没有特殊含义,字符串可以跨越多行。字符串文字

  • 我正在使用Swagger/OpenAPI V3注释创建应用程序的API描述,这些注释从以下依赖项导入:

  • 问题内容: 当我想在Python中执行命令并且需要使用引号时,我不知道如何在不关闭字符串的情况下执行该命令。 例如: 但是,当我尝试执行上面的操作时,我最终关闭了字符串,并且无法将需要的单词放在引号之间。 我怎样才能做到这一点? 问题答案: 您可以通过以下三种方式之一进行操作: 一起使用单引号和双引号: 转义字符串中的双引号: 使用三引号引起来的字符串:

  • 我正在使用从以下依赖项导入的Swagger/OpenAPIV3注释创建应用程序的API描述: 其中一个批注是批注,它接受名为的属性,该属性允许字符串数组: 现在,我想使用在枚举类上构造的自定义方法,该方法返回允许的字符串数组,因此不需要在每次向枚举添加类型时添加该方法。以便我们可以这样使用它: 现在这是无法编译的,因为在执行注释时不知道该方法。是否有这样的解决方案允许在SwaggerV3注释属性值

  • 问题内容: 当我想在Python中执行命令并且需要使用引号时,我不知道如何在不关闭字符串的情况下执行该命令。 例如: 但是,当我尝试执行上面的操作时,我最终关闭了字符串,并且无法将需要的单词放在引号之间。 我怎样才能做到这一点? 问题答案: 你可以通过以下三种方式之一进行操作: 1)一起使用单引号和双引号: 2)转义字符串中的双引号: 3)使用三引号引起来的字符串:

  • 问题内容: 可以说我有这段: 在此段中加双引号的CSS代码是什么?(因此它将呈现“这是我的段落”) 问题答案: 使用 伪元素 :