昂首阔步:用ref添加描述

Jon*_*uet 9 swagger swagger-ui swagger-editor

我想向引用其定义的对象属性添加描述。像这样:

      newCreditCard:
        type: object
        properties:
          billingPhone:
            description: Phone number of the card holder
            $ref: "#/definitions/PhoneNumber"
Run Code Online (Sandbox Code Playgroud)

但是编辑器警告说description属性将被跳过:

Extra JSON Reference properties will be ignored: description
Run Code Online (Sandbox Code Playgroud)

我发现了一种不太优雅的解决方法,该方法适用于编辑器,但不适用于Swagger UI(不确定这可能是由于Swagger UI的3.0.2版本最近进行了更新)

      newCreditCard:
        type: object
        properties:
          billingPhone:
            description: Phone number of the card holder
            allOf:
            - $ref: "#/definitions/PhoneNumber"
Run Code Online (Sandbox Code Playgroud)

您如何在Swaggers规范中做到这一点?

谢谢您的帮助!

Aru*_*llu 1

如果您将任何内容添加到同一级别,$ref它将被忽略。

json $ref 定义https://datatracker.ietf.org/doc/html/draft-pbryan-zyp-json-ref-03#section-3

正确的方法是在引用的对象中提供描述。

  • 不过,有几个电话号码,例如一个是“持卡人的电话号码”,另一个是“银行的电话号码”等。所以我不明白在引用的对象中提供描述是如何“正确”的,即在 PhoneNumber 类型的定义中。 (31认同)
  • 关于这一点有一个问题。2年了,还没解决。https://github.com/OAI/OpenAPI-Specification/issues/556 (3认同)