Swagger: Common response/payload object but different data defined per API?

时间:2016-08-31 17:28:24

标签: swagger swagger-ui swagger-2.0

What is the best way to represent a generic response/payload object that has basic fields such as message, total elements, data, and status? Where the variability between each API is the data element. For instance, one API could be for permissions, so the data element would hold an array of permissions. But for another API, it would hold a different array of object types. My main goal is to have reuse with a payload object and to define the next "layer" of actual data.

I want to be able to define a data type that's generic - like a "response" that has basic fields but I want to be able to further define that content for each API (data containing permissions or roles or whatever).

Here are some JSON samples of what's been attempted but isn't rendering the way we would expect it to in Swagger UI (i.e. a flat structure of 4 elements with data defined per the API).

Example 1 - composition:

    {
    "swagger": "2.0",
    "host": "localhost:8888",
    "info": {
        "version": "0.0.1",
        "title": "API"
    },
    "paths": {
        "/permissions": {
            "get": {
                "description": "Returns all permissions",
                "operationId": "getPermissions",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {
                        "description": "success",
                        "schema": {
                            "$ref": "#/definitions/permissionResponse"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "response": {
            "type": "object",
            "properties": {
                "message": {
                    "type": "string",
                    "description": "A string indicating the response from the server."
                },
                "totalElements": {
                    "type": "integer",
                    "format": "int64",
                    "description": "The number of items retrieved."
                },
                "status": {
                    "type": "string",
                    "description": "A string indicating the response from the server."
                }
            }
        },
        "permissionResponse": {
            "allOf": [
                {
                    "$ref": "#/definitions/response"
                }, {
                    "type": "object",
                    "properties": {
                        "data": {
                            "type": "array",
                            "description": "The collection of items returned from the API request.",
                            "items": {
                                "$ref": "#/definitions/permission"
                            }
                        }
                    }
                }
            ]
        },
        "permission": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer",
                    "format": "int64",
                    "description": "Unique identifier representing a specific permission."
                },
                "label": {
                    "type": "string",
                    "description": "The label of a permission."
                },
                "description": {
                    "type": "string",
                    "description": "A description of the permission."
                },
                "active": {
                    "type": "boolean",
                    "description": "A flag indicating whether a permission is active."
                }
            },
            "required": [
                "id",
                "label",
                "description",
                "active"
            ]
        }
    }
}

Example 2 - composition variation:

    {
    "swagger": "2.0",
    "host": "localhost:8888",
    "info": {
        "version": "0.0.1",
        "title": "API"
    },
    "paths": {
        "/permissions": {
            "get": {
                "description": "Returns all permissions",
                "operationId": "getPermissions",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {
                        "description": "success",
                        "schema": {
                            "$ref": "#/definitions/permissionResponse"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "response": {
            "type": "object",
            "properties": {
                "message": {
                    "type": "string",
                    "description": "A string indicating the response from the server."
                },
                "totalElements": {
                    "type": "integer",
                    "format": "int64",
                    "description": "The number of items retrieved."
                },
                "status": {
                    "type": "string",
                    "description": "A string indicating the response from the server."
                }
            }
        },
        "permissionResponse": {
            "allOf": [
                {
                    "$ref": "#/definitions/response"
                }],
            "type": "object",
            "properties": {
                "data": {
                    "type": "array",
                    "description": "The collection of items returned from the API request.",
                    "items": {
                        "$ref": "#/definitions/permission"
                    }
                }
            }
        },
        "permission": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer",
                    "format": "int64",
                    "description": "Unique identifier representing a specific permission."
                },
                "label": {
                    "type": "string",
                    "description": "The label of a permission."
                },
                "description": {
                    "type": "string",
                    "description": "A description of the permission."
                },
                "active": {
                    "type": "boolean",
                    "description": "A flag indicating whether a permission is active."
                }
            },
            "required": [
                "id",
                "label",
                "description",
                "active"
            ]
        }
    }

Example 3 - additional properties:

    {
    "swagger": "2.0",
    "host": "localhost:8888",
    "info": {
        "version": "0.0.1",
        "title": "API"
    },
    "paths": {
        "/permissions": {
            "get": {
                "description": "Returns all permissions",
                "operationId": "getPermissions",
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {
                        "description": "success",
                        "schema": {
                            "$ref": "#/definitions/permissionResponse"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "response": {
            "type": "object",
            "properties": {
                "message": {
                    "type": "string",
                    "description": "A string indicating the response from the server."
                },
                "totalElements": {
                    "type": "integer",
                    "format": "int64",
                    "description": "The number of items retrieved."
                },
                "status": {
                    "type": "string",
                    "description": "A string indicating the response from the server."
                }
            }
        },
        "permissionResponse": {
            "type": "object",
            "properties": {
                "data": {
                    "type": "array",
                    "description": "The collection of items returned from the API request.",
                    "items": {
                        "$ref": "#/definitions/permission"
                    }
                }
            },
            "additionalProperties": {
                "$ref": "#/definitions/response"
            }
        },
        "permission": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer",
                    "format": "int64",
                    "description": "Unique identifier representing a specific permission."
                },
                "label": {
                    "type": "string",
                    "description": "The label of a permission."
                },
                "description": {
                    "type": "string",
                    "description": "A description of the permission."
                },
                "active": {
                    "type": "boolean",
                    "description": "A flag indicating whether a permission is active."
                }
            },
            "required": [
                "id",
                "label",
                "description",
                "active"
            ]
        }
    }

Example 1 renders well with swagger2markup showing the consolidated view of the data with the other 3 elements and having a permission array. However, with Swagger UI, it renders it differently, separating the objects out. Markup rendered: Markup rendered

Swagger UI rendered Swagger UI rendered

Swagger UI rendered - expanded Swagger UI rendered - expanded

0 个答案:

没有答案