Skip to content

Commit 96bdf10

Browse files
authored
requestBody fields optional even with defaults (#1681)
* requestBody fields optional * update tests with optional requestBody params that have default values * add requestBody test
1 parent f3a634e commit 96bdf10

7 files changed

+511
-470
lines changed

packages/openapi-typescript/examples/github-api-export-type-immutable.ts

+114-114
Large diffs are not rendered by default.

packages/openapi-typescript/examples/github-api-immutable.ts

+114-114
Large diffs are not rendered by default.

packages/openapi-typescript/examples/github-api-next.ts

+114-114
Large diffs are not rendered by default.

packages/openapi-typescript/examples/github-api.ts

+114-114
Large diffs are not rendered by default.

packages/openapi-typescript/examples/octokit-ghes-3.6-diff-to-api.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -7195,7 +7195,7 @@ export interface operations {
71957195
* @description Determines if notifications are sent when the webhook is triggered. Set to `true` to send notifications.
71967196
* @default true
71977197
*/
7198-
active: boolean;
7198+
active?: boolean;
71997199
};
72007200
};
72017201
};
@@ -7285,7 +7285,7 @@ export interface operations {
72857285
* @description Determines if notifications are sent when the webhook is triggered. Set to `true` to send notifications.
72867286
* @default true
72877287
*/
7288-
active: boolean;
7288+
active?: boolean;
72897289
};
72907290
};
72917291
};
@@ -9332,12 +9332,12 @@ export interface operations {
93329332
* @default read
93339333
* @enum {string}
93349334
*/
9335-
default_repository_permission: "read" | "write" | "admin" | "none";
9335+
default_repository_permission?: "read" | "write" | "admin" | "none";
93369336
/**
93379337
* @description Whether of non-admin organization members can create repositories. **Note:** A parameter can override this parameter. See `members_allowed_repository_creation_type` in this table for details.
93389338
* @default true
93399339
*/
9340-
members_can_create_repositories: boolean;
9340+
members_can_create_repositories?: boolean;
93419341
/** @description Whether organization members can create internal repositories, which are visible to all enterprise members. You can only allow members to create internal repositories if your organization is associated with an enterprise account using GitHub Enterprise Cloud or GitHub Enterprise Server 2.20+. For more information, see "[Restricting repository creation in your organization](https://docs.github.com/github/setting-up-and-managing-organizations-and-teams/restricting-repository-creation-in-your-organization)" in the GitHub Help documentation. */
93429342
members_can_create_internal_repositories?: boolean;
93439343
/** @description Whether organization members can create private repositories, which are visible to organization members with permission. For more information, see "[Restricting repository creation in your organization](https://docs.github.com/github/setting-up-and-managing-organizations-and-teams/restricting-repository-creation-in-your-organization)" in the GitHub Help documentation. */
@@ -9354,17 +9354,17 @@ export interface operations {
93549354
* @description Whether organization members can create GitHub Pages sites. Existing published sites will not be impacted.
93559355
* @default true
93569356
*/
9357-
members_can_create_pages: boolean;
9357+
members_can_create_pages?: boolean;
93589358
/**
93599359
* @description Whether organization members can fork private organization repositories.
93609360
* @default false
93619361
*/
9362-
members_can_fork_private_repositories: boolean;
9362+
members_can_fork_private_repositories?: boolean;
93639363
/**
93649364
* @description Whether contributors to organization repositories are required to sign off on commits they make through GitHub's web interface.
93659365
* @default false
93669366
*/
9367-
web_commit_signoff_required: boolean;
9367+
web_commit_signoff_required?: boolean;
93689368
/** @example "http://github.blog" */
93699369
blog?: string;
93709370
};
@@ -9795,7 +9795,7 @@ export interface operations {
97959795
* @default pull
97969796
* @enum {string}
97979797
*/
9798-
permission: "pull" | "push";
9798+
permission?: "pull" | "push";
97999799
/** @description The ID of a team to set as the parent team. */
98009800
parent_team_id?: number;
98019801
/** @description The [distinguished name](https://www.ldap.com/ldap-dns-and-rdns) (DN) of the LDAP entry to map to a team. LDAP synchronization must be enabled to map LDAP entries to a team. Use the "[Update LDAP mapping for a team](https://docs.github.com/[email protected]/rest/reference/enterprise-admin#update-ldap-mapping-for-a-team)" endpoint to change the LDAP DN. For more information, see "[Using LDAP](https://docs.github.com/[email protected]/admin/identity-and-access-management/authenticating-users-for-your-github-enterprise-server-instance/using-ldap#enabling-ldap-sync)." */
@@ -10240,7 +10240,7 @@ export interface operations {
1024010240
* @description Whether this autolink reference matches alphanumeric characters. If true, the `<num>` parameter of the `url_template` matches alphanumeric characters `A-Z` (case insensitive), `0-9`, and `-`. If false, this autolink reference only matches numeric characters.
1024110241
* @default true
1024210242
*/
10243-
is_alphanumeric: boolean;
10243+
is_alphanumeric?: boolean;
1024410244
};
1024510245
};
1024610246
};
@@ -10771,17 +10771,17 @@ export interface operations {
1077110771
* @description `true` to create a draft (unpublished) release, `false` to create a published one.
1077210772
* @default false
1077310773
*/
10774-
draft: boolean;
10774+
draft?: boolean;
1077510775
/**
1077610776
* @description `true` to identify the release as a prerelease. `false` to identify the release as a full release.
1077710777
* @default false
1077810778
*/
10779-
prerelease: boolean;
10779+
prerelease?: boolean;
1078010780
/**
1078110781
* @description Whether to automatically generate the name and body for this release. If `name` is specified, the specified name will be used; otherwise, a name will be automatically generated. If `body` is specified, the body will be pre-pended to the automatically generated notes.
1078210782
* @default false
1078310783
*/
10784-
generate_release_notes: boolean;
10784+
generate_release_notes?: boolean;
1078510785
};
1078610786
};
1078710787
};

packages/openapi-typescript/src/transform/schema-object.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,10 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
454454
let optional =
455455
schemaObject.required?.includes(k) ||
456456
(schemaObject.required === undefined && options.ctx.propertiesRequiredByDefault) ||
457-
("default" in v && options.ctx.defaultNonNullable && !options.path?.includes("parameters")) // parameters can’t be required, even with defaults
457+
("default" in v &&
458+
options.ctx.defaultNonNullable &&
459+
!options.path?.includes("parameters") &&
460+
!options.path?.includes("requestBody")) // parameters can’t be required, even with defaults
458461
? undefined
459462
: QUESTION_TOKEN;
460463
let type =

packages/openapi-typescript/test/transform/request-body-object.test.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,45 @@ describe("transformRequestBodyObject", () => {
4848
"*/*"?: never;
4949
};
5050
}`,
51-
// options: DEFAULT_OPTIONS,
51+
},
52+
],
53+
[
54+
"POST data with default values",
55+
{
56+
given: {
57+
content: {
58+
"application/x-www-form-urlencoded": {
59+
schema: {
60+
type: "object",
61+
properties: {
62+
required: { type: "string" },
63+
optional: { type: "string" },
64+
flag_optional: { type: "boolean", default: false },
65+
flag_required: { type: "boolean", default: false },
66+
},
67+
required: ["required", "flag_required"],
68+
},
69+
},
70+
},
71+
required: true,
72+
description: "description",
73+
},
74+
want: `{
75+
content: {
76+
"application/x-www-form-urlencoded": {
77+
required: string;
78+
optional?: string;
79+
/** @default false */
80+
flag_optional?: boolean;
81+
/** @default false */
82+
flag_required: boolean;
83+
};
84+
};
85+
}`,
86+
options: {
87+
path: "#/paths/~post-item/post/requestBody/application~1x-www-form-urlencoded",
88+
ctx: { ...DEFAULT_CTX },
89+
},
5290
},
5391
],
5492
];

0 commit comments

Comments
 (0)