openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Optional parameters with `default` are generated as required

Open isweetland opened this issue 1 year ago • 3 comments

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 avatar Nov 19 '24 18:11 isweetland

Possible duplicate of https://github.com/openapi-ts/openapi-typescript/issues/1957

isweetland avatar Nov 19 '24 18:11 isweetland

I'm currently handling this bug with post-processing. I wrote a janky Python script to parse the generated schema.d.ts file and fix the output for the cases described in this issue

It'd be great to get this fix into the library!

kylebebak avatar Jan 14 '25 21:01 kylebebak

Add the flag --default-non-nullable false when generating schema.

mgfinch avatar Jan 29 '25 22:01 mgfinch

He is right, `--default-non-nullable false' solves the problem. I think this is intended, because this default value is not essentially set on the code end, after all.

suiramdev avatar Apr 01 '25 14:04 suiramdev

My code got bitten by this issue. I was previously using version 6 or earlier. The --default-non-nullable false fix worked.

Respectfully, I'd like to understand why this new command line parameter is needed. I checked a few versions, and it appears versions 7.0 through 7.8 now convert optional properties with a default value to required, unless I specify --default-non-nullable false. Shouldn't optional properties remain optional, regardless of the existence of a default value?

hathawsh avatar May 20 '25 05:05 hathawsh