Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add propertiesRequired option to Node.js API and CLI #1570 #1621

Merged
merged 1 commit into from
Apr 23, 2024

Conversation

TzviPM
Copy link
Contributor

@TzviPM TzviPM commented Apr 18, 2024

Changes

Closes #1570.

Adds a propertiesRequired option to the Node.js API and global context and exposes this as --properties-required in the CLI.

More nuanced description

Schema objects are allowed to have a required property that is set to an array of keys in properties that are considered required. The default for required is [] as specified by https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-6.5.3 and adopted by openapi via https://swagger.io/specification/#data-types. This may not be the intent of many API designers, so the propertiesRequired option changes the interpretation of section 6.5.3 from

Omitting this keyword has the same behavior as an empty array.

to the following:

Omitting this keyword has the same behavior as specifying an array containing the names of all properties in the instance.

How to Review

Pay special attention to the naming of the option and cli flag. I'm open to any better names you can come up with 馃槃 My approach was inspired by the behaviour of additionalProperties and defaultNonNullable.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

Copy link

changeset-bot bot commented Apr 18, 2024

鈿狅笍 No Changeset found

Latest commit: 7670bc4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@@ -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. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor change, and this is a little more verbose, but is a little more descriptive as to what it does.

Suggested change
| `--properties-required` | | `false` | Treat schema objects without `required` as having all properties required. |
| `--properties-required-by-default` | | `false` | Treat schema objects without `required` as having all properties required. |

@@ -28,6 +28,27 @@ describe("transformSchemaObject > object", () => {
// options: DEFAULT_OPTIONS,
},
],
[
"basic > options.propertiesRequired: tre",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not particular about the naming of the test, but it should just describe it鈥檚 not just about setting the flag to true; it鈥檚 about the behavior of true + the required array

Suggested change
"basic > options.propertiesRequired: tre",
"options > propertiesRequired + required array",

given: { type: "object", properties: { string: { type: "string" } } },
want: `{
string: string;
}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃憤

Great test

@drwpow
Copy link
Contributor

drwpow commented Apr 23, 2024

This looks good! I鈥檓 going to fix CI on main, and will rebase off that when that鈥檚 fixed

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much! If it鈥檚 all right, I鈥檓 going to merge since CI is failing, and there鈥檚 a lint error, but otherwise your PR looks great.

Since your PR allowed this, I think I鈥檒l add a minor change to the flag but keep everything else as-is. Thanks! Will release this in a new release of @next

@drwpow drwpow merged commit 2afcb9c into openapi-ts:main Apr 23, 2024
1 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is there some way to generate responses as non-optional even if not 'required' in the schema?
2 participants