Skip to content

Commit

Permalink
Fixes issue caused when trying to unmarshall null binary shapes (#1346)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisradek committed Feb 6, 2017
1 parent 0642da0 commit a888658
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .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."
}
6 changes: 6 additions & 0 deletions lib/util.js
Expand Up @@ -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');
},
Expand All @@ -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');
}

Expand Down
6 changes: 6 additions & 0 deletions test/protocol/json.spec.coffee
Expand Up @@ -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)
11 changes: 11 additions & 0 deletions test/protocol/rest_json.spec.coffee
Expand Up @@ -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)
12 changes: 12 additions & 0 deletions test/util.spec.coffee
Expand Up @@ -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', ->
Expand All @@ -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

Expand Down

0 comments on commit a888658

Please sign in to comment.