Skip to content

Commit

Permalink
Fix support RFC 7807 format (T1156273) (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegKipchatov committed Apr 12, 2023
1 parent 1dcc20e commit 151544a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
13 changes: 10 additions & 3 deletions js-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
QUnit.test("other mime", testCase("unknown/unknown", "any", "Bad Request"));

QUnit.test("RFC 7807 - title", testCase(
"application/json",
"application/problem+json",
JSON.stringify({
type: "https://tools.ietf.org/html/rfc7231#section-6.6.1",
title: SAMPLE_MESSAGE,
Expand All @@ -186,13 +186,20 @@
));

QUnit.test("RFC 7807 - detail fallback", testCase(
"application/json",
"application/problem+json",
JSON.stringify({
detail: SAMPLE_MESSAGE
}),
SAMPLE_MESSAGE
))
));

QUnit.test("RFC 7807 - error format", testCase(
"application/problem+json",
JSON.stringify({
some_property: SAMPLE_MESSAGE
}),
'{\"some_property\":\"' + SAMPLE_MESSAGE + '\"}'
));
});

QUnit.test("Issue #146", function(assert) {
Expand Down
22 changes: 15 additions & 7 deletions js/dx.aspnet.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,23 @@
if(mime.indexOf("application/json") === 0) {
var jsonObj = safeParseJSON(responseText);

if(isNonEmptyString(jsonObj))
if(typeof jsonObj === "string")
return jsonObj;

if(typeof jsonObj === "object") {
for(var key in jsonObj) {
if(typeof jsonObj[key] === "string")
return jsonObj[key];
}
}

return responseText;
}

if(mime.indexOf("application/problem+json") === 0) {
var jsonObj = safeParseJSON(responseText);

var candidate;
if(typeof jsonObj === "object") {
candidate = jsonObj.title;
if(isNonEmptyString(candidate))
Expand All @@ -390,12 +404,6 @@
candidate = jsonObj.detail;
if(isNonEmptyString(candidate))
return candidate;

for(var key in jsonObj) {
candidate = jsonObj[key];
if(isNonEmptyString(candidate))
return candidate;
}
}

return responseText;
Expand Down

0 comments on commit 151544a

Please sign in to comment.