Skip to content

Commit

Permalink
Add propertiesRequired option to Node.js API and CLI #1570 (#1621)
Browse files Browse the repository at this point in the history
  • Loading branch information
TzviPM committed Apr 23, 2024
1 parent a5d5836 commit 2afcb9c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ The following flags are supported in the CLI:
| `--alphabetize` | | `false` | Sort types alphabetically |
| `--array-length` | | `false` | Generate tuples using array `minItems` / `maxItems` |
| `--default-non-nullable` | | `false` | Treat schema objects with default values as non-nullable |
| `--properties-required` | | `false` | Treat schema objects without `required` as having all properties required. |
| `--empty-objects-unknown` | | `false` | Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties` |
| `--enum` | | `false` | Generate true [TS enums](https://www.typescriptlang.org/docs/handbook/enums.html) rather than string unions. |
| `--exclude-deprecated` | | `false` | Exclude deprecated fields from types |
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi-typescript/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Options
--additional-properties Treat schema objects as if \`additionalProperties: true\` is set
--empty-objects-unknown Generate \`unknown\` instead of \`Record<string, never>\` for empty objects
--default-non-nullable Set to \`false\` to ignore default values when generating non-nullable types
--properties-required Treat schema objects as if \`required\` is set to all properties by default
--array-length Generate tuples using array minItems / maxItems
--path-params-as-types Convert paths to template literal types
--alphabetize Sort object keys alphabetically
Expand Down Expand Up @@ -69,6 +70,7 @@ const flags = parser(args, {
"arrayLength",
"contentNever",
"defaultNonNullable",
"propertiesRequired",
"emptyObjectsUnknown",
"enum",
"excludeDeprecated",
Expand Down Expand Up @@ -96,6 +98,7 @@ async function generateSchema(schema, { redoc, silent = false }) {
alphabetize: flags.alphabetize,
arrayLength: flags.arrayLength,
contentNever: flags.contentNever,
propertiesRequired: flags.propertiesRequired,
defaultNonNullable: flags.defaultNonNullable,
emptyObjectsUnknown: flags.emptyObjectsUnknown,
enum: flags.enum,
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default async function openapiTS(
additionalProperties: options.additionalProperties ?? false,
alphabetize: options.alphabetize ?? false,
defaultNonNullable: options.defaultNonNullable ?? true,
propertiesRequired: options.propertiesRequired ?? false,
discriminators: scanDiscriminators(schema, options),
emptyObjectsUnknown: options.emptyObjectsUnknown ?? false,
enum: options.enum ?? false,
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ function transformSchemaObjectCore(
}
let optional =
schemaObject.required?.includes(k) ||
(schemaObject.required === undefined && options.ctx.propertiesRequired) ||
("default" in v &&
options.ctx.defaultNonNullable &&
!options.path?.includes("parameters")) // parameters can’t be required, even with defaults
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi-typescript/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ export interface OpenAPITSOptions {
cwd?: PathLike;
/** Should schema objects with a default value not be considered optional? */
defaultNonNullable?: boolean;
/** Treat all objects as if they have \`required\` set to all properties by default (default: false) */
propertiesRequired?: boolean;
/** Manually transform certain Schema Objects with a custom TypeScript type */
transform?: (
schemaObject: SchemaObject,
Expand Down Expand Up @@ -686,6 +688,7 @@ export interface GlobalContext {
additionalProperties: boolean;
alphabetize: boolean;
defaultNonNullable: boolean;
propertiesRequired: boolean;
discriminators: {
objects: Record<string, DiscriminatorObject>;
refsHandled: string[];
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-typescript/test/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const DEFAULT_CTX: GlobalContext = {
alphabetize: false,
arrayLength: false,
defaultNonNullable: true,
propertiesRequired: false,
discriminators: {
objects: {},
refsHandled: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ describe("transformSchemaObject > object", () => {
// options: DEFAULT_OPTIONS,
},
],
[
"basic > options.propertiesRequired: tre",
{
given: {
type: "object",
properties: {
required: { type: "boolean" },
optional: { type: "boolean" },
},
required: ["required"],
},
want: `{
required: boolean;
optional?: boolean;
}`,
options: {
...DEFAULT_OPTIONS,
ctx: { ...DEFAULT_OPTIONS.ctx, propertiesRequired: true },
},
},
],
[
"empty",
{
Expand Down Expand Up @@ -278,6 +299,19 @@ describe("transformSchemaObject > object", () => {
},
},
],
[
"options > propertiesRequired: true",
{
given: { type: "object", properties: { string: { type: "string" } } },
want: `{
string: string;
}`,
options: {
...DEFAULT_OPTIONS,
ctx: { ...DEFAULT_OPTIONS.ctx, propertiesRequired: true },
},
},
],
[
"options > immutable (string)",
{
Expand Down

0 comments on commit 2afcb9c

Please sign in to comment.