Skip to content

bug: request body fields with default values are treated as required #1741

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

Closed
1 of 2 tasks
ysemennikov opened this issue Jul 3, 2024 · 4 comments · Fixed by #1746
Closed
1 of 2 tasks

bug: request body fields with default values are treated as required #1741

ysemennikov opened this issue Jul 3, 2024 · 4 comments · Fixed by #1746
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@ysemennikov
Copy link

Description

We have generated TS interfaces using our OpenAPI 3.1.0 specification. There are some parameters that have default values (so they are not required in request, and the body without them will be still valid):

image

However, these parameters are treated as required when generating paths:

image
Name Version
openapi-typescript 7.0.1
Node.js 20.13.1
OS + version macOS Sonoma 14.4

Reproduction

Try to use this openapi schema:

"src__schemas__social_profiles__Create": {
  "properties": {
    "card_title": {
      "type": "string",
      "maxLength": 64,
      "minLength": 1,
      "title": "Card Title",
      "default": "Social Profile"
    },
    "template": {
      "type": "string",
      "maxLength": 64,
      "minLength": 1,
      "title": "Template"
    },
    "photo": {
      "anyOf": [
        {
          "type": "string",
          "maxLength": 2083,
          "minLength": 1,
          "format": "uri"
        },
        {
          "const": ""
        }
      ],
      "title": "Photo",
      "default": ""
    },
    "signature": {
      "anyOf": [
        {
          "type": "string",
          "maxLength": 2083,
          "minLength": 1,
          "format": "uri"
        },
        {
          "const": ""
        }
      ],
      "title": "Signature",
      "default": ""
    },
    "design": {
      "type": "object",
      "title": "Design",
      "default": {

      }
    },
    "details": {
      "type": "object",
      "title": "Details",
      "default": {

      }
    },
    "socials": {
      "type": "object",
      "title": "Socials",
      "default": {

      }
    },
    "meta": {
      "type": "object",
      "title": "Meta",
      "default": {

      }
    }
  },
  "type": "object",
  "required": [
    "template"
  ],
  "title": "Create",
  "description": "Social Profile schema for create."
}

You can see that default values are set, but the fields are not recognized as not required.

Expected result

Parameters that have default values have | undefined in type declaration, like

type SocialProfileCreate = {
    card_title: string;
    template: string;
    photo?: string | "";
    signature?: string | "";
    design?: Record<string, never>;
    details?: Record<string, never>;
    socials?: Record<string, never>;
    meta?: Record<string, never>;
}

Checklist

@ysemennikov ysemennikov added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Jul 3, 2024
@phk422
Copy link
Contributor

phk422 commented Jul 4, 2024

Description

We have generated TS interfaces using our OpenAPI 3.1.0 specification. There are some parameters that have default values (so they are not required in request, and the body without them will be still valid):

image However, these parameters are treated as required when generating paths: image Name Version `openapi-typescript` `7.0.1` Node.js `20.13.1` OS + version `macOS Sonoma 14.4` **Reproduction**

Try to use this openapi schema:

"src__schemas__social_profiles__Create": {
  "properties": {
    "card_title": {
      "type": "string",
      "maxLength": 64,
      "minLength": 1,
      "title": "Card Title",
      "default": "Social Profile"
    },
    "template": {
      "type": "string",
      "maxLength": 64,
      "minLength": 1,
      "title": "Template"
    },
    "photo": {
      "anyOf": [
        {
          "type": "string",
          "maxLength": 2083,
          "minLength": 1,
          "format": "uri"
        },
        {
          "const": ""
        }
      ],
      "title": "Photo",
      "default": ""
    },
    "signature": {
      "anyOf": [
        {
          "type": "string",
          "maxLength": 2083,
          "minLength": 1,
          "format": "uri"
        },
        {
          "const": ""
        }
      ],
      "title": "Signature",
      "default": ""
    },
    "design": {
      "type": "object",
      "title": "Design",
      "default": {

      }
    },
    "details": {
      "type": "object",
      "title": "Details",
      "default": {

      }
    },
    "socials": {
      "type": "object",
      "title": "Socials",
      "default": {

      }
    },
    "meta": {
      "type": "object",
      "title": "Meta",
      "default": {

      }
    }
  },
  "type": "object",
  "required": [
    "template"
  ],
  "title": "Create",
  "description": "Social Profile schema for create."
}

You can see that default values are set, but the fields are not recognized as not required.

Expected result

Parameters that have default values have | undefined in type declaration, like

type SocialProfileCreate = {
    card_title: string;
    template: string;
    photo?: string | "";
    signature?: string | "";
    design?: Record<string, never>;
    details?: Record<string, never>;
    socials?: Record<string, never>;
    meta?: Record<string, never>;
}

Checklist

Hi, @ysemennikov ! The results I generated using your scheme test were correct.
image

@duncanbeevers
Copy link
Contributor

@phk422 We're seeing this issue as well. Could the difference be that the requestBody is using a referenced component schema rather than an inline schema?

@ysemennikov
Copy link
Author

@phk422 We're seeing this issue as well. Could the difference be that the requestBody is using a referenced component schema rather than an inline schema?

Hi @phk422, the requestBody in our OpenAPI schema was also referenced, not inline. Maybe this really makes sense?

@phk422
Copy link
Contributor

phk422 commented Jul 5, 2024

@phk422 We're seeing this issue as well. Could the difference be that the requestBody is using a referenced component schema rather than an inline schema?

Hi @phk422, the requestBody in our OpenAPI schema was also referenced, not inline. Maybe this really makes sense?

Ah, yes, I overlooked it. I already submitted a pr to fix it!

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
3 participants