Skip to content

Commit

Permalink
Merge pull request #1329 from postmanlabs/feature/file-toJSON
Browse files Browse the repository at this point in the history
Retain string file content while parsing formdata and file bodies
  • Loading branch information
codenirvana authored Aug 3, 2023
2 parents 93a3b19 + 6b7b3a2 commit a751823
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
unreleased:
new features:
- GH-1329 Added support for `fileName` property in formdata request body
- GH-1329 Retain string file content while parsing formdata and file bodies
chores:
- Updated dependencies

4.1.7:
date: 2023-01-24
fixed bugs:
Expand Down
5 changes: 3 additions & 2 deletions lib/collection/form-param.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ _.inherit((
this.type = options.type;
this.src = options.src;
this.contentType = options.contentType;
this.fileName = options.fileName;
}), Property);

_.assign(FormParam.prototype, /** @lends FormParam.prototype */ {
Expand Down Expand Up @@ -54,8 +55,8 @@ _.assign(FormParam.prototype, /** @lends FormParam.prototype */ {
toJSON () {
var obj = PropertyBase.toJSON(this);

// remove value from file param because it is non-serializable ReadStream
if (obj.type === 'file') {
// remove value from file param if it's empty or non-string (can be non-serializable ReadStream)
if (obj.type === 'file' && (typeof obj.value !== 'string' || !obj.value)) {
_.unset(obj, 'value');
}

Expand Down
6 changes: 4 additions & 2 deletions lib/collection/request-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ _.assign(RequestBody.prototype, /** @lends RequestBody.prototype */ {
toJSON () {
var obj = PropertyBase.toJSON(this);

// make sure that file content is removed because it is non-serializable ReadStream
_.unset(obj, 'file.content');
// remove value from file param if it's empty or non-string (can be non-serializable ReadStream)
if (obj.file && obj.file.content && typeof obj.file.content !== 'string') {
_.unset(obj, 'file.content');
}

return obj;
}
Expand Down
15 changes: 13 additions & 2 deletions test/unit/form-param.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,28 @@ describe('FormParam', function () {
key: 'foo',
src: 'fileSrc',
type: 'file',
fileName: 'file.txt',
contentType: 'application/json'
},
fp = new FormParam(definition);

expect(fp.toJSON()).to.eql(definition);
});

it('should not have value for param with type `file`', function () {
it('should not have non-string value for param with type `file`', function () {
var fp = new FormParam({
key: 'foo',
value: 'this will be removed for file param',
value: 'bar',
type: 'file',
contentType: 'application/json',
src: 'fileSrc'
});

expect(fp.toJSON()).to.have.property('value', 'bar');

fp = new FormParam({
key: 'foo',
value: Buffer.from('bar'),
type: 'file',
contentType: 'application/json',
src: 'fileSrc'
Expand Down
7 changes: 5 additions & 2 deletions test/unit/request-body.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,16 @@ describe('RequestBody', function () {
expect(rBody.toJSON()).to.eql(definition);
});

it('should not have content in file body', function () {
it('should not have non-string content in file body', function () {
var definition = {
mode: 'file',
file: { src: 'fileSrc', content: 'this should be removed' }
file: { src: 'fileSrc', content: 'foo' }
},
rBody = new RequestBody(definition);

expect(rBody.toJSON().file).to.have.property('content', 'foo');

rBody = new RequestBody({ mode: 'file', file: { content: Buffer.from('foo') } });
expect(rBody.toJSON().file).to.not.have.property('content');
});
});
Expand Down

0 comments on commit a751823

Please sign in to comment.