Denitsa
Denitsa

Reputation: 111

How to validate nested properties in component schema (openapi 3 in Postman)

I'm working on an OpenAPI 3 schema.

I would like to use a data model from the components.schemas inside the responses content and have some required nested properties inside that data model. However, it doesn't seem like the required validation is being applied. I'm testing this in Postman with a mock server.

Here is my schema:

{
    "openapi": "3.0.0",
    "info": {
        "version": "1.0.0",
        "title": "Usage stats API"
    },
    "servers": [
        {
            "url": "http://some-middleware-endpoint.com"
        }
    ],
    "paths": {
        "/publishers/{publisherId}/files/{fileId}": {
            "get": {
                "summary": "Get single file for publisher",
                "parameters": [
                    {
                        "name": "publisherId",
                        "in": "path",
                        "description": "ID of the publisher",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    },
                    {
                        "name": "fileId",
                        "in": "path",
                        "description": "ID of the file",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "File for publisher",
                        "headers": {
                            "Content-Type": {
                                "description": "application/json"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "required": [
                                        "meta"
                                    ],
                                    "properties": {
                                        "meta": {
                                            "type": "object",
                                            "required": ["page"],
                                            "properties": {
                                                "$ref": "#/components/schemas/Pagination"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Pagination": {
                "properties": {
                    "page": {
                        "required": ["current-page", "per-page", "from", "to", "total", "last-page"],
                        "type": "object",
                        "properties": {
                            "current-page": {
                                "type": "integer"
                            },
                            "per-page": {
                                "type": "integer"
                            },
                            "from": {
                                "type": "integer"
                            },
                            "to": {
                                "type": "integer"
                            },
                            "total": {
                                "type": "integer"
                            },
                            "last-page": {
                                "type": "integer"
                            }
                        }
                    }
                }
            }

        }
    }
}

This response passes validation:

{
    "meta": {
        "page": {}
    }
}

Even though all of the attributes I've required ("required": ["current-page", "per-page", "from", "to", "total", "last-page"]) are not present.

Basically, I would like page and all its nested properties to be required.

I guess I'm doing something wrong in defining the properties. Any help is appreciated!

Upvotes: 3

Views: 1135

Answers (1)

Denitsa
Denitsa

Reputation: 111

Oh well, I guess my issue was pulling up the $ref one level up.

The following seems to work inside responses.content.

"meta": {
    "type": "object",
    "required": [
        "page"
    ],
    "$ref": "#/components/schemas/Pagination"
}

instead of

"meta": {
    "type": "object",
    "required": ["page"],
    "properties": {
        "$ref": "#/components/schemas/Pagination"
    }
}

Upvotes: 2

Related Questions