Skip to content

Commit

Permalink
Merge branch 'release-4.4.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Nov 29, 2018
2 parents 04760d1 + 937070b commit 483d6d5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.2] (2018-11-29)

### Fixed

- [#518](https://github.com/dadi/api/pull/518): make media field handle null values

## [4.4.1] (2018-11-28)

### Fixed
Expand Down
12 changes: 10 additions & 2 deletions dadi/lib/fields/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports.beforeOutput = function ({
let isArraySyntax = Array.isArray(input[field])
let normalisedValue = isArraySyntax ? input[field] : [input[field]]
let mediaObjectIDs = normalisedValue.map(value => {
if (typeof value !== 'string') {
if (value && typeof value !== 'string') {
return value._id
}

Expand Down Expand Up @@ -93,7 +93,15 @@ module.exports.beforeSave = function ({
}

return value
})
}).filter(Boolean)

// Are we just setting the field to null?
if (normalisedValue.length === 0) {
return Promise.resolve({
[field]: null
})
}

let queue = Promise.resolve()

// If there is a validation block with a `mimeTypes` property, it means we
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dadi/api",
"version": "4.4.1",
"version": "4.4.2",
"main": "main.js",
"scripts": {
"create-client": "cd ../../.. && node ./node_modules/@dadi/api/utils/create-client.js",
Expand All @@ -21,7 +21,7 @@
]
},
"dependencies": {
"@dadi/api-validator": "^1.1.0",
"@dadi/api-validator": "^1.1.1",
"@dadi/boot": "^1.1.3",
"@dadi/cache": "^3.0.0",
"@dadi/et": "^2.0.0",
Expand Down
22 changes: 22 additions & 0 deletions test/acceptance/fields/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,28 @@ describe('Media field', () => {
})
})

it('should accept `null` as a value', done => {
let payload = {
title: 'Media support in DADI API',
leadImage: null
}

client
.post('/vtest/testdb/test-schema')
.set('content-type', 'application/json')
.set('Authorization', `Bearer ${bearerToken}`)
.expect(200)
.send(payload)
.end((err, res) => {
res.body.results.should.be.instanceOf(Array)
res.body.results.length.should.eql(1)
res.body.results[0].title.should.eql(payload.title)
should.not.exist(res.body.results[0].leadImage)

done(err)
})
})

it('should accept an ID that does not correspond to a valid media object', done => {
let payload = {
title: 'Media support in DADI API',
Expand Down

0 comments on commit 483d6d5

Please sign in to comment.