Skip to content

Commit 2afcb9c

Browse files
authored
Add propertiesRequired option to Node.js API and CLI #1570 (#1621)
1 parent a5d5836 commit 2afcb9c

File tree

7 files changed

+44
-0
lines changed

7 files changed

+44
-0
lines changed

docs/cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ The following flags are supported in the CLI:
101101
| `--alphabetize` | | `false` | Sort types alphabetically |
102102
| `--array-length` | | `false` | Generate tuples using array `minItems` / `maxItems` |
103103
| `--default-non-nullable` | | `false` | Treat schema objects with default values as non-nullable |
104+
| `--properties-required` | | `false` | Treat schema objects without `required` as having all properties required. |
104105
| `--empty-objects-unknown` | | `false` | Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties` |
105106
| `--enum` | | `false` | Generate true [TS enums](https://www.typescriptlang.org/docs/handbook/enums.html) rather than string unions. |
106107
| `--exclude-deprecated` | | `false` | Exclude deprecated fields from types |

packages/openapi-typescript/bin/cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Options
2929
--additional-properties Treat schema objects as if \`additionalProperties: true\` is set
3030
--empty-objects-unknown Generate \`unknown\` instead of \`Record<string, never>\` for empty objects
3131
--default-non-nullable Set to \`false\` to ignore default values when generating non-nullable types
32+
--properties-required Treat schema objects as if \`required\` is set to all properties by default
3233
--array-length Generate tuples using array minItems / maxItems
3334
--path-params-as-types Convert paths to template literal types
3435
--alphabetize Sort object keys alphabetically
@@ -69,6 +70,7 @@ const flags = parser(args, {
6970
"arrayLength",
7071
"contentNever",
7172
"defaultNonNullable",
73+
"propertiesRequired",
7274
"emptyObjectsUnknown",
7375
"enum",
7476
"excludeDeprecated",
@@ -96,6 +98,7 @@ async function generateSchema(schema, { redoc, silent = false }) {
9698
alphabetize: flags.alphabetize,
9799
arrayLength: flags.arrayLength,
98100
contentNever: flags.contentNever,
101+
propertiesRequired: flags.propertiesRequired,
99102
defaultNonNullable: flags.defaultNonNullable,
100103
emptyObjectsUnknown: flags.emptyObjectsUnknown,
101104
enum: flags.enum,

packages/openapi-typescript/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export default async function openapiTS(
7171
additionalProperties: options.additionalProperties ?? false,
7272
alphabetize: options.alphabetize ?? false,
7373
defaultNonNullable: options.defaultNonNullable ?? true,
74+
propertiesRequired: options.propertiesRequired ?? false,
7475
discriminators: scanDiscriminators(schema, options),
7576
emptyObjectsUnknown: options.emptyObjectsUnknown ?? false,
7677
enum: options.enum ?? false,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ function transformSchemaObjectCore(
479479
}
480480
let optional =
481481
schemaObject.required?.includes(k) ||
482+
(schemaObject.required === undefined && options.ctx.propertiesRequired) ||
482483
("default" in v &&
483484
options.ctx.defaultNonNullable &&
484485
!options.path?.includes("parameters")) // parameters can’t be required, even with defaults

packages/openapi-typescript/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,8 @@ export interface OpenAPITSOptions {
647647
cwd?: PathLike;
648648
/** Should schema objects with a default value not be considered optional? */
649649
defaultNonNullable?: boolean;
650+
/** Treat all objects as if they have \`required\` set to all properties by default (default: false) */
651+
propertiesRequired?: boolean;
650652
/** Manually transform certain Schema Objects with a custom TypeScript type */
651653
transform?: (
652654
schemaObject: SchemaObject,
@@ -686,6 +688,7 @@ export interface GlobalContext {
686688
additionalProperties: boolean;
687689
alphabetize: boolean;
688690
defaultNonNullable: boolean;
691+
propertiesRequired: boolean;
689692
discriminators: {
690693
objects: Record<string, DiscriminatorObject>;
691694
refsHandled: string[];

packages/openapi-typescript/test/test-helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const DEFAULT_CTX: GlobalContext = {
1010
alphabetize: false,
1111
arrayLength: false,
1212
defaultNonNullable: true,
13+
propertiesRequired: false,
1314
discriminators: {
1415
objects: {},
1516
refsHandled: [],

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ describe("transformSchemaObject > object", () => {
2828
// options: DEFAULT_OPTIONS,
2929
},
3030
],
31+
[
32+
"basic > options.propertiesRequired: tre",
33+
{
34+
given: {
35+
type: "object",
36+
properties: {
37+
required: { type: "boolean" },
38+
optional: { type: "boolean" },
39+
},
40+
required: ["required"],
41+
},
42+
want: `{
43+
required: boolean;
44+
optional?: boolean;
45+
}`,
46+
options: {
47+
...DEFAULT_OPTIONS,
48+
ctx: { ...DEFAULT_OPTIONS.ctx, propertiesRequired: true },
49+
},
50+
},
51+
],
3152
[
3253
"empty",
3354
{
@@ -278,6 +299,19 @@ describe("transformSchemaObject > object", () => {
278299
},
279300
},
280301
],
302+
[
303+
"options > propertiesRequired: true",
304+
{
305+
given: { type: "object", properties: { string: { type: "string" } } },
306+
want: `{
307+
string: string;
308+
}`,
309+
options: {
310+
...DEFAULT_OPTIONS,
311+
ctx: { ...DEFAULT_OPTIONS.ctx, propertiesRequired: true },
312+
},
313+
},
314+
],
281315
[
282316
"options > immutable (string)",
283317
{

0 commit comments

Comments
 (0)