Skip to content

Schemas using $ref give different output to identical inlined spec when using defaults #1903

@philios33

Description

@philios33

Description

Defining an object using an external file $ref gives different typing outputs for schemas with keys that have defaults defined. Object keys are incorrectly marked as required/non optional if they contain a default and are defined within an externally $ref ed file. The same schema generates correctly without using a $ref.

Name Version
openapi-typescript 7.4.0
Node.js 20.16.0
OS + version macOS 14.5

Reproduction

spec.json
alpha.json

Gives the following typing on the /failing endpoint:

{
    /** @default false */
    bool_with_default: boolean;
    bool_with_no_default?: boolean;
    /** @default ok */
    string_with_default: string;
    string_with_no_default?: string;
};

Note, the omission of the ? optional identifier on keys that have defaults set.

Expected result

All fields should be marked as optional in both of the endpoints. Giving a field a default should not make it mandatory. Using an external file with $ref should be logically identical to an inline spec.

{
    /** @default false */
    bool_with_default?: boolean;
    bool_with_no_default?: boolean;
    /** @default ok */
    string_with_default?: string;
    string_with_no_default?: string;
}

Checklist

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions