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

Inconsistent behavior for optional properties with defaults #1957

Open
1 of 2 tasks
Gero-B opened this issue Oct 22, 2024 · 5 comments
Open
1 of 2 tasks

Inconsistent behavior for optional properties with defaults #1957

Gero-B opened this issue Oct 22, 2024 · 5 comments
Assignees
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@Gero-B
Copy link

Gero-B commented Oct 22, 2024

Description

I've discovered inconsistent behavior when generating types for optional fields which also provide a default value. See sandbox for more info

Given a simple Item schema:

"Item": {
    "type": "object",
    "properties": {
        "status": { "type": "string", "nullable": true, "default": "complete" },
    }
}

Depending on where it is defined in the openapi spec file we get different results:

If we define Item in the endpoint directly without using a ref we get:

"application/json": {
    /** @default complete */
    status?: string; // correct behavior
};

If we define Item in components and use a ref in the endpoint spec the result becomes:

Item: {
    /** @default complete */
    status: string; // this is now no longer optional
};

The latter behavior is unexpected and incorrect as the field should not be required

Name Version
openapi-typescript 7.4.1
Node.js 20.12.1

Reproduction

https://codesandbox.io/p/devbox/openapi-typescript-optional-default-5zjvsz

see spec.json input file and output.d.ts result

Checklist

@Gero-B Gero-B added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Oct 22, 2024
@Gero-B
Copy link
Author

Gero-B commented Oct 22, 2024

Jus found this - #1903 seems to be the same issue

@theetrain
Copy link

theetrain commented Oct 30, 2024

Not sure if my issue is identical to this one.

I have a spec.json with the following response body spec (partially redacted)

{
  "paths": {
    "/schools": {
      "post": {
        "responses": {
          "200": {
            "description": "List of school objects",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "school_id": {
                            "type": "string",
                            "format": "uuid",
                            "description": "The school identifier",
                            "example": "acb46979-1072-41fc-920c-5577816a88bc"
                          },
                          "school_name": {
                            "type": "string",
                            "description": "The school name",
                            "example": "Super School"
                          },
// closing braces

But the generated .d.ts spec looks like this:

export interface operations {
// upper content redacted
          responses: {
            /** @description List of school objects */
            200: {
                headers: {
                    [name: string]: unknown;
                };
                content: {
                    "application/json": {
                        data?: {
                            /**
                             * Format: uuid
                             * @description The school identifier
                             * @example acb46979-1072-41fc-920c-5577816a88bc
                             */
                            school_id?: string;
                            /**
                             * @description The school name
                             * @example Super school
                             */
                            school_name?: string;
// lower content redacted
}

Though I'm not using NULLABLE in my OpanAPI spec, the output data types are shown as optional, such school_id?: string when it should be school_id: string.

@Gero-B
Copy link
Author

Gero-B commented Oct 30, 2024

Not sure if my issue is identical to this one.

I don't think it's related - I can't tell from your redacted openapi schema but it looks like the fields are not specified as required eg "required": ["school_id", "school_name"], seems to be missing. If so, it's an issue with your schema itself not this library

@theetrain
Copy link

Thanks for your reply, and you're right I am missing required: ... from the 200 response I had shared above from my OAS file. I'll open a new issue if I experience anything new.

@alex-mcleod
Copy link

alex-mcleod commented Nov 8, 2024

I am seeing this as well still with version 7.4.3. Here is the spec im using (google admin API): https://pastebin.com/4d48Kyap

Command which hangs:

npx openapi-typescript --properties-required-by-default ./sources/openapi.yaml -o ./schema.ts

Whereas this finishes basically instantly:

npx openapi-typescript ./sources/openapi.yaml -o ./schema.ts

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

4 participants