From 75b490fe480ed77b5496cc7127ebfcc78f89f278 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 1 Aug 2019 18:33:36 +0200 Subject: [PATCH] Fix #54 (#55) --- CHANGELOG.md | 7 +- lib/src/document/identifier_object.dart | 2 +- lib/src/document/resource_object.dart | 5 +- pubspec.yaml | 2 +- test/unit/document/meta_members_test.dart | 99 +++++++++++++++++++++++ 5 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 test/unit/document/meta_members_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index e01d75f..8fa0bc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.0.2] - 2019-08-01 +### Fixed +- Meta members have incorrect type ([#54](https://github.com/f3ath/json-api-dart/issues/54)) + ## [2.0.1] - 2019-07-12 ### Fixed - Readme example is outdated @@ -81,7 +85,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Client: fetch resources, collections, related resources and relationships -[Unreleased]: https://github.com/f3ath/json-api-dart/compare/2.0.1...HEAD +[Unreleased]: https://github.com/f3ath/json-api-dart/compare/2.0.2...HEAD +[2.0.2]: https://github.com/f3ath/json-api-dart/compare/2.0.1...2.0.2 [2.0.1]: https://github.com/f3ath/json-api-dart/compare/2.0.0...2.0.1 [2.0.0]: https://github.com/f3ath/json-api-dart/compare/1.0.1...2.0.0 [1.0.1]: https://github.com/f3ath/json-api-dart/compare/1.0.0...1.0.1 diff --git a/lib/src/document/identifier_object.dart b/lib/src/document/identifier_object.dart index 4dcebf3..659f4c3 100644 --- a/lib/src/document/identifier_object.dart +++ b/lib/src/document/identifier_object.dart @@ -7,7 +7,7 @@ class IdentifierObject { final String type; final String id; - final Map meta; + final Map meta; IdentifierObject(this.type, this.id, {this.meta}); diff --git a/lib/src/document/resource_object.dart b/lib/src/document/resource_object.dart index 81c697a..e184122 100644 --- a/lib/src/document/resource_object.dart +++ b/lib/src/document/resource_object.dart @@ -18,7 +18,7 @@ class ResourceObject { final Link self; final Map attributes; final Map relationships; - final Map meta; + final Map meta; ResourceObject(this.type, this.id, {this.self, @@ -40,7 +40,8 @@ class ResourceObject { return ResourceObject(json['type'], json['id'], attributes: attributes, relationships: Relationship.decodeJsonMap(relationships), - self: links['self']); + self: links['self'], + meta: json['meta']); } } throw DecodingException('Can not decode ResourceObject from $json'); diff --git a/pubspec.yaml b/pubspec.yaml index 6fa7e7d..bbd4e26 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ author: "Alexey Karapetov " description: "JSON:API Client for Flutter, Web and VM. Supports JSON:API v1.0 (http://jsonapi.org)" homepage: "https://github.com/f3ath/json-api-dart" name: "json_api" -version: "2.0.1+1" +version: "2.0.2" dependencies: collection: "^1.14.11" http: "^0.12.0" diff --git a/test/unit/document/meta_members_test.dart b/test/unit/document/meta_members_test.dart new file mode 100644 index 0000000..9af8383 --- /dev/null +++ b/test/unit/document/meta_members_test.dart @@ -0,0 +1,99 @@ +import 'package:json_api/document.dart'; +import 'package:test/test.dart'; + +void main() { + group('Meta members', () { + test('should be parsed correctly', () { + final meta = { + "bool": true, + "array": [1, 2, 3], + "string": "foo" + }; + final json = { + "links": { + "self": "http://example.com/articles", + "next": "http://example.com/articles?page=2", + "last": "http://example.com/articles?page=10" + }, + "meta": meta, + "data": [ + { + "type": "articles", + "id": "1", + "attributes": {"title": "JSON:API paints my bikeshed!"}, + "meta": meta, + "relationships": { + "author": { + "links": { + "self": "http://example.com/articles/1/relationships/author", + "related": "http://example.com/articles/1/author" + }, + "data": {"type": "people", "id": "9"} + }, + "comments": { + "links": { + "self": + "http://example.com/articles/1/relationships/comments", + "related": "http://example.com/articles/1/comments" + }, + "data": [ + { + "type": "comments", + "id": "5", + "meta": meta, + }, + {"type": "comments", "id": "12"} + ] + } + }, + "links": {"self": "http://example.com/articles/1"} + } + ], + "included": [ + { + "type": "people", + "id": "9", + "attributes": { + "firstName": "Dan", + "lastName": "Gebhardt", + "twitter": "dgeb" + }, + "links": {"self": "http://example.com/people/9"} + }, + { + "type": "comments", + "id": "5", + "attributes": {"body": "First!"}, + "relationships": { + "author": { + "data": {"type": "people", "id": "2"} + } + }, + "links": {"self": "http://example.com/comments/5"} + }, + { + "type": "comments", + "id": "12", + "attributes": {"body": "I like XML better"}, + "relationships": { + "author": { + "data": {"type": "people", "id": "9"} + } + }, + "links": {"self": "http://example.com/comments/12"} + } + ] + }; + + final doc = Document.decodeJson(json, ResourceCollectionData.decodeJson); + expect(doc.meta["bool"], true); + expect(doc.data.collection.first.meta, meta); + expect( + (doc.data.collection.first.relationships['comments'] as ToMany) + .linkage + .first + .meta, + meta); + }); + }); +}