首页 > 解决方案 > 指向同一对象的 OpenAPI ref 不起作用

问题描述

definitions:   
   TestObject2:
        type: object
        properties:
          key1:
            type: string
   TestObject:
        type: object
        properties:
          name:
            type: string
          city:
            type: string
          coordinates:
            $ref: '#/definitions/TestObject2'
          directions:
            $ref: '#/definitions/TestObject2'

如果我删除其中一个对 TestObject2 的引用,那么它就可以工作。但是,如果我将相同的引用添加到不同的属性,我看不到在 api 网关“模型”上创建的模型在日志中我看到:

paths:
          '/my/custom/1.0/path/{param1}/{param2}/{param3}':
            put:
              parameters:
                - in: path
                  name: param1
                  type: string
                  required: true
                - in: path
                  name: param2
                  type: string
                  required: true
                - in: path
                  name: param3
                  type: string
                  required: true
              requestBody:
                required: true
                content:
                  application/json:
                    schema:
                      type: object
                      properties:
                        name:
                          type: string
                        city:
                          type: string
                        coordinates:
                          $ref: >-
                            #/paths/~1my~1custom~11.0~1path~1%7Bparam1%7D~1%7Bparam2%7D~1%7Bparam3%7D/put/requestBody/content/application~1json/schema/properties/directions
                        directions:
                          type: object
                          properties:
                            key1:
                              type: string
                      required:
                        - name

注意生成的坐标参考链接看起来很奇怪。它似乎没有使用 TestObject2,而是依赖于 TestObject.directions 中的 ref 链接。对于方向,TestObject2 被正确替换。

标签: aws-api-gatewayopenapi

解决方案


事实证明,它与我定义架构的方式没有任何关系。我使用 swagger-cli 捆绑到一个文件中,默认情况下,swagger-cli 创建这些内部引用链接。我不得不添加一个尊重标志。这有效:

swagger-cli bundle apigateway/endpoints/main.api.yml --dereference --type yaml > apigateway/swagger.yml

推荐阅读