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 @@ -331,7 +331,6 @@ export async function generateApi(
331
331
const queryArgValues = Object . values ( queryArg ) ;
332
332
333
333
const isFlatArg = flattenArg && queryArgValues . length === 1 ;
334
-
335
334
const QueryArg = factory . createTypeReferenceNode (
336
335
registerInterface (
337
336
factory . createTypeAliasDeclaration (
@@ -340,7 +339,16 @@ export async function generateApi(
340
339
undefined ,
341
340
queryArgValues . length > 0
342
341
? isFlatArg
343
- ? withQueryComment ( { ...queryArgValues [ 0 ] . type } , queryArgValues [ 0 ] , false )
342
+ ? withQueryComment (
343
+ factory . createUnionTypeNode ( [
344
+ queryArgValues [ 0 ] . type ,
345
+ ...( ! queryArgValues [ 0 ] . required
346
+ ? [ factory . createKeywordTypeNode ( ts . SyntaxKind . UndefinedKeyword ) ]
347
+ : [ ] ) ,
348
+ ] ) ,
349
+ queryArgValues [ 0 ] ,
350
+ false
351
+ )
344
352
: factory . createTypeLiteralNode (
345
353
queryArgValues . map ( ( def ) =>
346
354
withQueryComment (
Original file line number Diff line number Diff line change @@ -181,6 +181,22 @@ describe('option flattenArg', () => {
181
181
} ) ;
182
182
expect ( api ) . toContain ( 'queryArg.body' ) ;
183
183
} ) ;
184
+
185
+ it ( 'should flatten an optional arg as an optional type' , async ( ) => {
186
+ const api = await generateEndpoints ( {
187
+ ...config ,
188
+ filterEndpoints : 'findPetsByTags' ,
189
+ } ) ;
190
+ expect ( api ) . toMatch ( / \| u n d e f i n e d / ) ;
191
+ } ) ;
192
+
193
+ it ( 'should not flatten a non-optional arg with a superfluous union' , async ( ) => {
194
+ const api = await generateEndpoints ( {
195
+ ...config ,
196
+ filterEndpoints : 'getPetById' ,
197
+ } ) ;
198
+ expect ( api ) . not . toMatch ( / ^ \s * \| / ) ;
199
+ } ) ;
184
200
} ) ;
185
201
186
202
test ( 'hooks generation' , async ( ) => {
You can’t perform that action at this time.
0 commit comments