Skip to content

Commit f21d76e

Browse files
committed
fix: jsonObj cannot have null value
Swift and ObjC cannot handle JSON Obj with null value and would error out with "Uncaught TypeError: Cannot read property 'toString' of null". Added a fix for both, tests, and output tests for each code language.
1 parent c8b1da4 commit f21d76e

File tree

27 files changed

+376
-2
lines changed

27 files changed

+376
-2
lines changed

src/targets/objc/helpers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ module.exports = {
6464
case '[object Boolean]':
6565
return value ? '@YES' : '@NO'
6666
default:
67-
return '@"' + value.toString().replace(/"/g, '\\"') + '"'
67+
try {
68+
return '@"' + value.toString().replace(/"/g, '\\"') + '"'
69+
} catch (e) {
70+
return ''
71+
}
6872
}
6973
}
7074
}

src/targets/swift/helpers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ module.exports = {
7474
case '[object Boolean]':
7575
return value.toString()
7676
default:
77-
return '"' + value.toString().replace(/"/g, '\\"') + '"'
77+
try {
78+
return '"' + value.toString().replace(/"/g, '\\"') + '"'
79+
} catch (e) {
80+
return ''
81+
}
7882
}
7983
}
8084
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CURL *hnd = curl_easy_init();
2+
3+
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
4+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har");
5+
6+
struct curl_slist *headers = NULL;
7+
headers = curl_slist_append(headers, "content-type: application/json");
8+
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
9+
10+
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"foo\":null}");
11+
12+
CURLcode ret = curl_easy_perform(hnd);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var client = new RestClient("http://mockbin.com/har");
2+
var request = new RestRequest(Method.POST);
3+
request.AddHeader("content-type", "application/json");
4+
request.AddParameter("application/json", "{\"foo\":null}", ParameterType.RequestBody);
5+
IRestResponse response = client.Execute(request);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
12+
url := "http://mockbin.com/har"
13+
14+
payload := strings.NewReader("{\"foo\":null}")
15+
16+
req, _ := http.NewRequest("POST", url, payload)
17+
18+
req.Header.Add("content-type", "application/json")
19+
20+
res, _ := http.DefaultClient.Do(req)
21+
22+
defer res.Body.Close()
23+
body, _ := ioutil.ReadAll(res.Body)
24+
25+
fmt.Println(res)
26+
fmt.Println(string(body))
27+
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
MediaType mediaType = MediaType.parse("application/json");
4+
RequestBody body = RequestBody.create(mediaType, "{\"foo\":null}");
5+
Request request = new Request.Builder()
6+
.url("http://mockbin.com/har")
7+
.post(body)
8+
.addHeader("content-type", "application/json")
9+
.build();
10+
11+
Response response = client.newCall(request).execute();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("content-type", "application/json")
3+
.body("{\"foo\":null}")
4+
.asString();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var settings = {
2+
"async": true,
3+
"crossDomain": true,
4+
"url": "http://mockbin.com/har",
5+
"method": "POST",
6+
"headers": {
7+
"content-type": "application/json"
8+
},
9+
"processData": false,
10+
"data": "{\"foo\":null}"
11+
}
12+
13+
$.ajax(settings).done(function (response) {
14+
console.log(response);
15+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var data = JSON.stringify({
2+
"foo": null
3+
});
4+
5+
var xhr = new XMLHttpRequest();
6+
xhr.withCredentials = true;
7+
8+
xhr.addEventListener("readystatechange", function () {
9+
if (this.readyState === this.DONE) {
10+
console.log(this.responseText);
11+
}
12+
});
13+
14+
xhr.open("POST", "http://mockbin.com/har");
15+
xhr.setRequestHeader("content-type", "application/json");
16+
17+
xhr.send(data);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var http = require("http");
2+
3+
var options = {
4+
"method": "POST",
5+
"hostname": "mockbin.com",
6+
"port": null,
7+
"path": "/har",
8+
"headers": {
9+
"content-type": "application/json"
10+
}
11+
};
12+
13+
var req = http.request(options, function (res) {
14+
var chunks = [];
15+
16+
res.on("data", function (chunk) {
17+
chunks.push(chunk);
18+
});
19+
20+
res.on("end", function () {
21+
var body = Buffer.concat(chunks);
22+
console.log(body.toString());
23+
});
24+
});
25+
26+
req.write(JSON.stringify({ foo: null }));
27+
req.end();

0 commit comments

Comments
 (0)