Skip to content

Commit 23c980c

Browse files
authored
Merge pull request #2678 from hey-api/feat/transform-properties-required-by-default
fix(parser): add propertiesRequiredByDefault transform option
2 parents 6476ecb + 0ed8796 commit 23c980c

File tree

21 files changed

+25219
-92
lines changed

21 files changed

+25219
-92
lines changed

.changeset/chilled-pumas-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix(parser): add `propertiesRequiredByDefault` transform option

docs/openapi-ts/configuration/parser.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,40 @@ export default {
376376

377377
You can customize the naming and casing pattern for `enums` schemas using the `.name` and `.case` options.
378378

379+
### Properties required by default
380+
381+
By default, any object schema with a missing `required` keyword is interpreted as "no properties are required." This is the correct behavior according to the OpenAPI standard. However, some specifications interpret a missing `required` keyword as "all properties should be required."
382+
383+
This option allows you to change the default behavior so that properties are required by default unless explicitly marked as optional.
384+
385+
::: code-group
386+
387+
```js [default]
388+
export default {
389+
input: 'hey-api/backend', // sign up at app.heyapi.dev
390+
output: 'src/client',
391+
parser: {
392+
transforms: {
393+
propertiesRequiredByDefault: false, // [!code ++]
394+
},
395+
},
396+
};
397+
```
398+
399+
```js [required]
400+
export default {
401+
input: 'hey-api/backend', // sign up at app.heyapi.dev
402+
output: 'src/client',
403+
parser: {
404+
transforms: {
405+
propertiesRequiredByDefault: true, // [!code ++]
406+
},
407+
},
408+
};
409+
```
410+
411+
:::
412+
379413
### Read-write
380414

381415
Your schemas might contain read-only or write-only fields. Using such schemas directly could mean asking the user to provide a read-only field in requests, or expecting a write-only field in responses. We separate schemas for requests and responses if direct usage would result in such scenarios.

packages/openapi-ts-tests/main/test/3.0.x.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,19 @@ describe(`OpenAPI ${version}`, () => {
635635
}),
636636
description: 'transforms an array',
637637
},
638+
{
639+
config: createConfig({
640+
input: 'dutchie.json',
641+
output: 'transforms-properties-required-by-default',
642+
parser: {
643+
transforms: {
644+
propertiesRequiredByDefault: true,
645+
},
646+
},
647+
plugins: ['@hey-api/typescript'],
648+
}),
649+
description: 'makes all object properties required by default',
650+
},
638651
{
639652
config: createConfig({
640653
input: 'type-invalid.json',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type * from './types.gen';

0 commit comments

Comments
 (0)