Skip to content

Commit bf593ac

Browse files
authored
Fix #77 (#78)
1 parent d97f769 commit bf593ac

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [3.2.2] - 2020-01-07
8+
### Fixed
9+
- Can not decode related resource which is null ([#77](https://github.com/f3ath/json-api-dart/issues/77))
10+
711
## [3.2.1] - 2020-01-01
812
### Fixed
9-
- issue [#74](https://github.com/f3ath/json-api-dart/issues/74)
13+
- Incorrect URL in the example in the Client documentation ([#74](https://github.com/f3ath/json-api-dart/issues/74))
1014

1115
## [3.2.0] - 2019-12-30
1216
### Added
@@ -142,8 +146,8 @@ Most of the changes are **BC-BREAKING**.
142146
### Added
143147
- Client: fetch resources, collections, related resources and relationships
144148

145-
[Unreleased]: https://github.com/f3ath/json-api-dart/compare/3.2.1...HEAD
146-
[3.2.0]: https://github.com/f3ath/json-api-dart/compare/3.2.0...3.2.1
149+
[3.2.2]: https://github.com/f3ath/json-api-dart/compare/3.2.1..3.2.2
150+
[3.2.1]: https://github.com/f3ath/json-api-dart/compare/3.2.0...3.2.1
147151
[3.2.0]: https://github.com/f3ath/json-api-dart/compare/3.1.0...3.2.0
148152
[3.1.0]: https://github.com/f3ath/json-api-dart/compare/3.0.0...3.1.0
149153
[3.0.0]: https://github.com/f3ath/json-api-dart/compare/2.1.0...3.0.0

lib/src/document/resource_data.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import 'package:json_api/src/document/link.dart';
33
import 'package:json_api/src/document/primary_data.dart';
44
import 'package:json_api/src/document/resource.dart';
55
import 'package:json_api/src/document/resource_object.dart';
6+
import 'package:json_api/src/nullable.dart';
67

78
/// Represents a single resource or a single related resource of a to-one relationship
89
class ResourceData extends PrimaryData {
910
final ResourceObject resourceObject;
1011

1112
ResourceData(this.resourceObject,
1213
{Iterable<ResourceObject> included, Map<String, Link> links})
13-
: super(included: included, links: {...?resourceObject.links, ...?links});
14+
: super(
15+
included: included, links: {...?resourceObject?.links, ...?links});
1416

1517
static ResourceData fromJson(Object json) {
1618
if (json is Map) {
@@ -19,7 +21,7 @@ class ResourceData extends PrimaryData {
1921
if (included is List) {
2022
resources.addAll(included.map(ResourceObject.fromJson));
2123
}
22-
final data = ResourceObject.fromJson(json['data']);
24+
final data = nullable(ResourceObject.fromJson)(json['data']);
2325
return ResourceData(data,
2426
links: Link.mapFromJson(json['links'] ?? {}),
2527
included: resources.isNotEmpty ? resources : null);
@@ -33,5 +35,5 @@ class ResourceData extends PrimaryData {
3335
'data': resourceObject,
3436
};
3537

36-
Resource unwrap() => resourceObject.unwrap();
38+
Resource unwrap() => resourceObject?.unwrap();
3739
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: json_api
2-
version: 3.2.1
2+
version: 3.2.2
33
homepage: https://github.com/f3ath/json-api-dart
44
description: JSON:API Client for Flutter, Web and VM. Supports JSON:API v1.0 (http://jsonapi.org)
55
environment:
66
sdk: '>=2.6.0 <3.0.0'
77
dependencies:
88
http: ^0.12.0
99
dev_dependencies:
10-
pedantic: ^1.0.0
10+
pedantic: ^1.9.0
1111
test: ^1.9.2
1212
json_matcher: ^0.2.3
1313
stream_channel: ^2.0.0

test/unit/document/resource_data_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ void main() {
2020
expect(data.unwrap().id, isNull);
2121
});
2222

23+
test('Can decode a related resource which is null', () {
24+
final data =
25+
ResourceData.fromJson(json.decode(json.encode({'data': null})));
26+
expect(data.unwrap(), null);
27+
});
28+
2329
test('Inherits links from ResourceObject', () {
2430
final res = ResourceObject('apples', '1', links: {
2531
'foo': Link(Uri.parse('/foo')),

0 commit comments

Comments
 (0)