diff --git a/.changes/next-release/bugfix-JSON-00a26a06.json b/.changes/next-release/bugfix-JSON-00a26a06.json new file mode 100644 index 0000000000..25ba1ecf8d --- /dev/null +++ b/.changes/next-release/bugfix-JSON-00a26a06.json @@ -0,0 +1,5 @@ +{ + "type": "bugfix", + "category": "JSON", + "description": "Fixes issue caused when trying to unmarshall null binary shapes." +} \ No newline at end of file diff --git a/lib/util.js b/lib/util.js index 83a4aa0492..8b5c63ccad 100644 --- a/lib/util.js +++ b/lib/util.js @@ -101,6 +101,9 @@ var util = { if (typeof string === 'number') { throw util.error(new Error('Cannot base64 encode number ' + string)); } + if (string === null || typeof string === 'undefined') { + return string; + } var buf = (typeof util.Buffer.from === 'function' && util.Buffer.from !== Uint8Array.from) ? util.Buffer.from(string) : new util.Buffer(string); return buf.toString('base64'); }, @@ -109,6 +112,9 @@ var util = { if (typeof string === 'number') { throw util.error(new Error('Cannot base64 decode number ' + string)); } + if (string === null || typeof string === 'undefined') { + return string; + } return (typeof util.Buffer.from === 'function' && util.Buffer.from !== Uint8Array.from) ? util.Buffer.from(string, 'base64') : new util.Buffer(string, 'base64'); } diff --git a/test/protocol/json.spec.coffee b/test/protocol/json.spec.coffee index 6da0138bed..91f5b931d0 100644 --- a/test/protocol/json.spec.coffee +++ b/test/protocol/json.spec.coffee @@ -166,3 +166,9 @@ describe 'AWS.Protocol.Json', -> extractData '' expect(response.error).to.equal(null) expect(response.data).to.eql({}) + + it 'can handle null binary values', -> + extractData '{"i":1, "b": null}' + expect(response.error).to.equal(null) + expect(response.data.i).to.equal(1) + expect(response.data.b).to.equal(null) \ No newline at end of file diff --git a/test/protocol/rest_json.spec.coffee b/test/protocol/rest_json.spec.coffee index 577dd630eb..78e513418f 100644 --- a/test/protocol/rest_json.spec.coffee +++ b/test/protocol/rest_json.spec.coffee @@ -284,3 +284,14 @@ describe 'AWS.Protocol.RestJson', -> extractData '' expect(response.error).to.equal(null) expect(response.data).to.eql({}) + + it 'can handle null binary values', -> + defop output: + type: 'structure' + members: + bin: type: 'binary' + i: type: 'integer' + extractData '{"i": 1, "bin": null}' + expect(response.error).to.equal(null) + expect(response.data.i).to.equal(1) + expect(response.data.bin).to.equal(null) \ No newline at end of file diff --git a/test/util.spec.coffee b/test/util.spec.coffee index 755b735a6e..011499dbb6 100644 --- a/test/util.spec.coffee +++ b/test/util.spec.coffee @@ -566,6 +566,12 @@ describe 'AWS.util.base64', -> catch e err = e expect(err.message).to.equal('Cannot base64 encode number 3.14') + + it 'does not encode null', -> + expect(base64.encode(null)).to.eql(null) + + it 'does not encode undefined', -> + expect(base64.encode(undefined)).to.eql(undefined) describe 'decode', -> it 'decodes the given string', -> @@ -584,6 +590,12 @@ describe 'AWS.util.base64', -> err = e expect(err.message).to.equal('Cannot base64 decode number 3.14') + it 'does not decode null', -> + expect(base64.decode(null)).to.eql(null) + + it 'does not decode undefined', -> + expect(base64.decode(undefined)).to.eql(undefined) + describe 'AWS.util.hoistPayloadMember', -> hoist = AWS.util.hoistPayloadMember