Skip to content

Commit 787a244

Browse files
authored
fix: openapi - do not generate "id" field in create input if the field has default value (#758)
1 parent 033d95d commit 787a244

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

packages/plugins/openapi/src/rest-generator.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -867,20 +867,36 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
867867
}
868868
}
869869

870+
const toplevelRequired = ['type', 'attributes'];
871+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
872+
let properties: any = {
873+
type: { type: 'string' },
874+
attributes: {
875+
type: 'object',
876+
required: required.length > 0 ? required : undefined,
877+
properties: attributes,
878+
},
879+
};
880+
881+
if (mode === 'create') {
882+
// 'id' is required if there's no default value
883+
const idField = model.fields.find((f) => isIdField(f));
884+
if (idField && !hasAttribute(idField, '@default')) {
885+
properties = { id: { type: 'string' }, ...properties };
886+
toplevelRequired.unshift('id');
887+
}
888+
} else {
889+
// 'id' always required for read and update
890+
properties = { id: { type: 'string' }, ...properties };
891+
toplevelRequired.unshift('id');
892+
}
893+
870894
// eslint-disable-next-line @typescript-eslint/no-explicit-any
871895
const result: any = {
872896
type: 'object',
873897
description: `The "${model.name}" model`,
874-
required: ['id', 'type', 'attributes'],
875-
properties: {
876-
type: { type: 'string' },
877-
id: { type: 'string' },
878-
attributes: {
879-
type: 'object',
880-
required: required.length > 0 ? required : undefined,
881-
properties: attributes,
882-
},
883-
},
898+
required: toplevelRequired,
899+
properties,
884900
} satisfies OAPI.SchemaObject;
885901

886902
if (Object.keys(relationships).length > 0) {

packages/plugins/openapi/tests/baseline/rest-type-coverage.baseline.yaml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,10 @@ components:
590590
- type
591591
- attributes
592592
properties:
593-
type:
594-
type: string
595593
id:
596594
type: string
595+
type:
596+
type: string
597597
attributes:
598598
type: object
599599
properties:
@@ -628,14 +628,11 @@ components:
628628
type: object
629629
description: The "Foo" model
630630
required:
631-
- id
632631
- type
633632
- attributes
634633
properties:
635634
type:
636635
type: string
637-
id:
638-
type: string
639636
attributes:
640637
type: object
641638
required:
@@ -685,10 +682,10 @@ components:
685682
- type
686683
- attributes
687684
properties:
688-
type:
689-
type: string
690685
id:
691686
type: string
687+
type:
688+
type: string
692689
attributes:
693690
type: object
694691
properties:

packages/plugins/openapi/tests/baseline/rest.baseline.yaml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,10 +1525,10 @@ components:
15251525
- type
15261526
- attributes
15271527
properties:
1528-
type:
1529-
type: string
15301528
id:
15311529
type: string
1530+
type:
1531+
type: string
15321532
attributes:
15331533
type: object
15341534
properties:
@@ -1557,14 +1557,11 @@ components:
15571557
type: object
15581558
description: The "User" model
15591559
required:
1560-
- id
15611560
- type
15621561
- attributes
15631562
properties:
15641563
type:
15651564
type: string
1566-
id:
1567-
type: string
15681565
attributes:
15691566
type: object
15701567
required:
@@ -1602,10 +1599,10 @@ components:
16021599
- type
16031600
- attributes
16041601
properties:
1605-
type:
1606-
type: string
16071602
id:
16081603
type: string
1604+
type:
1605+
type: string
16091606
attributes:
16101607
type: object
16111608
properties:
@@ -1689,10 +1686,10 @@ components:
16891686
- type
16901687
- attributes
16911688
properties:
1692-
type:
1693-
type: string
16941689
id:
16951690
type: string
1691+
type:
1692+
type: string
16961693
attributes:
16971694
type: object
16981695
properties:
@@ -1729,10 +1726,10 @@ components:
17291726
- type
17301727
- attributes
17311728
properties:
1732-
type:
1733-
type: string
17341729
id:
17351730
type: string
1731+
type:
1732+
type: string
17361733
attributes:
17371734
type: object
17381735
required:
@@ -1774,10 +1771,10 @@ components:
17741771
- type
17751772
- attributes
17761773
properties:
1777-
type:
1778-
type: string
17791774
id:
17801775
type: string
1776+
type:
1777+
type: string
17811778
attributes:
17821779
type: object
17831780
properties:

packages/plugins/openapi/tests/baseline/rpc.baseline.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ components:
352352
posts:
353353
$ref: '#/components/schemas/Post_ItemCreateNestedManyWithoutAuthorInput'
354354
required:
355-
- id
356355
- email
357356
UserUpdateInput:
358357
type: object
@@ -397,7 +396,6 @@ components:
397396
role:
398397
$ref: '#/components/schemas/Role'
399398
required:
400-
- id
401399
- email
402400
UserUpdateManyMutationInput:
403401
type: object
@@ -1709,7 +1707,6 @@ components:
17091707
role:
17101708
$ref: '#/components/schemas/Role'
17111709
required:
1712-
- id
17131710
- email
17141711
UserUncheckedCreateWithoutPostsInput:
17151712
type: object
@@ -1727,7 +1724,6 @@ components:
17271724
role:
17281725
$ref: '#/components/schemas/Role'
17291726
required:
1730-
- id
17311727
- email
17321728
UserCreateOrConnectWithoutPostsInput:
17331729
type: object

packages/plugins/openapi/tests/openapi-restful.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ enum role {
2323
}
2424
2525
model User {
26-
id String @id
26+
id String @id @default(cuid())
2727
createdAt DateTime @default(now())
2828
updatedAt DateTime @updatedAt
2929
email String @unique

packages/plugins/openapi/tests/openapi-rpc.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ enum role {
2323
}
2424
2525
model User {
26-
id String @id
26+
id String @id @default(cuid())
2727
createdAt DateTime @default(now())
2828
updatedAt DateTime @updatedAt
2929
email String @unique

0 commit comments

Comments
 (0)