diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e6004d..c38419f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.2.2] - 2020-01-07 +### Fixed +- Can not decode related resource which is null ([#77](https://github.com/f3ath/json-api-dart/issues/77)) + ## [3.2.1] - 2020-01-01 ### Fixed -- issue [#74](https://github.com/f3ath/json-api-dart/issues/74) +- Incorrect URL in the example in the Client documentation ([#74](https://github.com/f3ath/json-api-dart/issues/74)) ## [3.2.0] - 2019-12-30 ### Added @@ -142,8 +146,8 @@ Most of the changes are **BC-BREAKING**. ### Added - Client: fetch resources, collections, related resources and relationships -[Unreleased]: https://github.com/f3ath/json-api-dart/compare/3.2.1...HEAD -[3.2.0]: https://github.com/f3ath/json-api-dart/compare/3.2.0...3.2.1 +[3.2.2]: https://github.com/f3ath/json-api-dart/compare/3.2.1..3.2.2 +[3.2.1]: https://github.com/f3ath/json-api-dart/compare/3.2.0...3.2.1 [3.2.0]: https://github.com/f3ath/json-api-dart/compare/3.1.0...3.2.0 [3.1.0]: https://github.com/f3ath/json-api-dart/compare/3.0.0...3.1.0 [3.0.0]: https://github.com/f3ath/json-api-dart/compare/2.1.0...3.0.0 diff --git a/lib/src/document/resource_data.dart b/lib/src/document/resource_data.dart index f89be68..2fc5817 100644 --- a/lib/src/document/resource_data.dart +++ b/lib/src/document/resource_data.dart @@ -3,6 +3,7 @@ import 'package:json_api/src/document/link.dart'; import 'package:json_api/src/document/primary_data.dart'; import 'package:json_api/src/document/resource.dart'; import 'package:json_api/src/document/resource_object.dart'; +import 'package:json_api/src/nullable.dart'; /// Represents a single resource or a single related resource of a to-one relationship class ResourceData extends PrimaryData { @@ -10,7 +11,8 @@ class ResourceData extends PrimaryData { ResourceData(this.resourceObject, {Iterable included, Map links}) - : super(included: included, links: {...?resourceObject.links, ...?links}); + : super( + included: included, links: {...?resourceObject?.links, ...?links}); static ResourceData fromJson(Object json) { if (json is Map) { @@ -19,7 +21,7 @@ class ResourceData extends PrimaryData { if (included is List) { resources.addAll(included.map(ResourceObject.fromJson)); } - final data = ResourceObject.fromJson(json['data']); + final data = nullable(ResourceObject.fromJson)(json['data']); return ResourceData(data, links: Link.mapFromJson(json['links'] ?? {}), included: resources.isNotEmpty ? resources : null); @@ -33,5 +35,5 @@ class ResourceData extends PrimaryData { 'data': resourceObject, }; - Resource unwrap() => resourceObject.unwrap(); + Resource unwrap() => resourceObject?.unwrap(); } diff --git a/pubspec.yaml b/pubspec.yaml index d097701..e0f3748 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: json_api -version: 3.2.1 +version: 3.2.2 homepage: https://github.com/f3ath/json-api-dart description: JSON:API Client for Flutter, Web and VM. Supports JSON:API v1.0 (http://jsonapi.org) environment: @@ -7,7 +7,7 @@ environment: dependencies: http: ^0.12.0 dev_dependencies: - pedantic: ^1.0.0 + pedantic: ^1.9.0 test: ^1.9.2 json_matcher: ^0.2.3 stream_channel: ^2.0.0 diff --git a/test/unit/document/resource_data_test.dart b/test/unit/document/resource_data_test.dart index 65d7d11..4c4c71e 100644 --- a/test/unit/document/resource_data_test.dart +++ b/test/unit/document/resource_data_test.dart @@ -20,6 +20,12 @@ void main() { expect(data.unwrap().id, isNull); }); + test('Can decode a related resource which is null', () { + final data = + ResourceData.fromJson(json.decode(json.encode({'data': null}))); + expect(data.unwrap(), null); + }); + test('Inherits links from ResourceObject', () { final res = ResourceObject('apples', '1', links: { 'foo': Link(Uri.parse('/foo')),