Skip to content

Commit 0a1ef95

Browse files
committed
Work-around for non-standard values in error source. Fixes #91
1 parent 87a0e92 commit 0a1ef95

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dart:
55
- "2.6.0"
66
- "2.6.1"
77
- "2.7.0"
8+
- "2.8.0"
89
dart_task:
910
- test: --platform vm
1011
- test: --platform chrome

lib/src/document/error_object.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ class ErrorObject implements JsonEncodable {
3232

3333
static ErrorObject fromJson(Object json) {
3434
if (json is Map) {
35+
final source = json['source'];
3536
return ErrorObject(
3637
id: json['id'],
3738
status: json['status'],
3839
code: json['code'],
3940
title: json['title'],
4041
detail: json['detail'],
41-
source: json['source'],
42+
source: source is Map
43+
? source.map(
44+
(key, value) => MapEntry(key.toString(), value.toString()))
45+
: {},
4246
meta: json['meta'],
4347
links: nullable(Link.mapFromJson)(json['links']) ?? const {});
4448
}

test/unit/document/json_api_error_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,18 @@ void main() {
3131
'http://example.com');
3232
});
3333
});
34+
35+
group('parsing', () {
36+
// see https://github.com/f3ath/json-api-dart/issues/91
37+
test('non-standard keys/values in the source object casted to string', () {
38+
final e = ErrorObject.fromJson({
39+
'detail': 'Oops',
40+
'source': {'file': '/some/file.php', 'line': 42, 'parameter': 'foo'}
41+
});
42+
expect(e.detail, 'Oops');
43+
expect(e.source['parameter'], 'foo');
44+
expect(e.source['file'], '/some/file.php');
45+
expect(e.source['line'], '42');
46+
});
47+
});
3448
}

0 commit comments

Comments
 (0)