File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed
packages/rtk-query-codegen-openapi Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -324,7 +324,6 @@ export async function generateApi(
324
324
const queryArgValues = Object . values ( queryArg ) ;
325
325
326
326
const isFlatArg = flattenArg && queryArgValues . length === 1 ;
327
-
328
327
const QueryArg = factory . createTypeReferenceNode (
329
328
registerInterface (
330
329
factory . createTypeAliasDeclaration (
@@ -333,7 +332,16 @@ export async function generateApi(
333
332
undefined ,
334
333
queryArgValues . length > 0
335
334
? isFlatArg
336
- ? withQueryComment ( { ...queryArgValues [ 0 ] . type } , queryArgValues [ 0 ] , false )
335
+ ? withQueryComment (
336
+ factory . createUnionTypeNode ( [
337
+ queryArgValues [ 0 ] . type ,
338
+ ...( ! queryArgValues [ 0 ] . required
339
+ ? [ factory . createKeywordTypeNode ( ts . SyntaxKind . UndefinedKeyword ) ]
340
+ : [ ] ) ,
341
+ ] ) ,
342
+ queryArgValues [ 0 ] ,
343
+ false
344
+ )
337
345
: factory . createTypeLiteralNode (
338
346
queryArgValues . map ( ( def ) =>
339
347
withQueryComment (
Original file line number Diff line number Diff line change @@ -113,6 +113,22 @@ describe('option flattenArg', () => {
113
113
} ) ;
114
114
expect ( api ) . toContain ( 'queryArg.body' ) ;
115
115
} ) ;
116
+
117
+ it ( 'should flatten an optional arg as an optional type' , async ( ) => {
118
+ const api = await generateEndpoints ( {
119
+ ...config ,
120
+ filterEndpoints : 'findPetsByTags' ,
121
+ } ) ;
122
+ expect ( api ) . toMatch ( / \| u n d e f i n e d / ) ;
123
+ } ) ;
124
+
125
+ it ( 'should not flatten a non-optional arg with a superfluous union' , async ( ) => {
126
+ const api = await generateEndpoints ( {
127
+ ...config ,
128
+ filterEndpoints : 'getPetById' ,
129
+ } ) ;
130
+ expect ( api ) . not . toMatch ( / ^ \s * \| / ) ;
131
+ } ) ;
116
132
} ) ;
117
133
118
134
test ( 'hooks generation' , async ( ) => {
You can’t perform that action at this time.
0 commit comments