Skip to content

Components are generated as required they are not listed in the required array #2532

@florianmartens

Description

@florianmartens

openapi-typescript version

7.10.1

Node.js version

22.20

OS + version

macOS

Description

I'm seeing an issue in the openapi-typescript library that I'm not sure how to get around. Previously this worked fine (without components) but after moving to components I'm seeing a lot of wrongly generated types.

Reproduction

Here's an example:

{
  "components": {
    "schemas": {
      "PipesRequestConfig": {
        "type": "object",
        "properties": {
          "environment": {
            "type": "string",
            "enum": [
              "production",
              "sandbox"
            ],
            "default": "production"
          },
          "widgets": {
            "type": "object",
            "properties": {
              "enabled": {
                "type": "boolean",
                "default": false
              }
            },
            "default": {
              "enabled": false
            }
          },
          "field_definitions": {
            "type": "object",
            "properties": {
              "enabled": {
                "type": "boolean"
              }
            },
            "default": {
              "enabled": false
            },
            "required": [
              "enabled"
            ]
          },
          "transform": {
            "type": "object",
            "properties": {
              "include_fields": {
                "type": "string",
                "enum": [
                  "input",
                  "annotated"
                ]
              }
            },
            "default": {
              "include_fields": "input"
            },
            "required": [
              "include_fields"
            ]
          }
        },
        "default": {
          "environment": "production",
          "transform": {
            "include_fields": "input"
          },
          "widgets": {
            "enabled": false
          },
          "field_definitions": {
            "enabled": false
          }
        }
      }
    }
  }
}

But it is turned into the following type:

 PipesRequestConfig: {
            /**
             * @default production
             * @enum {string}
             */
            environment: "production" | "sandbox";
            /** @default {
             *       "enabled": false
             *     } */
            widgets: {
                /** @default false */
                enabled: boolean;
            };
            /** @default {
             *       "enabled": false
             *     } */
            field_definitions: {
                enabled: boolean;
            };
            /** @default {
             *       "include_fields": "input"
             *     } */
            transform: {
                /** @enum {string} */
                include_fields: "input" | "annotated";
            };
 };

Example: The widgets key is not listed as a required property. But the resulting type has it as required.I've tried explicitly setting the --properties-required-by-default=false but it did not help.

Expected result

Fields like widgets are optional.

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