Skip to content

Error: token "definitions" does not exist #524

@Fdawgs

Description

@Fdawgs

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.25.3

Plugin version

4.12.1

Node.js version

16.13.0

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

Upon upgrading from 4.12.0 to ^4.12.1, components.schemas.def-0 is no longer populated with shared schemas/definitions.
This is leading to Invalid reference token: definitions being thrown by Redoc, or token "definitions" does not exist by other validators, when attempting to load the schema.

Steps to Reproduce

This issue is present in Fdawgs/ydh-myydh-crud-api.
The shared schemas are held in a plugin.

4.12.0 example

In v4.12.0 components.schemas.def-0 looks like so:

"def-0": {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "unauthorized": {
            "type": "object",
            "title": "401 Unauthorized",
            "properties": {
                "statusCode": {
                    "type": "number",
                    "const": 401
                },
                "error": {
                    "type": "string",
                    "const": "Unauthorized"
                },
                "message": {
                    "type": "string",
                    "examples": ["missing authorization header"]
                }
            }
        },
        "notFoundDbResults": {
            "type": "object",
            "title": "404 Not Found Response",
            "properties": {
                "statusCode": {
                    "type": "number",
                    "const": 404
                },
                "error": {
                    "type": "string",
                    "const": "Not Found"
                },
                "message": {
                    "type": "string",
                    "enum": [
                        "Invalid or expired search results",
                        "Record does not exist or has already been deleted",
                        "User not found"
                    ]
                }
            }
        },
        "notAcceptable": {
            "type": "object",
            "title": "406 Not Acceptable Response",
            "properties": {
                "statusCode": {
                    "type": "number",
                    "const": 406
                },
                "error": {
                    "type": "string",
                    "const": "Not Acceptable"
                },
                "message": {
                    "type": "string",
                    "const": "Not Acceptable"
                }
            }
        },
        "tooManyRequests": {
            "type": "object",
            "title": "429 Too Many Requests Response",
            "properties": {
                "statusCode": {
                    "type": "number",
                    "const": 429
                },
                "error": {
                    "type": "string",
                    "const": "Too Many Requests"
                },
                "message": {
                    "type": "string",
                    "examples": [
                        "Rate limit exceeded, retry in 1 minute"
                    ]
                }
            }
        },
        "internalServerError": {
            "type": "object",
            "title": "500 Internal Server Error Response",
            "properties": {
                "statusCode": {
                    "type": "number",
                    "const": 500
                },
                "error": {
                    "type": "string",
                    "const": "Internal Server Error"
                },
                "message": {
                    "type": "string",
                    "examples": [
                        "Unable to delete read receipt from database",
                        "Unable to return result(s) from database",
                        "Unable to update patient preference in database",
                        "Unable to update read receipt in database"
                    ]
                }
            }
        },
        "serviceUnavailable": {
            "type": "object",
            "title": "503 Service Unavailable",
            "properties": {
                "statusCode": {
                    "type": "number",
                    "const": 503
                },
                "code": {
                    "type": "string",
                    "const": "FST_UNDER_PRESSURE"
                },
                "error": {
                    "type": "string",
                    "const": "Service Unavailable"
                },
                "message": {
                    "type": "string",
                    "const": "Service Unavailable"
                }
            }
        }
    },
    "type": "object",
    "title": "Responses",
    "description": "Common response schemas"
},

^4.12.1 example

When launching the API with any version of fastify-swagger ^4.12.1, components.schemas looks like so:

"schemas": {
    "def-0": {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "title": "Responses",
        "description": "Common response schemas"
    },
    "def-1": {
        "type": "object",
        "title": "401 Unauthorized",
        "properties": {
            "statusCode": {
                "type": "number",
                "const": 401
            },
            "error": {
                "type": "string",
                "const": "Unauthorized"
            },
            "message": {
                "type": "string",
                "example": "missing authorization header"
            }
        }
    },
}

As you can see above, it seems the definitions have moved outside of the original def-0 object.
The route schemas still refer to non-existent definitions at def-0:

"406": {
    "description": "Not Acceptable",
    "content": {
      "text/plain": {
        "schema": {
          "$ref": "#/components/schemas/def-0/definitions/notAcceptable",
          "description": "Not Acceptable"
        }
      }
    }
  },

Expected Behavior

$refs to refer to correct definitions, either by adding them back to def-0 or by referring to def-*

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions