Skip to content

fix(types): add optional value to MultipartFile for onFile - #624

Merged
mcollina merged 1 commit into
fastify:mainfrom
imharjot:fix/multipartfile-value-type
Oct 22, 2026
Merged

mcollina merged 1 commit into
fastify:mainfrom
imharjot:fix/multipartfile-value-type

Conversation

@imharjot

@imharjot imharjot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Add optional value?: unknown to MultipartFile so the documented onFile example for attachFieldsToBody: 'keyValues' typechecks. The type regression now uses that decoding example.

The existing custom-onFile runtime test verifies that the assigned file value reaches the request body. No runtime change is needed.

Validation: npm test passed (103 runtime tests, 100% coverage and 35 type assertions); npm run lint passed.

Fixes #574

@Tony133
Tony133 requested a review from a team July 16, 2026 10:39

@is2ei is2ei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the issue is a bit more complicated.

It looks like assigning the value field to the part object in the onFile function is a bit of a hack.
This works because the if statement uses field.value when it is not undefined.

https://github.com/fastify/fastify-multipart/blob/main/index.js#L100-L103

            if (field.value !== undefined) {
              body[key] = field.value
            } else if (field._buf) {
              body[key] = field._buf

However, the value property is intended for objects whose type is field.

https://github.com/fastify/fastify-multipart/blob/main/index.js#L338-L347

      const value = {
        type: 'field',
        fieldname: name,
        mimetype: contentType,
        encoding,
        value: fieldValue,
        fieldnameTruncated,
        valueTruncated,
        fields: body
      }

On the other hand, the _buf property is intended for objects whose type is file.

https://github.com/fastify/fastify-multipart/blob/main/index.js#L373-L382

      const value = {
        type: 'file',
        fieldname: name,
        filename,
        encoding,
        mimetype,
        file,
        fields: body,
        _buf: null,
        async toBuffer () {

@imharjot
imharjot force-pushed the fix/multipartfile-value-type branch from d8def27 to 513afe6 Compare October 4, 2026 13:50
@imharjot

imharjot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

@is2ei I checked the file-part behavior against the current README and the existing custom-onFile test in test/multipart-attach-body.test.js. With attachFieldsToBody: 'keyValues', assigning part.value in onFile is how the documented example supplies a decoded file value to the body. I added type coverage for that exact example and kept the property optional. All 103 runtime tests, the type suite and lint pass locally.

@mcollina
mcollina merged commit 8fec686 into fastify:main Oct 22, 2026
17 checks passed
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.

Type value does not exist on MultipartFile when using onFile

4 participants