Swagger Validator无法识别openapi yaml格式错误-Codegen在有效的openapi规范上的getParameterFromRef()处抛出NullPointerException

时间:2018-10-12 17:28:00

标签: swagger openapi swagger-codegen

尽管如此,验证没有发现任何问题,昂首阔步的代码源在NPE上一直失败。它发生在v2.3.1和3.0.1(https://github.com/swagger-api/swagger-codegen)上。生成Java,Spring或php都没关系-仍然是相同的错误。

以下是API定义的失败部分(当我仅按照以下代码段代码使API包含时):

openapi: 3.0.2

paths:
  /Service/listProducts:
    get:
      parameters:
        - in: header
          name: engineName
          schema:
            $ref: '#/components/schemas/EngineNameEnum'
          required: true
      responses:
        '200':
          description: 'Listing OK'

components:
  schemas:   
    EngineNameEnum:
      type: string
      enum:
        - A
        - B
        - C
        - D
        - E

我跑步:

java -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i openapi.yaml -l spring -o tmp-spring\

已生成组件,但在第一个路径操作定义上,我得到了:

18:51:01.070 [Thread-1] WARN io.swagger.codegen.v3.generators.DefaultCodegenConfig - Empty operationId found for path: get /Service/listProducts. Renamed to auto-generated operationId: serviceListProductsGet
18:51:01.092 [Thread-1] DEBUG io.swagger.codegen.v3.generators.DefaultCodegenConfig - Property type is not primitive: \Swagger\Client\Model\EngineNameEnum
java.lang.NullPointerException
        at io.swagger.codegen.v3.generators.DefaultCodegenConfig.getParameterFromRef(DefaultCodegenConfig.java:3659)
        at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromOperation(DefaultCodegenConfig.java:2012)
        at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:847)
        at io.swagger.codegen.v3.DefaultGenerator.processPaths(DefaultGenerator.java:771)
        at io.swagger.codegen.v3.DefaultGenerator.generateApis(DefaultGenerator.java:419)
        at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:722)
        at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:342)
        at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-1" java.lang.RuntimeException: Could not process operation:
  Tag: class Tag {
    name: products
    description: Operations for Products Management
    externalDocs: null
}
  Operation: null
  Resource: get /Service/listProducts
  Exception: null
        at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:872)
        at io.swagger.codegen.v3.DefaultGenerator.processPaths(DefaultGenerator.java:771)
        at io.swagger.codegen.v3.DefaultGenerator.generateApis(DefaultGenerator.java:419)
        at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:722)
        at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:342)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
        at io.swagger.codegen.v3.generators.DefaultCodegenConfig.getParameterFromRef(DefaultCodegenConfig.java:3659)
        at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromOperation(DefaultCodegenConfig.java:2012)
        at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:847)
        ... 5 more

是否有关于io.swagger.codegen.v3.generators.DefaultCodegenConfig.getParameterFromRef()上NPE原因的想法?我还没有发现有关此方法的任何信息。

我希望该问题是由更大的API格式引起的,但验证程序找不到任何问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

所以我的问题没有完全描述问题。发电机故障的原因是下面的正确代码,请参见错误/确定的代码段:

# WRONG
paths:
  /Service/listProducts:
    get:
      parameters:
        - in: header
          name: engineName
          schema:
            $ref: '#/components/schemas/EngineNameEnum'
          required: true
        - $ref: '#/components/schemas/ListProductsRequest
      responses:
        '200':
          description: 'Listing OK'

# OK
paths:
  /Service/listProducts:
    get:
      parameters:
        - in: header
          name: engineName
          schema:
            $ref: '#/components/schemas/EngineNameEnum'
          required: true
        - in: query
          name: listCampaignRequest
          schema:
            $ref: '#/components/schemas/ListProductsRequest'
          required: true
      responses:
        '200':
          description: 'Listing OK' 

因此,这是一个麻烦的验证者问题-我将相应地更新此问题的标题。