Julien Kosinski
Julien Kosinski

Reputation: 25

Object array rendered as empty array in Swagger UI

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.

swagger-ui bug

Is the definition incorrect in some way?

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

Upvotes: 1

Views: 3949

Answers (1)

Helen
Helen

Reputation: 97540

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

Upvotes: 1

Related Questions