Skip to content

add meta parameter for createResourceAt() #106

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 7 commits into from
Jul 30, 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
8 changes: 4 additions & 4 deletions lib/src/client/json_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class JsonApiClient {
///
/// https://jsonapi.org/format/#crud-creating
Future<Response<ResourceData>> createResourceAt(Uri uri, Resource resource,
{Map<String, String> headers}) =>
_call(_post(uri, headers, _resourceDoc(resource)), ResourceData.fromJson);
{Map<String, String> headers, Map<String, Object> meta}) =>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the argument is not passed down to _resourceDoc().
Please add a unit test supporting this change. It would belong in this folder https://github.com/f3ath/json-api-dart/tree/master/test/unit/client
You can use the ones that are there already as an example.

Copy link
Contributor Author

@dan12411 dan12411 Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, i will fix it, thank you!

Copy link
Contributor Author

@dan12411 dan12411 Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I use naming documentMeta replace meta? 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Please disregard that comment. I removed it once realized that resource meta would be in the resource object itself. So just meta is OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get it!

_call(_post(uri, headers, _resourceDoc(resource, meta: meta)), ResourceData.fromJson);

/// Deletes the resource.
///
Expand Down Expand Up @@ -127,8 +127,8 @@ class JsonApiClient {

final _api = Api(version: '1.0');

Document<ResourceData> _resourceDoc(Resource resource) =>
Document(ResourceData.fromResource(resource), api: _api);
Document<ResourceData> _resourceDoc(Resource resource, {Map<String, Object> meta}) =>
Document(ResourceData.fromResource(resource), meta: meta, api: _api);

Document<ToMany> _toManyDoc(Iterable<Identifier> identifiers) =>
Document(ToMany.fromIdentifiers(identifiers), api: _api);
Expand Down
25 changes: 25 additions & 0 deletions test/unit/client/creating_resource_with_meta_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:json_api/client.dart';
import 'package:json_api/document.dart';
import 'package:json_api/http.dart';
import 'package:test/test.dart';

import '../../helper/test_http_handler.dart';

void main() async {
test('request actually has the meta property', () async {
final handler = TestHttpHandler();
final client = JsonApiClient(handler);

final uri = Uri.parse('https://github.com/f3ath/json-api-dart');
final person =
Resource('people', '123', attributes: {'name': 'Te Cheng Hung'});
final meta = {'friend': 'Martin Fowler'};

handler.nextResponse = HttpResponse(201);
await client.createResourceAt(uri, person, meta: meta);

final request = handler.requestLog.first;
expect(request.body,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave just this expect. The others are unrelated to this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

'{"data":{"type":"people","id":"123","attributes":{"name":"Te Cheng Hung"}},"meta":{"friend":"Martin Fowler"}}');
});
}