Skip to content

Common response/payload object but different data defined per API? #773

@atdiff

Description

@atdiff

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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions