【问题标题】:Object array rendered as empty array in Swagger UI在 Swagger UI 中呈现为空数组的对象数组
【发布时间】:2017-12-05 20:52:41
【问题描述】:

我在 OpenAPI/Swagger 规范中有以下模型定义:

"definitions": {
    "models.Equipment": {
        "title": "Equipment",
        "type": "object",
        "properties": {
            "Features": {
                "type": "array",
                "items": {
                    "$ref": "#/definitions/models.Feature"
                }
            },
            "Id": {
                "type": "integer",
                "format": "int64"
            },
            "IdType": {
                "type": "string"
            },
            "Name": {
                "type": "string"
            },
            "Price": {
                "type": "integer",
                "format": "int32"
            }
        }
    },
    "models.Feature": {
        "title": "Feature",
        "type": "object",
        "properties": {
            "Equipments": {
                "type": "array",
                "items": {
                    "$ref": "#/definitions/models.Equipment"
                }
            },
            "Id": {
                "type": "integer",
                "format": "int64"
            },
            "IdFeature": {
                "$ref": "#/definitions/models.Feature"
            },
            "Name": {
                "type": "string"
            }
        }
    }
}

Feature 模型中,他的Equipments 属性定义为Equipment 模型的数组,但Swagger UI 3.x 将其呈现为空数组[]。到处都在使用Feature 模型,例如FeaturePOST 方法的示例我有这种显示。

这个定义在某些方面不正确吗?

完整的规范在这里:
https://dl.dropboxusercontent.com/s/anjfhgxhr0pfmnu/swagger-bug.json

【问题讨论】:

    标签: swagger swagger-ui


    【解决方案1】:

    这似乎是 Swagger UI 中的一个错误,很可能是由模型中的循环引用引起的 - models.Equipment 引用 models.Featuremodels.Feature 引用 models.Equipment。您可以在 GitHub 上的 Swagger UI repository 中打开问题。


    您的规范还包含响应定义中的错误:

            "responses": {
                "200": {
                    "schema": {
                        "$ref": "#/definitions/models.Equipment"
                    }
                },
                "403": {}
            }
    

    每个回复must have a description,所以正确的版本应该是:

            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/models.Equipment"
                    }
                },
                "403": {
                    "description": "Oops"
                }
            }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2021-05-28
    相关资源
    最近更新 更多