从外部 yaml 文件引用响应时的 NPE

NPE when responses are referenced from external yaml files

我正在拆分 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

观察结果:
一个观察结果是,如果 'schemas' 是从 'responses' 的外部文件引用的,代码生成就会工作。 所以 !如果从外部 yaml 文件引用 'responses' 会出现什么问题?

以下 有效 :- 'schemas' 外部引用而不是 'response' (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

此问题已在 openapi-generator-cli-4.0.0-beta3 中修复。

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

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

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:

似乎 open API 寻找外部引用的内容。虽然不确定。但对我有用!