Skip to content

Commit 04760d1

Browse files
committed
Merge branch 'release-4.4.1'
2 parents 736e858 + c61ffbc commit 04760d1

5 files changed

Lines changed: 105 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [4.4.1] (2018-11-28)
8+
9+
### Fixed
10+
11+
- [#516](https://github.com/dadi/api/issues/516): make media field handle legacy values
12+
713
## [4.4.0] (2018-11-22)
814

915
### Added

dadi/lib/fields/media.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,25 @@ module.exports.beforeOutput = function ({
4343
return mediaObjects
4444
}, {})
4545
}).then(mediaObjects => {
46-
return normalisedValue.map(value => {
47-
if (mediaObjects[value._id]) {
48-
let mergedValue = Object.assign({}, mediaObjects[value._id], value)
46+
return mediaObjectIDs.map((id, index) => {
47+
let value = typeof normalisedValue[index] === 'object'
48+
? normalisedValue[index]
49+
: {}
50+
51+
if (mediaObjects[id]) {
52+
let mergedValue = Object.assign({}, mediaObjects[id], value)
4953
let sortedValue = Object.keys(mergedValue).sort().reduce((sortedValue, field) => {
5054
sortedValue[field] = mergedValue[field]
5155

5256
return sortedValue
5357
}, {})
5458

55-
composedIDs.push(value._id)
59+
composedIDs.push(id.toString())
5660

5761
return sortedValue
5862
}
5963

60-
return value._id
64+
return id
6165
})
6266
}).then(composedValue => {
6367
let output = Object.assign(input, {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dadi/api",
3-
"version": "4.4.0",
3+
"version": "4.4.1",
44
"main": "main.js",
55
"scripts": {
66
"create-client": "cd ../../.. && node ./node_modules/@dadi/api/utils/create-client.js",

test/acceptance/fields/media.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,89 @@ describe('Media field', () => {
659659
})
660660
})
661661
})
662+
663+
it('should resolve a legacy value created by a Reference field', done => {
664+
client
665+
.post('/media/upload')
666+
.set('content-type', 'application/json')
667+
.set('Authorization', `Bearer ${bearerToken}`)
668+
.attach('avatar', 'test/acceptance/temp-workspace/media/1f525.png')
669+
.end((err, res) => {
670+
let mediaObject = res.body.results[0]
671+
let payload = {
672+
title: 'Media support in DADI API',
673+
legacyImage: mediaObject._id
674+
}
675+
676+
client
677+
.post('/vtest/testdb/test-schema')
678+
.set('content-type', 'application/json')
679+
.set('Authorization', `Bearer ${bearerToken}`)
680+
.send(payload)
681+
.end((err, res) => {
682+
let {results} = res.body
683+
684+
results.should.be.instanceOf(Array)
685+
results.length.should.eql(1)
686+
results[0].title.should.eql(payload.title)
687+
results[0].legacyImage._id.should.eql(mediaObject._id)
688+
results[0].legacyImage.fileName.should.eql('1f525.png')
689+
results[0]._composed.legacyImage.should.eql(mediaObject._id)
690+
691+
client
692+
.get(`/vtest/testdb/test-schema/${results[0]._id}?compose=true`)
693+
.set('content-type', 'application/json')
694+
.set('Authorization', `Bearer ${bearerToken}`)
695+
.end((err, res) => {
696+
let {results} = res.body
697+
698+
results.should.be.instanceOf(Array)
699+
results.length.should.eql(1)
700+
results[0].title.should.eql(payload.title)
701+
results[0].legacyImage._id.should.eql(mediaObject._id)
702+
results[0].legacyImage.fileName.should.eql('1f525.png')
703+
results[0]._composed.legacyImage.should.eql(mediaObject._id)
704+
705+
let collectionSchemaPath = path.join(
706+
__dirname,
707+
'/../temp-workspace/collections/vtest/testdb/collection.test-schema.json'
708+
)
709+
let collectionSchema = require(collectionSchemaPath)
710+
711+
// Convert the field to use the Media type.
712+
collectionSchema.fields.legacyImage.type = 'Media'
713+
delete collectionSchema.fields.legacyImage.settings
714+
715+
help.writeTempFile(
716+
collectionSchemaPath,
717+
JSON.stringify(collectionSchema, null, 2),
718+
restoreCollection => {
719+
setTimeout(() => {
720+
client
721+
.get(`/vtest/testdb/test-schema/${results[0]._id}?cache=false`)
722+
.set('content-type', 'application/json')
723+
.set('Authorization', `Bearer ${bearerToken}`)
724+
.end((err, res) => {
725+
let {results} = res.body
726+
727+
results.should.be.instanceOf(Array)
728+
results.length.should.eql(1)
729+
results[0].title.should.eql(payload.title)
730+
results[0].legacyImage._id.should.eql(mediaObject._id)
731+
results[0].legacyImage.fileName.should.eql('1f525.png')
732+
results[0]._composed.legacyImage.should.eql(mediaObject._id)
733+
734+
restoreCollection()
735+
736+
done(err)
737+
})
738+
}, 1000)
739+
}
740+
)
741+
})
742+
})
743+
})
744+
})
662745
})
663746

664747
describe('PUT', () => {

test/acceptance/workspace/collections/vtest/testdb/collection.test-schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
"validation": {
2626
"mimeTypes": ["image/jpeg"]
2727
}
28+
},
29+
"legacyImage": {
30+
"type": "Reference",
31+
"settings": {
32+
"collection": "mediaStore"
33+
}
2834
}
2935
},
3036
"settings": {

0 commit comments

Comments
 (0)