Skip to content

Fix #77 #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/src/document/resource_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ 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 {
final ResourceObject resourceObject;

ResourceData(this.resourceObject,
{Iterable<ResourceObject> included, Map<String, Link> links})
: super(included: included, links: {...?resourceObject.links, ...?links});
: super(
included: included, links: {...?resourceObject?.links, ...?links});

static ResourceData fromJson(Object json) {
if (json is Map) {
Expand All @@ -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);
Expand All @@ -33,5 +35,5 @@ class ResourceData extends PrimaryData {
'data': resourceObject,
};

Resource unwrap() => resourceObject.unwrap();
Resource unwrap() => resourceObject?.unwrap();
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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:
sdk: '>=2.6.0 <3.0.0'
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
Expand Down
6 changes: 6 additions & 0 deletions test/unit/document/resource_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down