@@ -276,3 +276,66 @@ test('should return a 400 when request fails to decode', async () => {
276276
277277 assert ( response . body . error . startsWith ( 'Invalid value undefined supplied to' ) ) ;
278278} ) ;
279+
280+ test ( 'middleware that modifies req.body should reach handler even without routeHandler()' , async ( ) => {
281+ const PostWithData = httpRoute ( {
282+ path : '/data' ,
283+ method : 'POST' ,
284+ request : httpRequest ( {
285+ body : {
286+ originalField : t . string ,
287+ addedByMiddleware : optional ( t . string ) ,
288+ } ,
289+ } ) ,
290+ response : {
291+ 200 : t . type ( {
292+ originalField : t . string ,
293+ addedByMiddleware : optional ( t . string ) ,
294+ } ) ,
295+ } ,
296+ } ) ;
297+
298+ const testApiSpec = apiSpec ( {
299+ 'test.route' : {
300+ post : PostWithData ,
301+ } ,
302+ } ) ;
303+
304+ const modifyBodyMiddleware : express . RequestHandler = ( req , _res , next ) => {
305+ req . body . addedByMiddleware = 'ADDED' ;
306+ next ( ) ;
307+ } ;
308+
309+ const handler = async ( params : {
310+ originalField : string ;
311+ addedByMiddleware ?: string ;
312+ } ) => {
313+ return {
314+ type : 200 ,
315+ payload : {
316+ originalField : params . originalField ,
317+ addedByMiddleware : params . addedByMiddleware ,
318+ } ,
319+ } as const ;
320+ } ;
321+
322+ const app = createServer ( testApiSpec , ( app : express . Application ) => {
323+ app . use ( express . json ( ) ) ;
324+ return {
325+ 'test.route' : {
326+ post : { middleware : [ modifyBodyMiddleware ] , handler } ,
327+ } ,
328+ } ;
329+ } ) ;
330+
331+ const response = await supertest ( app )
332+ . post ( '/data' )
333+ . send ( { originalField : 'test' } )
334+ . expect ( 200 ) ;
335+
336+ assert . equal (
337+ response . body . addedByMiddleware ,
338+ 'ADDED' ,
339+ 'addedByMiddleware should be present because req.body is part of req.decoded' ,
340+ ) ;
341+ } ) ;
0 commit comments