表示具有基本字段(如消息,总元素,数据和状态)的通用响应/有效负载对象的最佳方法是什么?每个API之间的可变性是数据元素 . 例如,一个API可以用于权限,因此数据元素将包含一组权限 . 但对于另一个API,它将包含不同的对象类型数组 . 我的主要目标是重用有效负载对象并定义实际数据的下一个“层” .

我希望能够定义一个通用的数据类型 - 比如具有基本字段的“响应”,但我希望能够进一步为每个API定义该内容(包含权限或角色的数据或其他内容) .

下面是一些JSON样本,这些样本已经尝试过,但是没有按照我们在Swagger UI中的预期方式呈现(即4个元素的平面结构,每个API都有数据定义) .

例1 - 组成:

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

例2 - 成分变化:

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

示例3 - 其他属性:

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

示例1使用swagger2markup很好地呈现数据的整合视图,其中包含其他3个元素并具有权限数组 . 但是,使用Swagger UI时,它会以不同方式呈现它,将对象分离出来 . 呈现的标记:
Markup rendered

Swagger UI渲染
Swagger UI rendered

Swagger UI呈现 - 扩展
Swagger UI rendered - expanded