-
Notifications
You must be signed in to change notification settings - Fork 41
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
With multipart/form-data and swagger-ui unable to uplaod a file from Swagger UI #151
Comments
same here with 3.0, in order for swagger ui to display the "choose file" button, some changes in schema need to be done and errors start to show. (I'm not used to post here in github so sorry for format mistakes). If
error:
If
visual studio code thunder client extension uploads image.
And so many other tries left, following doc, overstack solutions... none of them seem to work with swagger ui upload. Some changes remove the upload file button, some other make it appear but return type error ("must be object", must be string") etc. |
UPD: There is probably a better solution. Old answer: fastify.post(
'/the/url',
{
schema: {
body: {
type: 'object',
properties: {
files: {
type: 'array',
items: { type: 'string', format: 'binary' },
},
filesPaths: {
type: 'array',
items: {
properties: {
fileName: { type: 'string' },
filePath: { type: 'string' },
},
},
},
},
},
validatorCompiler: ({ schema, method, url, httpPart }) => {
return () => true;
},
},
},
handler,
); The problem here is that this will also disable validation of validatorCompiler: ({ schema, method, url, httpPart }) => {
const {body:_body} = schema;
const body = {..._body, properties:{..._body.properties,files: undefined}}; //omitting files property from validation
return ajv.compile({...schema,body});
}, |
Prerequisites
Fastify version
4.26.1
Plugin version
3.0.0
Node.js version
21.7.3
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
11
Description
I'm trying to upload multiple files in a post API call with multipart/form-data
I'm not able to do it neither in the swagger-ui or remotely (also with postman)
I'm getting the error:
Error: body/files must be string\n
If I change my route schema definition from
files: { type: 'array', items: { type: 'string', format: 'binary' }, },
to
files: { type: 'array', items: { format: 'binary' }, },
it works fine with postman but i'm not able to upload the file directly from the swagger UI
This is my app.js file:
This is my route to upload files:
Link to code that reproduces the bug
No response
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: