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

Fix/mixed form-data w/ file cause 'length' problem #1583

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ export class ExpressTemplateService extends TemplateService<ExpressApiHandlerPar
return this.validationService.ValidateParam(param, request.body[name], name, fieldErrors, 'body.', this.minimalSwaggerConfig);
case 'formData': {
const files = Object.values(args).filter(param => param.dataType === 'file');
if (files.length > 0) {
if (param.dataType === 'file' && files.length > 0) {
const requestFiles = request.files as { [fileName: string]: Express.Multer.File[] };
const fileArgs = this.validationService.ValidateParam(param, requestFiles[name], name, fieldErrors, undefined, this.minimalSwaggerConfig);
return fileArgs.length === 1 ? fileArgs[0] : fileArgs;
} else if (param.dataType === 'array' && param.array && param.array.dataType === 'file') {
return this.validationService.ValidateParam(param, request.files, name, fieldErrors, undefined, this.minimalSwaggerConfig);
} else {
return this.validationService.ValidateParam(param, request.body[name], name, fieldErrors, undefined, this.minimalSwaggerConfig);
}
return this.validationService.ValidateParam(param, request.body[name], name, fieldErrors, undefined, this.minimalSwaggerConfig);
}
case 'res':
return (status: number | undefined, data: any, headers: any) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,13 @@ export class KoaTemplateService extends TemplateService<KoaApiHandlerParameters,
case 'formData': {
const files = Object.values(args).filter(param => param.dataType === 'file');
const contextRequest = context.request as any;
if (files.length > 0) {
if (param.dataType === 'file' && files.length > 0) {
const fileArgs = this.validationService.ValidateParam(param, contextRequest.files[name], name, errorFields, undefined, this.minimalSwaggerConfig);
return fileArgs.length === 1 ? fileArgs[0] : fileArgs;
} else if (param.dataType === 'array' && param.array && param.array.dataType === 'file') {
return this.validationService.ValidateParam(param, contextRequest.files, name, errorFields, undefined, this.minimalSwaggerConfig);
} else {
return this.validationService.ValidateParam(param, contextRequest.body[name], name, errorFields, undefined, this.minimalSwaggerConfig);
}
return this.validationService.ValidateParam(param, contextRequest.body[name], name, errorFields, undefined, this.minimalSwaggerConfig);
}
case 'res':
return async (status: number | undefined, data: any, headers: any): Promise<void> => {
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/controllers/postController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ export class PostTestController {
return [fileA, fileB];
}

@Post('MixedFormDataWithFile')
public async mixedFormDataWithFile(
@FormField('username') username: string,
@UploadedFile('avatar') avatar: File,
): Promise<{ username: string; avatar: File; }> {
return { username, avatar };
}

/**
*
* @param aFile File description of multipart
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/express-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,25 @@ describe('Express Server', () => {
});
});

it('can post mixed form data content with file', () => {
const formData = {
username: 'test',
avatar: '@../tsconfig.json',
};
return verifyFileUploadRequest(`${basePath}/PostTest/MixedFormDataWithFile`, formData, (_err, res) => {
const file = res.body.avatar;
const packageJsonBuffer = readFileSync(resolve(__dirname, `../${file.originalname}`));
const returnedBuffer = Buffer.from(file.buffer);
expect(res.body.username).to.equal(formData.username);
expect(file).to.not.be.undefined;
expect(file.fieldname).to.be.not.undefined;
expect(file.originalname).to.be.not.undefined;
expect(file.encoding).to.be.not.undefined;
expect(file.mimetype).to.equal('application/json');
expect(Buffer.compare(returnedBuffer, packageJsonBuffer)).to.equal(0);
});
});

function verifyFileUploadRequest(
path: string,
formData: any,
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/hapi-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,25 @@ describe('Hapi Server', () => {
});
});

it('can post mixed form data content with file', () => {
const formData = {
username: 'test',
avatar: '@../tsconfig.json',
};
return verifyFileUploadRequest(`${basePath}/PostTest/MixedFormDataWithFile`, formData, (_err, res) => {
const file = res.body.avatar;
const packageJsonBuffer = readFileSync(resolve(__dirname, `../${file.originalname}`));
const returnedBuffer = Buffer.from(file.buffer);
expect(res.body.username).to.equal(formData.username);
expect(file).to.not.be.undefined;
expect(file.fieldname).to.be.not.undefined;
expect(file.originalname).to.be.not.undefined;
expect(file.encoding).to.be.not.undefined;
expect(file.mimetype).to.equal('application/json');
expect(Buffer.compare(returnedBuffer, packageJsonBuffer)).to.equal(0);
});
});

function verifyFileUploadRequest(
path: string,
formData: any,
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/koa-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,25 @@ describe('Koa Server', () => {
});
});

it('can post mixed form data content with file', () => {
const formData = {
username: 'test',
avatar: '@../tsconfig.json',
};
return verifyFileUploadRequest(`${basePath}/PostTest/MixedFormDataWithFile`, formData, (_err, res) => {
const file = res.body.avatar;
const packageJsonBuffer = readFileSync(resolve(__dirname, `../${file.originalname}`));
const returnedBuffer = Buffer.from(file.buffer);
expect(res.body.username).to.equal(formData.username);
expect(file).to.not.be.undefined;
expect(file.fieldname).to.be.not.undefined;
expect(file.originalname).to.be.not.undefined;
expect(file.encoding).to.be.not.undefined;
expect(file.mimetype).to.equal('application/json');
expect(Buffer.compare(returnedBuffer, packageJsonBuffer)).to.equal(0);
});
});

function verifyFileUploadRequest(
path: string,
formData: any,
Expand Down
Loading