Swagger: common response / payload object, but different data specific to the API?

What is the best way to represent a common response / payload object that has basic fields like message, common elements, data and status? Where the variability between each API is a data item. For example, one API may be for permissions, so the data item will contain an array of permissions. But for another API, it will contain a different array of object types. My main goal is to reuse the payload object and determine the next "level" of evidence.

I want to be able to define a data type that is common - like a "response" that has basic fields, but I want to be able to further define this content for each API (data containing permissions or roles or something else).

Here are some JSON examples of what has been undertaken, but not the way we expect it in the Swagger user interface (i.e. a flat structure of 4 elements with data specific to 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 - change in 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 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 shows well that swagger2markup shows a consolidated view of data with the other 3 elements and has an array of permissions. However, with the Swagger user interface, it does it differently, separating objects from. Markup Display: Markup display

Swagger user interface Swagger UI

Swagger - Swagger Deployed User Interface - Advanced

+4

Source: https://habr.com/ru/post/1653198/


All Articles