Skip to content

Properties with default values but not in required array are generated as required in TypeScript #2489

@betovelandia

Description

@betovelandia

openapi-typescript version

7.8.0

Node.js version

22.7.0

OS + version

MacOS 26.0.1

Description

When generating TypeScript types from our OpenAPI 3.1 spec, we noticed that properties with a default value but not listed in the schema’s required array are generated as required in the resulting TypeScript types. According to the OpenAPI spec these should be optional.

Reproduction

OpenAPI Schema Example:

{
  "SomethingCreate": {
    "type": "object",
    "properties": {
      "user": { "$ref": "#/components/schemas/UReference" },
      "role": { "$ref": "#/components/schemas/RReference" },
      "reserved": {
        "type": "boolean",
        "default": false
      },
      "send_invite": {
        "type": "boolean",
        "default": false
      }
    },
    "required": ["user", "role"]
  }
}

Generated TypeScript Output:

SomethingCreate: {
  user: components["schemas"]["UReference"];
  role: components["schemas"]["RReference"];
  reserved: boolean;
  send_invite: boolean;
};

Expected result

reserved and send_invite should be optional in the generated TypeScript, since they are not in the required array.

Actual Behavior:
Both are generated as required properties.

Is this a bug or expected behavior? If it’s a bug, could you clarify if there’s a workaround or if a fix is planned?

Thank you!

Required

  • My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-tsRelevant to the openapi-typescript library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions