在Swagger UI中呈现为空数组的对象数组

时间:2017-07-02 13:05:19

标签: swagger swagger-ui

我在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模型,就像POSTFeature方法的示例一样。我有这种显示方式。

swagger-ui bug

定义是否以某种方式不正确?

完整的规范如下:
https://dl.dropboxusercontent.com/s/anjfhgxhr0pfmnu/swagger-bug.json

1 个答案:

答案 0 :(得分: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"
            }
        }