Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript: there is no way to make req.saveRequestFiles() works when we want to validate schema #313

Closed
2 tasks done
meotimdihia opened this issue Dec 21, 2021 · 12 comments
Labels
bug Confirmed bug

Comments

@meotimdihia
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

I have multi problems with the plugin:

  1. I am getting the error: typescript error: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator. for this
  2. await req.saveRequestFiles() doesn't work unless I remove all validations and configs: attachFieldsToBody, sharedSchemaId, onFile
// config
fastify.register(fastifyMultipart, {
	attachFieldsToBody: true,
	sharedSchemaId: "#mySharedSchema",
	onFile
})

//routes
  const body = {
    type: "object",
    properties: {
      images: {
            type: "array",
            items: fastify.getSchema("mySharedSchema")
      }
    },
    additionalProperties: true
  } as const

  fastify.post<{
    Params: FromSchema<typeof params>
    Body: FromSchema<typeof body>
  }>("/upload",
    {
      schema: {
        body: body,
        response: {
          200: {
            type: "object",
            additionalProperties: true
          }
        }
      }
    },
    async (req, reply) => {
     // typescript error: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
      for (const file of req.body.images) {

      }  
      
      // doesn't work
      const files = await req.saveRequestFiles()
      
      // doesn't work
      req.files
      
@Eomm
Copy link
Member

Eomm commented Jan 8, 2022

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

cc @fastify/typescript

@meotimdihia
Copy link
Author

@Eomm I am not really too good about Typescript.
I don't understand what is this error. 😀

@RafaelGSS
Copy link
Member

@meotimdihia Could you provide a minimal reproducible code? Further information, see: https://stackoverflow.com/help/minimal-reproducible-example

@stale
Copy link

stale bot commented Apr 17, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 17, 2022
@stale stale bot closed this as completed Apr 28, 2022
@meotimdihia
Copy link
Author

meotimdihia commented Sep 11, 2022

@RafaelGSS @Eomm Please re-open this issue.

This is the minimal reproducible code
https://github.com/meotimdihia/fastify-multipart-bug-report

STEP 1 FAILED

When you upload files you will get this response []

STEP 2 OK

If you remove

Untitled 19

@fmenis
Copy link

fmenis commented Dec 26, 2022

Guys, any update on this?
I confirm @meotimdihia issue with saveRequestFiles method: if the attachFieldsToBody option is enabled, this method doesn't work.
It would be nice to have both saveRequestFiles method and attachFieldsToBody options working properly, without doing by hand what saveRequestFiles does (save files in the temp dir and delete them when the request ends)

@mcollina
Copy link
Member

cc @fastify/typescript @simoneb could you take a look?

@Ceres6
Copy link
Contributor

Ceres6 commented Jan 5, 2023

I will be taking a look at it to see if I can figure that out

@Ceres6
Copy link
Contributor

Ceres6 commented Jan 9, 2023

  1. I am getting the error: typescript error: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator. for this

With the code provided I have not been able to reproduce. Maybe you could give further code for this.

  1. await req.saveRequestFiles() doesn't work unless I remove all validations and configs: attachFieldsToBody, sharedSchemaId, onFile

I will soon upload a PR to address this issue which will work with all of the above but the onFile which will depend on the behaviour of such function

async (req, reply) => {
 // typescript error: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
  for (const file of req.body.images) {

  }  

Here I was getting 'req.body.images' is possibly 'undefined'. which is solved by either adding required: [images] to schema or a non-null assertion

  // doesn't work
  const files = await req.saveRequestFiles()
  
  // doesn't work
  req.files

Addressing this in my PR

Let me know if I can help with something else

@mcollina
Copy link
Member

@Ceres6 is this fixed?

@Ceres6
Copy link
Contributor

Ceres6 commented Jan 13, 2023

@mcollina The problem with saveRequestFiles when attachFieldsToBody is. And I couldn't reproduce the Typescript problem so I guess it is. Anyway I'd be happy to help if there is code with the reproducible typing problem.

@simoneb
Copy link
Contributor

simoneb commented Jan 13, 2023

I'd say let's close this issue, the OP didn't follow up in months

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

No branches or pull requests

7 participants