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

Handle multipart/form-data endpoints with payload validation #152

Closed
jondlm opened this issue Sep 11, 2015 · 3 comments
Closed

Handle multipart/form-data endpoints with payload validation #152

jondlm opened this issue Sep 11, 2015 · 3 comments

Comments

@jondlm
Copy link

jondlm commented Sep 11, 2015

I'm having a bit of trouble getting my multipart/form-data endpoint to show up correctly in swagger. Here is my hapi route:

{
  path: '/api/projects',
  method: 'POST',
  handler: projectsController.create,
  config: {
    tags: ['api'],
    notes: ['Create a new build given a .tar.gz file, a project ID, and build ID. If the project does not exist, it will be created.'],
    payload: {
      output: 'stream',
      parse: true,
      allow: 'multipart/form-data'
    },
    validate: {
      payload: {
        file: Joi.any().required(),
        projectId: safeStringSchema.required(),
        buildId: safeStringSchema.required()
      }
    }
  }
}

What I'm seeing in the swagger UI:

screen shot 2015-09-11 at 10 36 51

Is this something that should be supported and fixed? I might have some time to do a PR in the next few weeks.

@glennjones
Copy link
Collaborator

Sorry its taken me a while to answer this. I am unclear to what you where trying to achieve, but I am guessing you where looking for form elements in the UI rather than JSON input. If that is the case add the plugins config section as in the example below:

{
    path: '/api/projects',
    method: 'POST',
    handler: projectsController.create,
    config: {
        tags: [
            'api'
        ],
        notes: [
            'Createanewbuildgivena.tar.gzfile,
            aprojectID,
            andbuildID.Iftheprojectdoesnotexist,
            itwillbecreated.'
        ],
        payload: {
            output: 'stream',
            parse: true,
            allow: 'multipart/form-data'
        },
        validate: {
            payload: {
                file: Joi.any().required(),
                projectId: safeStringSchema.required(),
                buildId: safeStringSchema.required()
            }
        },
        plugins: {
            'hapi-swagger': {
                payloadType: 'form'
            },
        },
    }
}

@jondlm
Copy link
Author

jondlm commented Oct 20, 2015

Sorry I wasn't very clear. Reading back over my question I understand the confusion. Regardless, you totally answered it. That's exactly what I was looking for. Thank you for your help, and a wonderful library!

@jondlm jondlm closed this as completed Oct 20, 2015
@rajeshwarpatlolla
Copy link

rajeshwarpatlolla commented Aug 14, 2020

Thanks for the answer @glennjones , but with file: Joi.any().required() it is showing as a simple text input in swagger. Along with your answer i have added file: Joi.any().meta({ swaggerType: 'file' }).required(), then is is showing as file type as in the screenshot attached.

I have the below versions

"hapi-swagger": "^13.0.2",
"@hapi/joi": "^17.1.1",

This is working for me.

config: {
  description: 'Upload a file',
  tags: ['api', 'files'],
  auth: false,
  payload: {
    multipart: true,
    // output: 'stream',
    output: 'file',
    allow: 'multipart/form-data',
    parse: true,
    maxBytes: 10 * 1024 * 1024, //  maxBytes: 10MB
  },
  validate: {
    payload: Joi.object({
      type: Joi.string().required(),
      file: Joi.any().meta({ swaggerType: 'file' }).required(),
    }),
  },
  plugins: {
    'hapi-swagger': {
      payloadType: 'form',
    },
  },
},

Notice

file: Joi.any().meta({ swaggerType: 'file' }).required(),

along with

plugins: {
  'hapi-swagger': {
    payloadType: 'form',
   },
},

Screenshot 2020-08-14 at 6 45 42 PM

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

No branches or pull requests

3 participants