Skip to content

Conversation

grigoran
Copy link

@grigoran grigoran commented May 13, 2025

There was a need to restrict the file size in each route when used alongside attachFieldsToBody. These changes will enable route-specific busboy configuration.
Example

fastify.post('/', {
    schema: {
        consumes: ['multipart/form-data'],
        body: Type.Object({
            // file: {isFile: true} as unknown as TSchema
            file: Type.Unsafe<{ toBuffer: () => Promise<Buffer> }>({ isFile: true })
        }),
        response: {
            200: Type.Object({
                ok: Type.Boolean()
            })
        }
    },
    config: {
        multipart_options: {
            limits: {
                fileSize: 1024*1024*2 // 2MB
            }
        }
    }
}, async (req, rep) => {
    const buf = req.body.file.toBuffer();
    return { ok: true }
})

Checklist

Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some unit test?

@grigoran
Copy link
Author

Thank you! Done

@grigoran grigoran requested a review from Eomm May 14, 2025 11:18
@Eomm Eomm requested a review from a team September 4, 2025 11:53
@grigoran grigoran requested a review from is2ei September 19, 2025 19:12
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs are missing


interface FastifyContextConfig {
multipartOptions?: Omit<BusboyConfig, 'headers'>
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for this type

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@grigoran grigoran requested a review from mcollina September 21, 2025 16:38
@grigoran grigoran requested a review from is2ei September 25, 2025 18:51
config: {
multipartOptions: {
limits: {
fileSize: 1_000_000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@grigoran

The file size limitation error isn’t caused by this configuration.
In this test, it should occur only when the request configuration is set, as described in the title.

Copy link
Author

@grigoran grigoran Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@is2ei
Hello! The error is caused by this exact configuration; I've verified it locally. This configuration is also specified in the title, except for the multipart_options(multipartOptions) field name. If I've misunderstood something, please correct me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@grigoran

Could you share how you verified it locally?
I commented out the config, but the test still passed.
It seems the error may not be caused by the config.

diff --git a/test/multipart-fileLimit.test.js b/test/multipart-fileLimit.test.js
index 6aed6a5..d6d4462 100644
--- a/test/multipart-fileLimit.test.js
+++ b/test/multipart-fileLimit.test.js
@@ -331,13 +331,13 @@ test('should throw fileSize limitation error when used alongside attachFieldsToB
   crypto.randomFillSync(randomFileBuffer)
 
   fastify.post('/', {
-    config: {
-      multipartOptions: {
-        limits: {
-          fileSize: 1_000_000
-        }
-      }
-    }
+    // config: {
+    //   multipartOptions: {
+    //     limits: {
+    //       fileSize: 1_000_000
+    //     }
+    //   }
+    // }
   }, async function (req, reply) {
     t.assert.fail('it should throw')

Copy link
Author

@grigoran grigoran Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@is2ei
Got it. I tested it by changing the fileSize value to 2_500_000. Commenting out the configuration results in an error due to the default fileSize value, which is fastify.initialConfig.bodyLimit = 1_000_000.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If necessary, I can change the values ​​to make it clearer what is causing the error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@grigoran

The test should be updated so that the error is caused by the config, as the message states:

should throw fileSize limitation error when used alongside attachFieldsToBody and set request config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants