diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f8f70b2..ebe2ecb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [4.4.1] (2018-11-28) + +### Fixed + +- [#516](https://github.com/dadi/api/issues/516): make media field handle legacy values + ## [4.4.0] (2018-11-22) ### Added diff --git a/dadi/lib/fields/media.js b/dadi/lib/fields/media.js index 81660b36..04a3cfca 100644 --- a/dadi/lib/fields/media.js +++ b/dadi/lib/fields/media.js @@ -43,21 +43,25 @@ module.exports.beforeOutput = function ({ return mediaObjects }, {}) }).then(mediaObjects => { - return normalisedValue.map(value => { - if (mediaObjects[value._id]) { - let mergedValue = Object.assign({}, mediaObjects[value._id], value) + return mediaObjectIDs.map((id, index) => { + let value = typeof normalisedValue[index] === 'object' + ? normalisedValue[index] + : {} + + if (mediaObjects[id]) { + let mergedValue = Object.assign({}, mediaObjects[id], value) let sortedValue = Object.keys(mergedValue).sort().reduce((sortedValue, field) => { sortedValue[field] = mergedValue[field] return sortedValue }, {}) - composedIDs.push(value._id) + composedIDs.push(id.toString()) return sortedValue } - return value._id + return id }) }).then(composedValue => { let output = Object.assign(input, { diff --git a/package.json b/package.json index 614f86c0..dc6288ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dadi/api", - "version": "4.4.0", + "version": "4.4.1", "main": "main.js", "scripts": { "create-client": "cd ../../.. && node ./node_modules/@dadi/api/utils/create-client.js", diff --git a/test/acceptance/fields/media.js b/test/acceptance/fields/media.js index 2be86678..10137d6b 100644 --- a/test/acceptance/fields/media.js +++ b/test/acceptance/fields/media.js @@ -659,6 +659,89 @@ describe('Media field', () => { }) }) }) + + it('should resolve a legacy value created by a Reference field', done => { + client + .post('/media/upload') + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerToken}`) + .attach('avatar', 'test/acceptance/temp-workspace/media/1f525.png') + .end((err, res) => { + let mediaObject = res.body.results[0] + let payload = { + title: 'Media support in DADI API', + legacyImage: mediaObject._id + } + + client + .post('/vtest/testdb/test-schema') + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerToken}`) + .send(payload) + .end((err, res) => { + let {results} = res.body + + results.should.be.instanceOf(Array) + results.length.should.eql(1) + results[0].title.should.eql(payload.title) + results[0].legacyImage._id.should.eql(mediaObject._id) + results[0].legacyImage.fileName.should.eql('1f525.png') + results[0]._composed.legacyImage.should.eql(mediaObject._id) + + client + .get(`/vtest/testdb/test-schema/${results[0]._id}?compose=true`) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerToken}`) + .end((err, res) => { + let {results} = res.body + + results.should.be.instanceOf(Array) + results.length.should.eql(1) + results[0].title.should.eql(payload.title) + results[0].legacyImage._id.should.eql(mediaObject._id) + results[0].legacyImage.fileName.should.eql('1f525.png') + results[0]._composed.legacyImage.should.eql(mediaObject._id) + + let collectionSchemaPath = path.join( + __dirname, + '/../temp-workspace/collections/vtest/testdb/collection.test-schema.json' + ) + let collectionSchema = require(collectionSchemaPath) + + // Convert the field to use the Media type. + collectionSchema.fields.legacyImage.type = 'Media' + delete collectionSchema.fields.legacyImage.settings + + help.writeTempFile( + collectionSchemaPath, + JSON.stringify(collectionSchema, null, 2), + restoreCollection => { + setTimeout(() => { + client + .get(`/vtest/testdb/test-schema/${results[0]._id}?cache=false`) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerToken}`) + .end((err, res) => { + let {results} = res.body + + results.should.be.instanceOf(Array) + results.length.should.eql(1) + results[0].title.should.eql(payload.title) + results[0].legacyImage._id.should.eql(mediaObject._id) + results[0].legacyImage.fileName.should.eql('1f525.png') + results[0]._composed.legacyImage.should.eql(mediaObject._id) + + restoreCollection() + + done(err) + }) + }, 1000) + } + ) + }) + }) + }) + }) }) describe('PUT', () => { diff --git a/test/acceptance/workspace/collections/vtest/testdb/collection.test-schema.json b/test/acceptance/workspace/collections/vtest/testdb/collection.test-schema.json index 01360028..28443821 100755 --- a/test/acceptance/workspace/collections/vtest/testdb/collection.test-schema.json +++ b/test/acceptance/workspace/collections/vtest/testdb/collection.test-schema.json @@ -25,6 +25,12 @@ "validation": { "mimeTypes": ["image/jpeg"] } + }, + "legacyImage": { + "type": "Reference", + "settings": { + "collection": "mediaStore" + } } }, "settings": {