Coder Social home page Coder Social logo

Comments (16)

alrik avatar alrik commented on July 17, 2024 2

I got it to work with multer and a little tweak. Hopefully the following examples will be helpful for some people.

Multer is a multipart form-data handling middleware. by default it stores the upload to req.file or req.files. The express-openapi-validation logic is looking for defined files in req.body. So we need to copy the file upload to the expected position.

expressOpenapi.initialize({
  consumesMiddleware: {
    // example 1: Allow for single file upload (filename: 'file')
    'multipart/form-data'(req, res, next) {
      multer().single('file')(req, res, (err) => {
        if (err) return next(err);
        req.body.file = req.file;
        next();
      });
    }

    // example 2: Allow for any file upload
    'multipart/form-data'(req, res, next) {
      multer().any()(req, res, (err) => {
        if (err) return next(err);
        req.files.forEach(f => req.body[f.fieldname] = f);
        next();
      });
    }
  },
});

from open-api.

jsdevel avatar jsdevel commented on July 17, 2024 1

@hkors there's currently no support for file uploads. It would be great if you could propose a solution. Most swagger frameworks that I've seen don't handle this very well. I'd love express-openapi to be one of the first!

from open-api.

hkors avatar hkors commented on July 17, 2024

I can now receive the file by adding 'x-express-openapi-disable-validation-middleware': true to this documentation, but I suppose this shouldn't be the solution...

from open-api.

mghignet avatar mghignet commented on July 17, 2024

Hi!

The issue should be fixed now, I had the same problem.
See #55

from open-api.

jsdevel avatar jsdevel commented on July 17, 2024

Thanks @mghignet ! I've published #55 as v0.30.0. If you have the time, could you add a sample project to the test suite showing a file upload example?

from open-api.

jsdevel avatar jsdevel commented on July 17, 2024

@hkors if you're able to try again with the latest version it would be appreciated. I'll leave this issue open until we have confirmation that file uploads work.

from open-api.

mghignet avatar mghignet commented on July 17, 2024

Actually the bug is still present.
The issue is that we need to validate the presence of some fields (typically, a file) in formData, but it's not as easy as for a body or a query string.
The only workaround I've found is to remove the "required" property from formData parameters.

from open-api.

mghignet avatar mghignet commented on July 17, 2024

kogosoftwarellc/express-openapi-validation@0096f84
I've commented on this.
The "quick and dirty" solution I see would be to remove the piece of code I commented to disable errors for formData fields, since we're not able to validate them correctly.
It means "required" property (and maybe some other ones) would be ineffective.
What do you think?

from open-api.

jsdevel avatar jsdevel commented on July 17, 2024

@mghignet sorry for the late reply. Holidays and election happened here in the states. Barely getting back in the swing of things.

Whatever we need to do to get file uploads working, as long as the tests pass, I say we do it! Can you submit a PR?

from open-api.

jsdevel avatar jsdevel commented on July 17, 2024

Nice @alrik ! Can you by chance update the README with this example, and possibly create a test (i.e. can you add multer as a devDependency to show that it works)?

from open-api.

alrik avatar alrik commented on July 17, 2024

Hey @jsdevel sure. I'll add an example to the readme and add some tests when i got some spare time. Could you assign me the issue, so that i don't forget about it?

from open-api.

jsdevel avatar jsdevel commented on July 17, 2024

@alrik looks like I'm unable to assign to non-project collaborators. If you own an issue, I can assign it to you. Want to open a new issue here about a missing example in the README for file uploads? I should be able to assign that to you.

from open-api.

nigelkirby avatar nigelkirby commented on July 17, 2024

I submit that this issue is addressed satisfactorily by #105

from open-api.

jsdevel avatar jsdevel commented on July 17, 2024

Agreed. Thanks @simpgeek! @hkors let us know if you disagree.

from open-api.

mbsimonovic avatar mbsimonovic commented on July 17, 2024

i'm getting an error trying to test this with curl. The spec is:

    /upload/:
        post:
            summary: Create NEW files
            operationId: upload
            requestBody:
                required: true
                content:
                    multipart/form-data:
                        schema:
                            type: object
                            properties:
                                docs:
                                    type: array
                                    items:
                                        type: string
                                        format: binary

and express-openapi is initialised as suggested:

 'multipart/form-data'(req, res, next) {
            multer().any()(req, res, (err) => {
                if (err) return next(err);
                req.files.forEach(f => req.body[f.fieldname] = f);
                return next(); // this executes correctly
            });
        }

Hitting the app with curl:
curl "http://localhost:3000/upload/" -H "accept: */*" -H "Content-Type: multipart/form-data" -F [email protected] , fails with the following error (the endpoint never gets called):

Unsupported Content-Type multipart/form-data; boundary=------------------------2a07fe1e704ec1f5

The same happens with superagent/supertest and postman. Any hints?

from open-api.

jsdevel avatar jsdevel commented on July 17, 2024

@mbsimonovic please file a new issue. that appears to be related to V3 which wasn't published at the time this issue was created.

from open-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.