File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed
packages/openapi-ts/src/plugins/@hey-api/sdk/shared Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 1+ import { describe , expect , it } from 'vitest' ;
2+
3+ import { getSignatureParameters } from '../signature' ;
4+
5+ const createOperation = ( requestBodyRequired : boolean ) : any => ( {
6+ body : {
7+ required : requestBodyRequired ,
8+ schema : {
9+ properties : {
10+ bar : { type : 'string' } ,
11+ foo : { type : 'string' } ,
12+ } ,
13+ required : [ 'foo' ] ,
14+ type : 'object' ,
15+ } ,
16+ } ,
17+ } ) ;
18+
19+ describe ( 'getSignatureParameters' , ( ) => {
20+ it ( 'marks required body properties as required when request body is required' , ( ) => {
21+ const signature = getSignatureParameters ( {
22+ operation : createOperation ( true ) ,
23+ plugin : { } as any ,
24+ } ) ;
25+
26+ expect ( signature ?. parameters . foo ?. isRequired ) . toBe ( true ) ;
27+ expect ( signature ?. parameters . bar ?. isRequired ) . toBe ( false ) ;
28+ } ) ;
29+
30+ it ( 'keeps body properties optional when request body is optional' , ( ) => {
31+ const signature = getSignatureParameters ( {
32+ operation : createOperation ( false ) ,
33+ plugin : { } as any ,
34+ } ) ;
35+
36+ expect ( signature ?. parameters . foo ?. isRequired ) . toBe ( false ) ;
37+ expect ( signature ?. parameters . bar ?. isRequired ) . toBe ( false ) ;
38+ } ) ;
39+ } ) ;
Original file line number Diff line number Diff line change @@ -126,12 +126,14 @@ export function getSignatureParameters({
126126 operation . body . schema . type === 'object' &&
127127 operation . body . schema . properties
128128 ) {
129+ const requiredBodyProperties = new Set ( operation . body . schema . required ?? [ ] ) ;
129130 const properties = operation . body . schema . properties ;
130131 for ( const originalName in properties ) {
131132 const property = properties [ originalName ] ! ;
132133 const name = conflicts . has ( originalName ) ? `${ location } _${ originalName } ` : originalName ;
133134 const signatureParameter : SignatureParameter = {
134- isRequired : property . required ?. includes ( originalName ) ?? false ,
135+ isRequired :
136+ ( operation . body . required ?? false ) && requiredBodyProperties . has ( originalName ) ,
135137 name,
136138 schema : property ,
137139 } ;
You can’t perform that action at this time.
0 commit comments