【问题标题】:Swagger: Add description with refSwagger:使用 ref 添加描述
【发布时间】:2017-03-24 15:24:10
【问题描述】:

我想为引用他的定义的对象属性添加描述。类似的东西:

      newCreditCard:
        type: object
        properties:
          billingPhone:
            description: Phone number of the card holder
            $ref: "#/definitions/PhoneNumber"

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

Extra JSON Reference properties will be ignored: description

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

      newCreditCard:
        type: object
        properties:
          billingPhone:
            description: Phone number of the card holder
            allOf:
            - $ref: "#/definitions/PhoneNumber"

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

感谢您的帮助!

【问题讨论】:

  • 第二个示例中的解决方法应该适用于最新版本的 Swagger UI。

标签: swagger swagger-ui swagger-editor


【解决方案1】:

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

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

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

【讨论】:

  • 有一个关于这个的问题。 2岁了,还没解决。 github.com/OAI/OpenAPI-Specification/issues/556
  • 虽然有几个电话号码,例如一个是“持卡人的电话号码”,另一个是“银行的电话号码”等。所以我看不出在引用对象中提供描述是“正确的”,即在电话号码类型的定义中.
【解决方案2】:

虽然它不符合 JSON 标准。 如果你正在使用 Swashbuckle 来产生你的招摇。 我利用了模式的“扩展”属性。 并设法创建了一个带有 $ref 和扩展属性的大张旗鼓的 JSON。


var refSchema = new OpenApiSchema
{      
     //Reference = new OpenApiReference { ExternalResource = referenceLink, Type = ReferenceType.Link }, this wont work and omit all your other properties
    Extensions = new Dictionary<string, IOpenApiExtension>
    {
        { "$ref" , new OpenApiString(referenceLink) } // adding ref as extension cause according to JSON standards $ref shouldnt have any other properties
    },
    Description = prop.Value.Description,
    ReadOnly = prop.Value.ReadOnly,
    Required = prop.Value.Required,
    Type = prop.Value.Type,
    Example = prop.Value.Example,
};

【讨论】:

    【解决方案3】:

    您可以简单地将description 属性移动到PhoneNumber 的定义中。您的原始帖子未显示您如何定义 PhoneNumber,但此 sn-p 验证时没有警告:

    definitions:
      phoneNumber:
        type: string
        description: Phone number of the card holder
    
      newCreditCard:
        type: object
        properties:
          billingPhone:
            $ref: "#/definitions/phoneNumber"
    

    如果此答案不是您想要的,请重述问题。我们需要知道你想要完成什么。

    【讨论】:

    • 假设 $ref 的目的是重用一个定义,那么单个 phoneNumber 定义应该可用于 'card holder'、'mobile'、'home'、'fax ',一个'工作'号码......你明白了。这些描述不能全部包含在 phoneNumber 定义中,而是必须应用于 reference phoneNumber 定义的定义。看起来 Swagger(或者它是底层的 JSONReference 规范?)使这变得困难,或者至少不明显。
    • 附录:该线程表明它是 JSONReference 规范的约束 - groups.google.com/forum/#!topic/swagger-swaggersocket/…。同意所有评估,这是一种耻辱,因为它确实在某些情况下使重复使用变得不可能。
    • 将描述属性与phoneNumber 模型分开的目的是允许具有不同描述但使用相同模型的多个属性。例如 billingPhone “持卡人的电话号码”,但以传真为例,描述应为“总公司的传真”。除了描述之外,所有模型定义都是相同的。感谢您的回答和帮助
    猜你喜欢
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2016-11-06
    相关资源
    最近更新 更多