Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional parameters with default are generated as required #2012

Open
2 tasks
isweetland opened this issue Nov 19, 2024 · 1 comment
Open
2 tasks

Optional parameters with default are generated as required #2012

isweetland opened this issue Nov 19, 2024 · 1 comment
Assignees
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@isweetland
Copy link

Description

An optional field in the openapi schema that includes the default property is generated as required in typescript

Name Version
openapi-typescript 7.4.3
Node.js 18.19
OS + version macOS 14.2.1

Reproduction

The following openapi schema is used:

{
  "openapi": "3.1.0",
  "info": {
    "title": "Config Service",
    "version": "local"
  },
  "paths": {
    "/config": {
      "post": {
        "summary": "Set Config",
        "operationId": "set_config_v1_config_post",
        "parameters": [],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "anyOf": [
                  {
                    "$ref": "#/components/schemas/ConfigIn"
                  },
                  {
                    "type": "null"
                  }
                ],
                "title": "Params"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "null",
                  "title": "Response Set Config Post"
                }
              }
            }
          }
        }
      },
    }
  },
  "components": {
    "schemas": {
      "ConfigIn": {
        "properties": {
          "interval": {
            "anyOf": [
              {
                "type": "integer"
              },
              {
                "type": "null"
              }
            ],
            "title": "Interval",
            "default": 1
          },
          "start_at": {
            "anyOf": [
              {
                "type": "string",
                "format": "date-time"
              },
              {
                "type": "null"
              }
            ],
            "title": "Start At"
          }
        },
        "type": "object",
        "required": [],
        "title": "ConfigIn"
      }
    }
  }
}

Running npx openapi-typescript ./schema.json -o ./types.ts

The following type for ConfigIn is generated:

ConfigIn: {
    /**
     * Interval
     * @default 1
     */
    interval: number | null;
    /** Start At */
    start_at?: string | null;
};

Expected result

I expect interval to be marked optional as well

ConfigIn: {
    /**
     * Interval
     * @default 1
     */
    interval?: number | null;
    /** Start At */
    start_at?: string | null;
};

Checklist

@isweetland isweetland added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Nov 19, 2024
@isweetland
Copy link
Author

Possible duplicate of #1957

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
Development

No branches or pull requests

2 participants