在引用架构时使用特定属性

Using specific properties while referencing to schema

我正在使用 swagger 2.0 并且我有以下架构(定义):

"User": {
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string",
            "example": "Tom"
        },
        "lastName": {
            "type": "string",
            "example": "Hanks"
        },
        "email": {
            "type": "string",
            "example": "Tom.Hanks@gmail.com"
        },
        "password": {
            "type": "string",
            "example": "azerty@123456"
    }
}

我想在我的一个回复中引用这个模式,所以我做了以下事情:

"responses": {
    "201": {
        "description": "Created.",
        "schema": {
            "$ref": "#/definitions/User"
        }
    }
}

到目前为止一切正常,但我不想在响应模式中公开密码 属性。无论如何要从 User 定义中准确选择我想使用的属性?

不行,没办法。我建议您定义 2 种类型:

  • 一种没有密码的用户数据,我们将其命名为User
  • 还有另一种类型从它继承 并另外包含一个password 属性。我们将其命名为 UserWithCredential.