Skip to content

Commit 1f9d08f

Browse files
authored
Rename --properties-required-by-default flag (openapi-ts#1625)
1 parent 2afcb9c commit 1f9d08f

File tree

9 files changed

+76
-66
lines changed

9 files changed

+76
-66
lines changed

docs/cli.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,23 @@ Refer to the [Redocly docs](https://redocly.com/docs/cli/configuration/#resolve-
9191

9292
The following flags are supported in the CLI:
9393

94-
| Flag | Alias | Default | Description |
95-
| :------------------------ | :---- | :------: | :------------------------------------------------------------------------------------------------------------------ |
96-
| `--help` | | | Display inline help message and exit |
97-
| `--version` | | | Display this library’s version and exit |
98-
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
99-
| `--redoc [location]` | | | Path to a `redocly.yaml` file (see [Multiple schemas](#multiple-schemas)) |
100-
| `--additional-properties` | | `false` | Allow arbitrary properties for all schema objects without `additionalProperties: false` |
101-
| `--alphabetize` | | `false` | Sort types alphabetically |
102-
| `--array-length` | | `false` | Generate tuples using array `minItems` / `maxItems` |
103-
| `--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. |
105-
| `--empty-objects-unknown` | | `false` | Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties` |
106-
| `--enum` | | `false` | Generate true [TS enums](https://www.typescriptlang.org/docs/handbook/enums.html) rather than string unions. |
107-
| `--exclude-deprecated` | | `false` | Exclude deprecated fields from types |
108-
| `--export-type` | `-t` | `false` | Export `type` instead of `interface` |
109-
| `--immutable` | | `false` | Generates immutable types (readonly properties and readonly array) |
110-
| `--path-params-as-types` | | `false` | Allow dynamic string lookups on the `paths` object |
94+
| Flag | Alias | Default | Description |
95+
| :--------------------------------- | :---- | :------: | :------------------------------------------------------------------------------------------------------------------ |
96+
| `--help` | | | Display inline help message and exit |
97+
| `--version` | | | Display this library’s version and exit |
98+
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
99+
| `--redoc [location]` | | | Path to a `redocly.yaml` file (see [Multiple schemas](#multiple-schemas)) |
100+
| `--additional-properties` | | `false` | Allow arbitrary properties for all schema objects without `additionalProperties: false` |
101+
| `--alphabetize` | | `false` | Sort types alphabetically |
102+
| `--array-length` | | `false` | Generate tuples using array `minItems` / `maxItems` |
103+
| `--default-non-nullable` | | `false` | Treat schema objects with default values as non-nullable |
104+
| `--properties-required-by-default` | | `false` | Treat schema objects without `required` as having all properties required. |
105+
| `--empty-objects-unknown` | | `false` | Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties` |
106+
| `--enum` | | `false` | Generate true [TS enums](https://www.typescriptlang.org/docs/handbook/enums.html) rather than string unions. |
107+
| `--exclude-deprecated` | | `false` | Exclude deprecated fields from types |
108+
| `--export-type` | `-t` | `false` | Export `type` instead of `interface` |
109+
| `--immutable` | | `false` | Generates immutable types (readonly properties and readonly array) |
110+
| `--path-params-as-types` | | `false` | Allow dynamic string lookups on the `paths` object |
111111

112112
### pathParamsAsTypes
113113

docs/data/contributors.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

docs/scripts/update-contributors.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ const OPENAPI_TS_CONTRIBUTORS = [
145145
"davidleger95",
146146
"phk422",
147147
"mzronek",
148+
"raurfang",
149+
"JeanRemiDelteil",
150+
"TzviPM",
148151
]),
149152
];
150153

@@ -169,6 +172,11 @@ export const OPENAPI_FETCH_CONTRIBUTORS = [
169172
"darwish",
170173
"kaechele",
171174
"phk422",
175+
"mikestopcontinues",
176+
"JE-Lee",
177+
"vipentti",
178+
"armandabric",
179+
"illright",
172180
]),
173181
];
174182

packages/openapi-typescript/bin/cli.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ 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
32+
--properties-required-by-default
33+
Treat schema objects as if \`required\` is set to all properties by default
3334
--array-length Generate tuples using array minItems / maxItems
3435
--path-params-as-types Convert paths to template literal types
3536
--alphabetize Sort object keys alphabetically
@@ -70,7 +71,7 @@ const flags = parser(args, {
7071
"arrayLength",
7172
"contentNever",
7273
"defaultNonNullable",
73-
"propertiesRequired",
74+
"propertiesRequiredByDefault",
7475
"emptyObjectsUnknown",
7576
"enum",
7677
"excludeDeprecated",

packages/openapi-typescript/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export default async function openapiTS(
7070
const ctx: GlobalContext = {
7171
additionalProperties: options.additionalProperties ?? false,
7272
alphabetize: options.alphabetize ?? false,
73+
arrayLength: options.arrayLength ?? false,
7374
defaultNonNullable: options.defaultNonNullable ?? true,
74-
propertiesRequired: options.propertiesRequired ?? false,
7575
discriminators: scanDiscriminators(schema, options),
7676
emptyObjectsUnknown: options.emptyObjectsUnknown ?? false,
7777
enum: options.enum ?? false,
@@ -84,9 +84,9 @@ export default async function openapiTS(
8484
typeof options.postTransform === "function"
8585
? options.postTransform
8686
: undefined,
87+
propertiesRequiredByDefault: options.propertiesRequiredByDefault ?? false,
8788
redoc,
8889
silent: options.silent ?? false,
89-
arrayLength: options.arrayLength ?? false,
9090
transform:
9191
typeof options.transform === "function" ? options.transform : undefined,
9292
resolve($ref) {

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

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

packages/openapi-typescript/src/types.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,16 @@ export interface OpenAPITSOptions {
641641
additionalProperties?: boolean;
642642
/** Alphabetize all keys? (default: false) */
643643
alphabetize?: boolean;
644+
/** (optional) Generate tuples using array minItems / maxItems */
645+
arrayLength?: boolean;
644646
/** Allow schema objects with no specified properties to have additional properties if not expressly forbidden? (default: false) */
645647
emptyObjectsUnknown?: boolean;
646648
/** Provide current working directory (cwd) which helps resolve relative remote schemas */
647649
cwd?: PathLike;
648650
/** Should schema objects with a default value not be considered optional? */
649651
defaultNonNullable?: boolean;
650-
/** Treat all objects as if they have \`required\` set to all properties by default (default: false) */
651-
propertiesRequired?: boolean;
652+
/** Exclude deprecated fields from types? (default: false) */
653+
excludeDeprecated?: boolean;
652654
/** Manually transform certain Schema Objects with a custom TypeScript type */
653655
transform?: (
654656
schemaObject: SchemaObject,
@@ -669,12 +671,10 @@ export interface OpenAPITSOptions {
669671
exportType?: boolean;
670672
/** Export true TypeScript enums instead of unions */
671673
enum?: boolean;
672-
/** (optional) Generate tuples using array minItems / maxItems */
673-
arrayLength?: boolean;
674674
/** (optional) Substitute path parameter names with their respective types */
675675
pathParamsAsTypes?: boolean;
676-
/** Exclude deprecated fields from types? (default: false) */
677-
excludeDeprecated?: boolean;
676+
/** Treat all objects as if they have \`required\` set to all properties by default (default: false) */
677+
propertiesRequiredByDefault?: boolean;
678678
/**
679679
* Configure Redocly for validation, schema fetching, and bundling
680680
* @see https://redocly.com/docs/cli/configuration/
@@ -687,8 +687,8 @@ export interface GlobalContext {
687687
// user options
688688
additionalProperties: boolean;
689689
alphabetize: boolean;
690+
arrayLength: boolean;
690691
defaultNonNullable: boolean;
691-
propertiesRequired: boolean;
692692
discriminators: {
693693
objects: Record<string, DiscriminatorObject>;
694694
refsHandled: string[];
@@ -701,9 +701,9 @@ export interface GlobalContext {
701701
injectFooter: ts.Node[];
702702
pathParamsAsTypes: boolean;
703703
postTransform: OpenAPITSOptions["postTransform"];
704+
propertiesRequiredByDefault: boolean;
704705
redoc: RedoclyConfig;
705706
silent: boolean;
706-
arrayLength: boolean;
707707
transform: OpenAPITSOptions["transform"];
708708
/** retrieve a node by $ref */
709709
resolve<T>($ref: string): T | undefined;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const DEFAULT_CTX: GlobalContext = {
1010
alphabetize: false,
1111
arrayLength: false,
1212
defaultNonNullable: true,
13-
propertiesRequired: false,
1413
discriminators: {
1514
objects: {},
1615
refsHandled: [],
@@ -23,6 +22,7 @@ export const DEFAULT_CTX: GlobalContext = {
2322
injectFooter: [],
2423
pathParamsAsTypes: false,
2524
postTransform: undefined,
25+
propertiesRequiredByDefault: false,
2626
redoc: await createConfig({}, { extends: ["minimal"] }),
2727
resolve($ref) {
2828
return resolveRef({}, $ref, { silent: false });

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,6 @@ 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-
],
5231
[
5332
"empty",
5433
{
@@ -299,19 +278,6 @@ describe("transformSchemaObject > object", () => {
299278
},
300279
},
301280
],
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-
],
315281
[
316282
"options > immutable (string)",
317283
{
@@ -371,6 +337,40 @@ describe("transformSchemaObject > object", () => {
371337
},
372338
},
373339
],
340+
[
341+
"options > propertiesRequiredByDefault: true",
342+
{
343+
given: { type: "object", properties: { string: { type: "string" } } },
344+
want: `{
345+
string: string;
346+
}`,
347+
options: {
348+
...DEFAULT_OPTIONS,
349+
ctx: { ...DEFAULT_OPTIONS.ctx, propertiesRequiredByDefault: true },
350+
},
351+
},
352+
],
353+
[
354+
"options > propertiesRequiredByDefault: true + array",
355+
{
356+
given: {
357+
type: "object",
358+
properties: {
359+
required: { type: "boolean" },
360+
optional: { type: "boolean" },
361+
},
362+
required: ["required"],
363+
},
364+
want: `{
365+
required: boolean;
366+
optional?: boolean;
367+
}`,
368+
options: {
369+
...DEFAULT_OPTIONS,
370+
ctx: { ...DEFAULT_OPTIONS.ctx, propertiesRequiredByDefault: true },
371+
},
372+
},
373+
],
374374
[
375375
"options > two-dimensional array",
376376
{

0 commit comments

Comments
 (0)