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

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

Open
2 tasks done
philios33 opened this issue Sep 5, 2024 · 2 comments
Open
2 tasks done
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@philios33
Copy link

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

@philios33 philios33 added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Sep 5, 2024
@philios33
Copy link
Author

philios33 commented Sep 5, 2024

Gah, I just found a similar closed ticket that references this merged PR
#1746

All problems go away when I use --default-non-nullable false which is weird. I don't even use nullable anywhere, plus the value for this param should default to false according to the latest docs.

@philios33
Copy link
Author

philios33 commented Sep 7, 2024

I've taken a look at the code, and basically the whole --default-non-nullable option now defaults to true (since v7), but the feature is essentially useless. It has some logic to attempt to disable it if you are within parameters, requestBody or requestBodies (inputs), but that does not work if you are using a schema $ref.

This feature seems to cater for people who are using defaults on their OUTPUT schemas to make fields mandatory in their output types. However, the default key is only ever used with api INPUTS to specify to the consumer what the value would be if you omit it.

Please can somebody explain the point of this feature (now enabled by default)?

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
None yet
Development

No branches or pull requests

1 participant