Skip to content

Commit ece8171

Browse files
authored
fix(File): add missing fields for #toJSON (#438)
1 parent b75bc42 commit ece8171

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

src/av.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,7 @@ AV._encode = function(value, seenObjects, disallowObjects) {
248248
if (!value.url() && !value.id) {
249249
throw new Error("Tried to save an object containing an unsaved file.");
250250
}
251-
return {
252-
__type: "File",
253-
id: value.id,
254-
name: value.name(),
255-
url: value.url()
256-
};
251+
return value._toFullJSON();
257252
}
258253
if (_.isObject(value)) {
259254
return _.mapObject(value, (v, k) => AV._encode(v, seenObjects, disallowObjects));

src/file.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,35 @@ module.exports = function(AV) {
199199
AV.File.prototype = {
200200
className: '_File',
201201

202-
toJSON: function() {
203-
return AV._encode(this);
202+
_toFullJSON(seenObjects) {
203+
var json = _.clone(this.attributes);
204+
AV._objectEach(json, function(val, key) {
205+
json[key] = AV._encode(val, seenObjects);
206+
});
207+
AV._objectEach(this._operations, function(val, key) {
208+
json[key] = val;
209+
});
210+
211+
if (_.has(this, "id")) {
212+
json.objectId = this.id;
213+
}
214+
_(['createdAt', 'updatedAt']).each((key) => {
215+
if (_.has(this, key)) {
216+
const val = this[key];
217+
json[key] = _.isDate(val) ? val.toJSON() : val;
218+
}
219+
});
220+
json.__type = "File";
221+
return json;
222+
},
223+
224+
toJSON() {
225+
const json = this._toFullJSON();
226+
// add id and keep __type for backward compatible
227+
if (_.has(this, 'id')) {
228+
json.id = this.id;
229+
}
230+
return json;
204231
},
205232

206233
/**
@@ -487,6 +514,7 @@ module.exports = function(AV) {
487514
name: value.name,
488515
url: value.url,
489516
mime_type: value.mime_type,
517+
bucket: value.bucket,
490518
};
491519
value.attributes.metaData = value.metaData || {};
492520
value.id = value.objectId;
@@ -496,6 +524,7 @@ module.exports = function(AV) {
496524
delete value.url;
497525
delete value.name;
498526
delete value.mime_type;
527+
delete value.bucket;
499528
_.extend(this, value);
500529
return this;
501530
}

src/object.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,12 @@ module.exports = function(AV) {
211211
if (_.has(this, "id")) {
212212
json.objectId = this.id;
213213
}
214-
if (_.has(this, "createdAt")) {
215-
if (_.isDate(this.createdAt)) {
216-
json.createdAt = this.createdAt.toJSON();
217-
} else {
218-
json.createdAt = this.createdAt;
214+
_(['createdAt', 'updatedAt']).each((key) => {
215+
if (_.has(this, key)) {
216+
const val = this[key];
217+
json[key] = _.isDate(val) ? val.toJSON() : val;
219218
}
220-
}
221-
222-
if (_.has(this, "updatedAt")) {
223-
if (_.isDate(this.updatedAt)) {
224-
json.updatedAt = this.updatedAt.toJSON();
225-
} else {
226-
json.updatedAt = this.updatedAt;
227-
}
228-
}
219+
});
229220
json.__type = "Object";
230221
json.className = this.className;
231222
return json;

test/file.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('File', function() {
123123
});
124124
});
125125

126-
describe('Fetch', function() {
126+
describe('#fetch', function() {
127127
var fileId = '52f9dd5ae4b019816c865985';
128128
it('createWithoutData() should return a File', function() {
129129
var file = AV.File.createWithoutData(fileId);
@@ -136,16 +136,32 @@ describe('File', function() {
136136
file.save();
137137
}).to.throwError(/File already saved\./);
138138
});
139-
it('fetch() should retrieve all data', function() {
140-
var file = AV.File.createWithoutData(fileId);
141-
return file.fetch().then(function(file) {
139+
describe('fetch', () => {
140+
before(function() {
141+
return AV.File.createWithoutData(fileId)
142+
.fetch()
143+
.then(file => this.file = file);
144+
})
145+
it('should retrieve all data', function() {
146+
var file = this.file;
142147
expect(file).to.be.a(AV.File);
143148
expect(file.id).to.be(fileId);
144149
expect(file.name()).to.be('myfile.txt');
145150
expect(file.get('mime_type')).to.be('text/plain');
146151
expect(typeof file.url()).to.be('string');
147152
});
148-
});
153+
it('decode and encode', function () {
154+
const json = this.file.toJSON();
155+
// backward compatible check
156+
json.should.have.properties(['__type', 'id', 'name', 'url']);
157+
const file = AV._decode(json);
158+
expect(file).to.be.a(AV.File);
159+
expect(file.id).to.be(fileId);
160+
expect(file.name()).to.be('myfile.txt');
161+
expect(file.get('mime_type')).to.be('text/plain');
162+
expect(typeof file.url()).to.be('string');
163+
});
164+
})
149165
});
150166

151167
describe('File get and set', function() {

0 commit comments

Comments
 (0)