Object array rendered as empty array in Swagger UI

为君一笑 提交于 2019-12-11 06:39:11

问题


I have the following model definitions in an OpenAPI/Swagger spec:

"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"
            }
        }
    }
}

In the Feature model, he Equipments property is defined as an array of Equipment models, but Swagger UI 3.x renders it as an empty array []. Everywhere Feature model is used, like as examples for POST method in Feature I have this kind of display.

Is the definition incorrect in some way?

The complete spec is here:
https://dl.dropboxusercontent.com/s/anjfhgxhr0pfmnu/swagger-bug.json


回答1:


This seems to be a bug in Swagger UI and is most likely caused by circular references in your models - models.Equipment references models.Feature, and models.Feature references models.Equipment. You can open an issue in the Swagger UI repository on GitHub.


Your spec also contains errors in the response definitions:

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

Each response must have a description, so the correct version would be:

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


来源:https://stackoverflow.com/questions/44871128/object-array-rendered-as-empty-array-in-swagger-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!