Skip to content

Query Parameters Are Not Marked As required: true When Using Ref #902

@cmcnicholas

Description

@cmcnicholas

Prerequisites

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

Fastify version

5.6.2

Plugin version

9.6.0

Node.js version

24.11.0

Operating system

Windows

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

10

Description

When using a schema reference with fastify query model definition the resulting openapi.json does not specify that the property is required.

// 1. define some sort of reusable schema
fastify.addSchema({
  $id: "CoringUploadTypeApiModel",
  type: "string",
  enum: ["health_safety", "coring"],
});

fastify.get(
  "/some-route",
  {
    schema: {
    query: {
        type: "object",
        // 2. specify thing property is required
        required: ["thing"],
        properties: {
        // 3. define thing property with $ref to reusable schema
        thing: {
            anyOf: [
            { type: "string", const: "health_safety" },
            { type: "string", const: "coring" },
            ],
            $ref: "CoringUploadTypeApiModel",
        },
        other: { type: "string" },
        },
    },
    response: {
        200: {
        type: "object",
        properties: {
            hello: { type: "string" },
        },
        },
    },
    },
  },
  (req, reply) => {
    reply.code(200).send({ hello: "world" });
  });

the resulting model definition when calling .swagger() is:

{
  "openapi": "3.0.0",
  "info": {
    "version": "9.6.1",
    "title": "@fastify/swagger"
  },
  "components": {
    "schemas": {
      "def-0": {
        "type": "string",
        "enum": [
          "health_safety",
          "coring"
        ],
        "title": "CoringUploadTypeApiModel"
      }
    }
  },
  "paths": {
    "/some-route": {
      "get": {
        "parameters": [
          {
            "schema": {
              "$ref": "#/components/schemas/def-0"
            },
            "in": "query",
            "name": "thing"
          },
          {
            "schema": {
              "type": "string"
            },
            "in": "query",
            "name": "other",
            "required": false
          }
        ],
        "responses": {
          "200": {
            "description": "Default Response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "hello": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

notice how thing does not have required: true

Link to code that reproduces the bug

https://github.com/cmcnicholas/fastify-optional-query-param-ref/blob/main/index.js

Expected Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions