当从外部Yaml文件引用响应时,NPE

时间:2019-04-16 09:04:23

标签: java yaml openapi

我正在拆分YAML文件,但是在生成代码时遇到以下异常:

java.lang.NullPointerException
    at io.swagger.v3.parser.ResolverCache.updateLocalRefs(ResolverCache.java:162)
    at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:152)
    at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalResponse(ExternalRefProcessor.java:205)
    at io.swagger.v3.parser.processors.ResponseProcessor.processReferenceResponse(ResponseProcessor.java:76)
    at io.swagger.v3.parser.processors.ResponseProcessor.processResponse(ResponseProcessor.java:38)
    at io.swagger.v3.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:56)
    at io.swagger.v3.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:83)
    at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:49)
    at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:53)
    at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:19)

我想要实现的目标(openapi.yaml)的示例:

openapi: 3.0.0
info:
  title: Common Data Types
  version: "1.0"
paths:
  /{appId}/subscriptions:
    get:
      summary: read all of the active subscriptions for the app

      responses:
        '200':
          description: OK (Successful)
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/subscription'
        '400':
          $ref: './common.yam#/components/responses/E400'
        '401':
          $ref: './common.yam#/components/responses/E401'
components:
  schemas:
    subscription:
      type: string

common.yaml

openapi: 3.0.0
info:
  title: Common Data Types
  version: "1.0"
paths: {}
components:
 responses:
    E400:
      description: Bad request
    E401:
      description: Unauthorized

上下文:

$ tree
├── common.yaml
└── openapi.yaml
$ openapi-generator version
3.3.0

观察
一种观察是,如果从插入“响应”的外部文件中引用“方案”,则代码生成有效。 所以!如果从外部yaml文件中引用“响应”会出现什么问题?

以下起作用:-从外部引用“方案”而不是“响应”(openapi.yaml):

openapi: 3.0.0
info:
  title: Common Data Types
  version: "1.0"
paths:
  /{appId}/subscriptions:
    get:
      summary: read all of the active subscriptions for the app

      responses:
        '200':
          description: OK (Successful)
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: './common.yam#/components/schemas/subscription'
        '400':
          $ref: '#/components/responses/E400'
        '401':
          $ref: '#/components/responses/E401'
components:
 responses:
    E400:
      description: Bad request
    E401:
      description: Unauthorized

common.yaml

openapi: 3.0.0
info:
  title: Common Data Types
  version: "1.0"
paths: {}
components:
  schemas:
    subscription:
      type: string

上下文:

$ tree
.
├── common.yaml
└── openapi.yaml
$ openapi-generator version
3.3.0

1 个答案:

答案 0 :(得分:0)

此问题已在openapi-generator-cli-4.0.0-beta3中解决。

但是,对于下面的3.3.0版本,它可以工作

我观察到的是,导致问题的以下if common.yaml文件被修改为common_modified.yaml,问题得以解决,并且没有Null指针异常。

YAML导致问题common.yaml

openapi: 3.0.0
    info:
      title: Common Data Types
      version: "1.0"
    paths: {}
    components:
     responses:
        E400:
          description: Bad request
        E401:
          description: Unauthorized

修改后的YAML修复了问题common_modified.YAML

openapi: 3.0.0
    info:
      title: Common Data Types
      version: "1.0"
    paths: {}
    components:
     responses:
        E400:
          description: Bad request
          content:
            applictaion/json:  
        E401:
          description: Unauthorized
          content:
            applictaion/json:

如果外部引用,打开的API似乎会查找内容。不确定。但是对我有用!