Commit c279ba2 1 parent 10d65d1 commit c279ba2 Copy full SHA for c279ba2
File tree 3 files changed +54
-2
lines changed
3 files changed +54
-2
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @hono/zod-openapi ' : patch
3
+ ---
4
+
5
+ fix(zod-openapi): correctly handle path parameters in basePath
Original file line number Diff line number Diff line change @@ -638,7 +638,7 @@ export class OpenAPIHono<
638
638
path : mergePath (
639
639
pathForOpenAPI ,
640
640
// @ts -expect-error _basePath is private
641
- app . _basePath ,
641
+ app . _basePath . replaceAll ( / : ( [ ^ \/ ] + ) / g , '{$1}' ) ,
642
642
def . route . path
643
643
) ,
644
644
} )
@@ -649,7 +649,7 @@ export class OpenAPIHono<
649
649
path : mergePath (
650
650
pathForOpenAPI ,
651
651
// @ts -expect-error _basePath is private
652
- app . _basePath ,
652
+ app . _basePath . replaceAll ( / : ( [ ^ \/ ] + ) / g , '{$1}' ) ,
653
653
def . webhook . path
654
654
) ,
655
655
} )
Original file line number Diff line number Diff line change @@ -1274,6 +1274,53 @@ describe('basePath()', () => {
1274
1274
expect ( res . status ) . toBe ( 200 )
1275
1275
expect ( await res . json ( ) ) . toEqual ( { path : 'abc' } )
1276
1276
} )
1277
+
1278
+ it ( 'Should correctly handle path parameters in nested basePath' , async ( ) => {
1279
+ const app = new OpenAPIHono ( )
1280
+ const nested = new OpenAPIHono ( ) . basePath ( '/:param2' )
1281
+
1282
+ nested . openapi (
1283
+ createRoute ( {
1284
+ method : 'get' ,
1285
+ path : '/{param3}' ,
1286
+ responses : {
1287
+ 200 : {
1288
+ description : 'Get message' ,
1289
+ } ,
1290
+ } ,
1291
+ } ) ,
1292
+ ( c ) => {
1293
+ return c . json ( {
1294
+ param1 : c . req . param ( 'param1' ) ,
1295
+ param2 : c . req . param ( 'param2' ) ,
1296
+ param3 : c . req . param ( 'param3' )
1297
+ } )
1298
+ }
1299
+ )
1300
+
1301
+ app . route ( '/:param1' , nested )
1302
+
1303
+ const json = app . getOpenAPIDocument ( {
1304
+ openapi : '3.0.0' ,
1305
+ info : {
1306
+ version : '1.0.0' ,
1307
+ title : 'My API' ,
1308
+ } ,
1309
+ } )
1310
+
1311
+ const paths = Object . keys ( json . paths )
1312
+
1313
+ expect ( paths ) . toStrictEqual ( [ '/{param1}/{param2}/{param3}' ] )
1314
+ expect ( paths ) . not . toStrictEqual ( [ '/{param1}/:param2/{param3}' ] )
1315
+
1316
+ const res = await app . request ( '/foo/bar/baz' )
1317
+ expect ( res . status ) . toBe ( 200 )
1318
+ expect ( await res . json ( ) ) . toEqual ( {
1319
+ param1 : 'foo' ,
1320
+ param2 : 'bar' ,
1321
+ param3 : 'baz'
1322
+ } )
1323
+ } )
1277
1324
} )
1278
1325
1279
1326
describe ( 'With hc' , ( ) => {
You can’t perform that action at this time.
0 commit comments