【问题标题】:Nested reference not working in Swagger UI / OpenAPI 3嵌套参考在 Swagger UI / OpenAPI 3 中不起作用
【发布时间】:2021-09-08 11:48:05
【问题描述】:

我有这个带有嵌套引用的 OpenAPI 3.0 定义:

Student:
    properties : &BaseStudent
    id:
      type: integer
      format: int64
      description: The ID of the new account.
    name:
      type: string
      description: The human-readable description of this account.
    address:
      $ref : 'address.yaml#/Address'

Address:
    properties : 
     <<* : BaseStudent
    city:
       type: string
      description: City.
    state:
      type: string
      description: State.
    
StudentMarks:
    <<: *BaseStudent
    makr1:
       type: string
      description: Mark1.
    mark2:
      type: string
      description: Mark2.

当我在 Swagger UI 中单击请求选项卡时,它显示错误:

无法解析引用,因为:无法解析指针:/components/schemas/components/schemas/Address 在文档中不存在

实际上,Address 引用应该只是 /components/schemas/。我不知道为什么它指的是/components/schemas/components/schemas/Address

还有其他方式来指定路径吗?

【问题讨论】:

  • 您在哪个工具中看到此错误 - Swagger Editor、Codegen 或其他工具? address.yaml 文件中有什么内容?
  • 我在 Swagger UI 页面中看到了这个错误。我的 api 工作正常,但是当我单击请求选项卡时,我收到了这个错误。抱歉,address.yaml 打错了。我更新了它

标签: swagger-ui openapi


【解决方案1】:

这个问题可能是由 YAML 锚引起的。

OpenAPI 具有引用定义和进行模型组合的内置方法 - $refallOf。通常应该使用这些关键字来代替 YAML 锚。

尝试如下重写架构,看看是否能解决问题:

components:
  schemas:

    Student:
      type: object
      properties:
        id:
          type: integer
          format: int64
          description: The ID of the new account.
        name:
          type: string
          description: The human-readable description of this account.
        address:
          $ref : 'address.yaml#/Address'

    Address:
      allOf:
        - $ref: '#/components/schemas/BaseStudent'
        - properties:
            city:
              type: string
              description: City.
            state:
              type: string
              description: State.
    
    StudentMarks:
      allOf:
        - $ref: '#/components/schemas/BaseStudent'
        - properties:
            mark1:
              type: string
              description: Mark1.
            mark2:
              type: string
              description: Mark2.

【讨论】:

  • 感谢海伦的回复。但是当我尝试这个时,我得到 java.io.FileNotFoundException: C:\test\src\main\resources\schema\v0_1 (Access is denied) 错误。使用 allOf 是否需要其他配置?
  • 你从哪里得到这个 Java 错误?您说您使用的是 Swagger UI,但它不是 Java 应用程序。
  • 是的,在构建项目本身时我收到了这个错误 Helen。
  • 什么项目?如何构建?
  • 我们每次构建都会生成类文件。使用 allOf 后,我无法构建 gettng 访问被拒绝错误
猜你喜欢
  • 2023-02-02
  • 1970-01-01
  • 2022-10-25
  • 2021-05-21
  • 1970-01-01
  • 2017-10-13
  • 2015-06-13
  • 2021-07-14
  • 1970-01-01
相关资源
最近更新 更多