active_model_serializers 0.10.0 → 0.10.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +239 -2
- data/README.md +171 -34
- data/lib/action_controller/serialization.rb +23 -3
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +4 -0
- data/lib/active_model/serializer/adapter/json.rb +2 -0
- data/lib/active_model/serializer/adapter/json_api.rb +2 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -0
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +10 -5
- data/lib/active_model/serializer/association.rb +64 -10
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
- data/lib/active_model/serializer/collection_serializer.rb +48 -13
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +89 -117
- data/lib/active_model/serializer/error_serializer.rb +13 -7
- data/lib/active_model/serializer/errors_serializer.rb +27 -20
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +3 -1
- data/lib/active_model/serializer/has_many_reflection.rb +5 -3
- data/lib/active_model/serializer/has_one_reflection.rb +3 -4
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +136 -130
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +130 -65
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model/serializer.rb +321 -86
- data/lib/active_model_serializers/adapter/attributes.rb +17 -57
- data/lib/active_model_serializers/adapter/base.rb +41 -39
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
- data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +3 -1
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +52 -20
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
- data/lib/active_model_serializers/adapter/json_api.rb +84 -65
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/adapter.rb +9 -1
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +3 -2
- data/lib/active_model_serializers/deserialization.rb +4 -0
- data/lib/active_model_serializers/json_pointer.rb +2 -0
- data/lib/active_model_serializers/logging.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model/caching.rb +26 -0
- data/lib/active_model_serializers/model.rb +111 -28
- data/lib/active_model_serializers/railtie.rb +7 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
- data/lib/active_model_serializers/serializable_resource.rb +10 -7
- data/lib/active_model_serializers/serialization_context.rb +12 -3
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers.rb +35 -10
- data/lib/generators/rails/resource_override.rb +3 -1
- data/lib/generators/rails/serializer_generator.rb +6 -4
- data/lib/grape/active_model_serializers.rb +9 -5
- data/lib/grape/formatters/active_model_serializers.rb +21 -2
- data/lib/grape/helpers/active_model_serializers.rb +3 -0
- data/lib/tasks/rubocop.rake +55 -0
- metadata +104 -296
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -104
- data/.rubocop_todo.yml +0 -167
- data/.simplecov +0 -110
- data/.travis.yml +0 -43
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -53
- data/Rakefile +0 -103
- data/active_model_serializers.gemspec +0 -66
- data/appveyor.yml +0 -24
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/serve_benchmark +0 -39
- data/docs/ARCHITECTURE.md +0 -126
- data/docs/README.md +0 -40
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -245
- data/docs/general/caching.md +0 -52
- data/docs/general/configuration_options.md +0 -100
- data/docs/general/deserialization.md +0 -100
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -14
- data/docs/general/rendering.md +0 -255
- data/docs/general/serializers.md +0 -372
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -139
- data/docs/howto/add_root_key.md +0 -51
- data/docs/howto/outside_controller_use.md +0 -58
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -32
- data/docs/howto/test.md +0 -152
- data/docs/integrations/ember-and-json-api.md +0 -112
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/jsonapi/schema.md +0 -151
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/lib/active_model/serializer/associations.rb +0 -100
- data/lib/active_model/serializer/attributes.rb +0 -82
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/configuration.rb +0 -35
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model/serializer/links.rb +0 -35
- data/lib/active_model/serializer/meta.rb +0 -29
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model/serializer/type.rb +0 -25
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/action_controller/adapter_selector_test.rb +0 -53
- data/test/action_controller/explicit_serializer_test.rb +0 -134
- data/test/action_controller/json/include_test.rb +0 -167
- data/test/action_controller/json_api/deserialization_test.rb +0 -112
- data/test/action_controller/json_api/errors_test.rb +0 -41
- data/test/action_controller/json_api/linked_test.rb +0 -197
- data/test/action_controller/json_api/pagination_test.rb +0 -116
- data/test/action_controller/json_api/transform_test.rb +0 -181
- data/test/action_controller/serialization_scope_name_test.rb +0 -229
- data/test/action_controller/serialization_test.rb +0 -469
- data/test/active_model_serializers/adapter_for_test.rb +0 -208
- data/test/active_model_serializers/json_pointer_test.rb +0 -20
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -9
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -58
- data/test/active_model_serializers/test/schema_test.rb +0 -130
- data/test/active_model_serializers/test/serializer_test.rb +0 -62
- data/test/active_record_test.rb +0 -9
- data/test/adapter/deprecation_test.rb +0 -100
- data/test/adapter/json/belongs_to_test.rb +0 -45
- data/test/adapter/json/collection_test.rb +0 -90
- data/test/adapter/json/has_many_test.rb +0 -45
- data/test/adapter/json/transform_test.rb +0 -93
- data/test/adapter/json_api/belongs_to_test.rb +0 -155
- data/test/adapter/json_api/collection_test.rb +0 -95
- data/test/adapter/json_api/errors_test.rb +0 -78
- data/test/adapter/json_api/fields_test.rb +0 -87
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
- data/test/adapter/json_api/has_many_test.rb +0 -144
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/json_api_test.rb +0 -35
- data/test/adapter/json_api/linked_test.rb +0 -392
- data/test/adapter/json_api/links_test.rb +0 -93
- data/test/adapter/json_api/pagination_links_test.rb +0 -166
- data/test/adapter/json_api/parse_test.rb +0 -137
- data/test/adapter/json_api/relationship_test.rb +0 -161
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/adapter/json_api/resource_identifier_test.rb +0 -85
- data/test/adapter/json_api/resource_meta_test.rb +0 -100
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
- data/test/adapter/json_api/transform_test.rb +0 -502
- data/test/adapter/json_api/type_test.rb +0 -61
- data/test/adapter/json_test.rb +0 -45
- data/test/adapter/null_test.rb +0 -23
- data/test/adapter/polymorphic_test.rb +0 -171
- data/test/adapter_test.rb +0 -67
- data/test/array_serializer_test.rb +0 -22
- data/test/benchmark/app.rb +0 -65
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_caching.rb +0 -119
- data/test/benchmark/bm_transform.rb +0 -34
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -84
- data/test/benchmark/fixtures.rb +0 -219
- data/test/cache_test.rb +0 -485
- data/test/collection_serializer_test.rb +0 -110
- data/test/fixtures/active_record.rb +0 -78
- data/test/fixtures/poro.rb +0 -282
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -57
- data/test/grape_test.rb +0 -82
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
- data/test/lint_test.rb +0 -49
- data/test/logger_test.rb +0 -18
- data/test/poro_test.rb +0 -9
- data/test/serializable_resource_test.rb +0 -83
- data/test/serializers/association_macros_test.rb +0 -36
- data/test/serializers/associations_test.rb +0 -295
- data/test/serializers/attribute_test.rb +0 -151
- data/test/serializers/attributes_test.rb +0 -52
- data/test/serializers/caching_configuration_test_isolated.rb +0 -170
- data/test/serializers/configuration_test.rb +0 -32
- data/test/serializers/fieldset_test.rb +0 -14
- data/test/serializers/meta_test.rb +0 -196
- data/test/serializers/options_test.rb +0 -21
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- data/test/serializers/root_test.rb +0 -21
- data/test/serializers/serialization_test.rb +0 -55
- data/test/serializers/serializer_for_test.rb +0 -134
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -79
- data/test/support/rails5_shims.rb +0 -47
- data/test/support/rails_app.rb +0 -45
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -53
- data/test/test_helper.rb +0 -57
data/docs/howto/test.md
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
# How to test
|
|
2
|
-
|
|
3
|
-
## Controller Serializer Usage
|
|
4
|
-
|
|
5
|
-
ActiveModelSerializers provides a `assert_serializer` method to be used on your controller tests to
|
|
6
|
-
assert that a specific serializer was used.
|
|
7
|
-
|
|
8
|
-
```ruby
|
|
9
|
-
class PostsControllerTest < ActionController::TestCase
|
|
10
|
-
test "should render post serializer" do
|
|
11
|
-
get :index
|
|
12
|
-
assert_serializer "PostSerializer"
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
See [ActiveModelSerializers::Test::Serializer](../../lib/active_model_serializers/test/serializer.rb)
|
|
18
|
-
for more examples and documentation.
|
|
19
|
-
|
|
20
|
-
## Serialization against a schema
|
|
21
|
-
|
|
22
|
-
### Dependencies
|
|
23
|
-
|
|
24
|
-
To use the `assert_response_schema` you need to have the
|
|
25
|
-
[`json_schema`](https://github.com/brandur/json_schema) on your Gemfile. Please
|
|
26
|
-
add it to your Gemfile and run `$ bundle install`.
|
|
27
|
-
|
|
28
|
-
### Minitest test helpers
|
|
29
|
-
|
|
30
|
-
ActiveModelSerializers provides a `assert_response_schema` method to be used on your controller tests to
|
|
31
|
-
assert the response against a [JSON Schema](http://json-schema.org/). Let's take
|
|
32
|
-
a look in an example.
|
|
33
|
-
|
|
34
|
-
```ruby
|
|
35
|
-
class PostsController < ApplicationController
|
|
36
|
-
def show
|
|
37
|
-
@post = Post.find(params[:id])
|
|
38
|
-
|
|
39
|
-
render json: @post
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
To test the `posts#show` response of this controller we need to create a file
|
|
45
|
-
named `test/support/schemas/posts/show.json`. The helper uses a naming convention
|
|
46
|
-
to locate the file.
|
|
47
|
-
|
|
48
|
-
This file is a JSON Schema representation of our response.
|
|
49
|
-
|
|
50
|
-
```json
|
|
51
|
-
{
|
|
52
|
-
"properties": {
|
|
53
|
-
"title" : { "type" : "string" },
|
|
54
|
-
"content" : { "type" : "string" }
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
With all in place we can go to our test and use the helper.
|
|
60
|
-
|
|
61
|
-
```ruby
|
|
62
|
-
class PostsControllerTest < ActionController::TestCase
|
|
63
|
-
test "should render right response" do
|
|
64
|
-
get :index
|
|
65
|
-
assert_response_schema
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
#### Load a custom schema
|
|
71
|
-
|
|
72
|
-
If we need to use another schema, for example when we have a namespaced API that
|
|
73
|
-
shows the same response, we can pass the path of the schema.
|
|
74
|
-
|
|
75
|
-
```ruby
|
|
76
|
-
module V1
|
|
77
|
-
class PostsController < ApplicationController
|
|
78
|
-
def show
|
|
79
|
-
@post = Post.find(params[:id])
|
|
80
|
-
|
|
81
|
-
render json: @post
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
```ruby
|
|
88
|
-
class V1::PostsControllerTest < ActionController::TestCase
|
|
89
|
-
test "should render right response" do
|
|
90
|
-
get :index
|
|
91
|
-
assert_response_schema('posts/show.json')
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
#### Change the schema path
|
|
97
|
-
|
|
98
|
-
By default all schemas are created at `test/support/schemas`. If we are using
|
|
99
|
-
RSpec for example we can change this to `spec/support/schemas` defining the
|
|
100
|
-
default schema path in an initializer.
|
|
101
|
-
|
|
102
|
-
```ruby
|
|
103
|
-
ActiveModelSerializers.config.schema_path = 'spec/support/schemas'
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
#### Using with the Heroku’s JSON Schema-based tools
|
|
107
|
-
|
|
108
|
-
To use the test helper with the [prmd](https://github.com/interagent/prmd) and
|
|
109
|
-
[committee](https://github.com/interagent/committee).
|
|
110
|
-
|
|
111
|
-
We need to change the schema path to the recommended by prmd:
|
|
112
|
-
|
|
113
|
-
```ruby
|
|
114
|
-
ActiveModelSerializers.config.schema_path = 'docs/schema/schemata'
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
We also need to structure our schemata according to Heroku's conventions
|
|
118
|
-
(e.g. including
|
|
119
|
-
[required metadata](https://github.com/interagent/prmd/blob/master/docs/schemata.md#meta-data)
|
|
120
|
-
and [links](https://github.com/interagent/prmd/blob/master/docs/schemata.md#links).
|
|
121
|
-
|
|
122
|
-
#### JSON Pointers
|
|
123
|
-
|
|
124
|
-
If we plan to use [JSON
|
|
125
|
-
Pointers](http://spacetelescope.github.io/understanding-json-schema/UnderstandingJSONSchema.pdf) we need to define the `id` attribute on the schema. Example:
|
|
126
|
-
|
|
127
|
-
```js
|
|
128
|
-
// attributes.json
|
|
129
|
-
|
|
130
|
-
{
|
|
131
|
-
"id": "file://attributes.json#",
|
|
132
|
-
"properties": {
|
|
133
|
-
"name" : { "type" : "string" },
|
|
134
|
-
"description" : { "type" : "string" }
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
```js
|
|
140
|
-
// show.json
|
|
141
|
-
|
|
142
|
-
{
|
|
143
|
-
"properties": {
|
|
144
|
-
"name": {
|
|
145
|
-
"$ref": "file://attributes.json#/properties/name"
|
|
146
|
-
},
|
|
147
|
-
"description": {
|
|
148
|
-
"$ref": "file://attributes.json#/properties/description"
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
```
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
[Back to Guides](../README.md)
|
|
2
|
-
|
|
3
|
-
# Integrating with Ember and JSON API
|
|
4
|
-
|
|
5
|
-
- [Preparation](./ember-and-json-api.md#preparation)
|
|
6
|
-
- [Server-Side Changes](./ember-and-json-api.md#server-side-changes)
|
|
7
|
-
- [Adapter Changes](./ember-and-json-api.md#adapter-changes)
|
|
8
|
-
- [Serializer Changes](./ember-and-json-api.md#serializer-changes)
|
|
9
|
-
- [Including Nested Resources](./ember-and-json-api.md#including-nested-resources)
|
|
10
|
-
|
|
11
|
-
## Preparation
|
|
12
|
-
|
|
13
|
-
Note: This guide assumes that `ember-cli` is used for your ember app.
|
|
14
|
-
|
|
15
|
-
The JSON API specification calls for hyphens for multi-word separators. ActiveModelSerializers uses underscores.
|
|
16
|
-
To solve this, in Ember, both the adapter and the serializer will need some modifications:
|
|
17
|
-
|
|
18
|
-
### Server-Side Changes
|
|
19
|
-
|
|
20
|
-
there are multiple mimetypes for json that should all be parsed similarly, so
|
|
21
|
-
in `config/initializers/mime_types.rb`:
|
|
22
|
-
```ruby
|
|
23
|
-
api_mime_types = %W(
|
|
24
|
-
application/vnd.api+json
|
|
25
|
-
text/x-json
|
|
26
|
-
application/json
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
Mime::Type.unregister :json
|
|
30
|
-
Mime::Type.register 'application/json', :json, api_mime_types
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### Adapter Changes
|
|
34
|
-
|
|
35
|
-
```javascript
|
|
36
|
-
// app/adapters/application.js
|
|
37
|
-
import DS from 'ember-data';
|
|
38
|
-
import ENV from "../config/environment";
|
|
39
|
-
|
|
40
|
-
export default DS.JSONAPIAdapter.extend({
|
|
41
|
-
namespace: 'api',
|
|
42
|
-
// if your rails app is on a different port from your ember app
|
|
43
|
-
// this can be helpful for development.
|
|
44
|
-
// in production, the host for both rails and ember should be the same.
|
|
45
|
-
host: ENV.host,
|
|
46
|
-
|
|
47
|
-
// allows the multiword paths in urls to be underscored
|
|
48
|
-
pathForType: function(type) {
|
|
49
|
-
let underscored = Ember.String.underscore(type);
|
|
50
|
-
return Ember.String.pluralize(underscored);
|
|
51
|
-
},
|
|
52
|
-
|
|
53
|
-
// allows queries to be sent along with a findRecord
|
|
54
|
-
// hopefully Ember / EmberData will soon have this built in
|
|
55
|
-
// ember-data issue tracked here:
|
|
56
|
-
// https://github.com/emberjs/data/issues/3596
|
|
57
|
-
urlForFindRecord(id, modelName, snapshot) {
|
|
58
|
-
let url = this._super(...arguments);
|
|
59
|
-
let query = Ember.get(snapshot, 'adapterOptions.query');
|
|
60
|
-
if(query) {
|
|
61
|
-
url += '?' + Ember.$.param(query);
|
|
62
|
-
}
|
|
63
|
-
return url;
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### Serializer Changes
|
|
69
|
-
|
|
70
|
-
```javascript
|
|
71
|
-
// app/serializers/application.js
|
|
72
|
-
import Ember from 'ember';
|
|
73
|
-
import DS from 'ember-data';
|
|
74
|
-
var underscore = Ember.String.underscore;
|
|
75
|
-
|
|
76
|
-
export default DS.JSONAPISerializer.extend({
|
|
77
|
-
keyForAttribute: function(attr) {
|
|
78
|
-
return underscore(attr);
|
|
79
|
-
},
|
|
80
|
-
|
|
81
|
-
keyForRelationship: function(rawKey) {
|
|
82
|
-
return underscore(rawKey);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
## Including Nested Resources
|
|
89
|
-
|
|
90
|
-
Previously, `store.find` and `store.findRecord` did not allow specification of any query params.
|
|
91
|
-
The ActiveModelSerializers default for the `include` parameter is to be `nil` meaning that if any associations are defined in your serializer, only the `id` and `type` will be in the `relationships` structure of the JSON API response.
|
|
92
|
-
For more on `include` usage, see: [The JSON API include examples](./../general/adapters.md#JSON-API)
|
|
93
|
-
|
|
94
|
-
With the above modifications, you can execute code as below in order to include nested resources while doing a find query.
|
|
95
|
-
|
|
96
|
-
```javascript
|
|
97
|
-
store.findRecord('post', postId, { adapterOptions: { query: { include: 'comments' } } });
|
|
98
|
-
```
|
|
99
|
-
will generate the path `/posts/{postId}?include='comments'`
|
|
100
|
-
|
|
101
|
-
So then in your controller, you'll want to be sure to have something like:
|
|
102
|
-
```ruby
|
|
103
|
-
render json: @post, include: params[:include]
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
If you want to use `include` on a collection, you'd write something like this:
|
|
107
|
-
|
|
108
|
-
```javascript
|
|
109
|
-
store.query('post', { include: 'comments' });
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
which will generate the path `/posts?include='comments'`
|
data/docs/integrations/grape.md
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# Integration with Grape
|
|
2
|
-
|
|
3
|
-
[Grape](https://github.com/ruby-grape/grape) is an opinionated micro-framework for creating REST-like APIs in ruby.
|
|
4
|
-
|
|
5
|
-
ActiveModelSerializers currently supports Grape >= 0.13, < 1.0
|
|
6
|
-
|
|
7
|
-
To add [Grape](https://github.com/ruby-grape/grape) support, enable the formatter and helper functions by including `Grape::ActiveModelSerializers` in your base endpoint. For example:
|
|
8
|
-
|
|
9
|
-
```ruby
|
|
10
|
-
module Example
|
|
11
|
-
class Dummy < Grape::API
|
|
12
|
-
require 'grape/active_model_serializers'
|
|
13
|
-
include Grape::ActiveModelSerializers
|
|
14
|
-
mount Example::V1::Base
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Aside from this, [configuration](../general/configuration_options.md) of ActiveModelSerializers is exactly the same.
|
data/docs/jsonapi/errors.md
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
[Back to Guides](../README.md)
|
|
2
|
-
|
|
3
|
-
# [JSON API Errors](http://jsonapi.org/format/#errors)
|
|
4
|
-
|
|
5
|
-
Rendering error documents requires specifying the error serializer(s):
|
|
6
|
-
|
|
7
|
-
- Serializer:
|
|
8
|
-
- For a single resource: `serializer: ActiveModel::Serializer::ErrorSerializer`.
|
|
9
|
-
- For a collection: `serializer: ActiveModel::Serializer::ErrorsSerializer`, `each_serializer: ActiveModel::Serializer::ErrorSerializer`.
|
|
10
|
-
|
|
11
|
-
The resource **MUST** have a non-empty associated `#errors` object.
|
|
12
|
-
The `errors` object must have a `#messages` method that returns a hash of error name to array of
|
|
13
|
-
descriptions.
|
|
14
|
-
|
|
15
|
-
## Use in controllers
|
|
16
|
-
|
|
17
|
-
```ruby
|
|
18
|
-
resource = Profile.new(name: 'Name 1',
|
|
19
|
-
description: 'Description 1',
|
|
20
|
-
comments: 'Comments 1')
|
|
21
|
-
resource.errors.add(:name, 'cannot be nil')
|
|
22
|
-
resource.errors.add(:name, 'must be longer')
|
|
23
|
-
resource.errors.add(:id, 'must be a uuid')
|
|
24
|
-
|
|
25
|
-
render json: resource, status: 422, adapter: :json_api, serializer: ActiveModel::Serializer::ErrorSerializer
|
|
26
|
-
# #=>
|
|
27
|
-
# { :errors =>
|
|
28
|
-
# [
|
|
29
|
-
# { :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
|
|
30
|
-
# { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
|
|
31
|
-
# { :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
|
|
32
|
-
# ]
|
|
33
|
-
# }.to_json
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
## Direct error document generation
|
|
37
|
-
|
|
38
|
-
```ruby
|
|
39
|
-
options = nil
|
|
40
|
-
resource = ModelWithErrors.new
|
|
41
|
-
resource.errors.add(:name, 'must be awesome')
|
|
42
|
-
|
|
43
|
-
serializable_resource = ActiveModelSerializers::SerializableResource.new(
|
|
44
|
-
resource, {
|
|
45
|
-
serializer: ActiveModel::Serializer::ErrorSerializer,
|
|
46
|
-
adapter: :json_api
|
|
47
|
-
})
|
|
48
|
-
serializable_resource.as_json(options)
|
|
49
|
-
# #=>
|
|
50
|
-
# {
|
|
51
|
-
# :errors =>
|
|
52
|
-
# [
|
|
53
|
-
# { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
|
|
54
|
-
# ]
|
|
55
|
-
# }
|
|
56
|
-
```
|
|
@@ -1,366 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "http://json-schema.org/draft-04/schema#",
|
|
3
|
-
"title": "JSON API Schema",
|
|
4
|
-
"description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
|
|
5
|
-
"oneOf": [
|
|
6
|
-
{
|
|
7
|
-
"$ref": "#/definitions/success"
|
|
8
|
-
},
|
|
9
|
-
{
|
|
10
|
-
"$ref": "#/definitions/failure"
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
"$ref": "#/definitions/info"
|
|
14
|
-
}
|
|
15
|
-
],
|
|
16
|
-
|
|
17
|
-
"definitions": {
|
|
18
|
-
"success": {
|
|
19
|
-
"type": "object",
|
|
20
|
-
"required": [
|
|
21
|
-
"data"
|
|
22
|
-
],
|
|
23
|
-
"properties": {
|
|
24
|
-
"data": {
|
|
25
|
-
"$ref": "#/definitions/data"
|
|
26
|
-
},
|
|
27
|
-
"included": {
|
|
28
|
-
"description": "To reduce the number of HTTP requests, servers **MAY** allow responses that include related resources along with the requested primary resources. Such responses are called \"compound documents\".",
|
|
29
|
-
"type": "array",
|
|
30
|
-
"items": {
|
|
31
|
-
"$ref": "#/definitions/resource"
|
|
32
|
-
},
|
|
33
|
-
"uniqueItems": true
|
|
34
|
-
},
|
|
35
|
-
"meta": {
|
|
36
|
-
"$ref": "#/definitions/meta"
|
|
37
|
-
},
|
|
38
|
-
"links": {
|
|
39
|
-
"description": "Link members related to the primary data.",
|
|
40
|
-
"allOf": [
|
|
41
|
-
{
|
|
42
|
-
"$ref": "#/definitions/links"
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
"$ref": "#/definitions/pagination"
|
|
46
|
-
}
|
|
47
|
-
]
|
|
48
|
-
},
|
|
49
|
-
"jsonapi": {
|
|
50
|
-
"$ref": "#/definitions/jsonapi"
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
"additionalProperties": false
|
|
54
|
-
},
|
|
55
|
-
"failure": {
|
|
56
|
-
"type": "object",
|
|
57
|
-
"required": [
|
|
58
|
-
"errors"
|
|
59
|
-
],
|
|
60
|
-
"properties": {
|
|
61
|
-
"errors": {
|
|
62
|
-
"type": "array",
|
|
63
|
-
"items": {
|
|
64
|
-
"$ref": "#/definitions/error"
|
|
65
|
-
},
|
|
66
|
-
"uniqueItems": true
|
|
67
|
-
},
|
|
68
|
-
"meta": {
|
|
69
|
-
"$ref": "#/definitions/meta"
|
|
70
|
-
},
|
|
71
|
-
"jsonapi": {
|
|
72
|
-
"$ref": "#/definitions/jsonapi"
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
"additionalProperties": false
|
|
76
|
-
},
|
|
77
|
-
"info": {
|
|
78
|
-
"type": "object",
|
|
79
|
-
"required": [
|
|
80
|
-
"meta"
|
|
81
|
-
],
|
|
82
|
-
"properties": {
|
|
83
|
-
"meta": {
|
|
84
|
-
"$ref": "#/definitions/meta"
|
|
85
|
-
},
|
|
86
|
-
"links": {
|
|
87
|
-
"$ref": "#/definitions/links"
|
|
88
|
-
},
|
|
89
|
-
"jsonapi": {
|
|
90
|
-
"$ref": "#/definitions/jsonapi"
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
"additionalProperties": false
|
|
94
|
-
},
|
|
95
|
-
|
|
96
|
-
"meta": {
|
|
97
|
-
"description": "Non-standard meta-information that can not be represented as an attribute or relationship.",
|
|
98
|
-
"type": "object",
|
|
99
|
-
"additionalProperties": true
|
|
100
|
-
},
|
|
101
|
-
"data": {
|
|
102
|
-
"description": "The document's \"primary data\" is a representation of the resource or collection of resources targeted by a request.",
|
|
103
|
-
"oneOf": [
|
|
104
|
-
{
|
|
105
|
-
"$ref": "#/definitions/resource"
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
"description": "An array of resource objects, an array of resource identifier objects, or an empty array ([]), for requests that target resource collections.",
|
|
109
|
-
"type": "array",
|
|
110
|
-
"items": {
|
|
111
|
-
"$ref": "#/definitions/resource"
|
|
112
|
-
},
|
|
113
|
-
"uniqueItems": true
|
|
114
|
-
}
|
|
115
|
-
]
|
|
116
|
-
},
|
|
117
|
-
"resource": {
|
|
118
|
-
"description": "\"Resource objects\" appear in a JSON API document to represent resources.",
|
|
119
|
-
"type": "object",
|
|
120
|
-
"required": [
|
|
121
|
-
"type",
|
|
122
|
-
"id"
|
|
123
|
-
],
|
|
124
|
-
"properties": {
|
|
125
|
-
"type": {
|
|
126
|
-
"type": "string"
|
|
127
|
-
},
|
|
128
|
-
"id": {
|
|
129
|
-
"type": "string"
|
|
130
|
-
},
|
|
131
|
-
"attributes": {
|
|
132
|
-
"$ref": "#/definitions/attributes"
|
|
133
|
-
},
|
|
134
|
-
"relationships": {
|
|
135
|
-
"$ref": "#/definitions/relationships"
|
|
136
|
-
},
|
|
137
|
-
"links": {
|
|
138
|
-
"$ref": "#/definitions/links"
|
|
139
|
-
},
|
|
140
|
-
"meta": {
|
|
141
|
-
"$ref": "#/definitions/meta"
|
|
142
|
-
}
|
|
143
|
-
},
|
|
144
|
-
"additionalProperties": false
|
|
145
|
-
},
|
|
146
|
-
|
|
147
|
-
"links": {
|
|
148
|
-
"description": "A resource object **MAY** contain references to other resource objects (\"relationships\"). Relationships may be to-one or to-many. Relationships can be specified by including a member in a resource's links object.",
|
|
149
|
-
"type": "object",
|
|
150
|
-
"properties": {
|
|
151
|
-
"self": {
|
|
152
|
-
"description": "A `self` member, whose value is a URL for the relationship itself (a \"relationship URL\"). This URL allows the client to directly manipulate the relationship. For example, it would allow a client to remove an `author` from an `article` without deleting the people resource itself.",
|
|
153
|
-
"type": "string",
|
|
154
|
-
"format": "uri"
|
|
155
|
-
},
|
|
156
|
-
"related": {
|
|
157
|
-
"$ref": "#/definitions/link"
|
|
158
|
-
}
|
|
159
|
-
},
|
|
160
|
-
"additionalProperties": true
|
|
161
|
-
},
|
|
162
|
-
"link": {
|
|
163
|
-
"description": "A link **MUST** be represented as either: a string containing the link's URL or a link object.",
|
|
164
|
-
"oneOf": [
|
|
165
|
-
{
|
|
166
|
-
"description": "A string containing the link's URL.",
|
|
167
|
-
"type": "string",
|
|
168
|
-
"format": "uri"
|
|
169
|
-
},
|
|
170
|
-
{
|
|
171
|
-
"type": "object",
|
|
172
|
-
"required": [
|
|
173
|
-
"href"
|
|
174
|
-
],
|
|
175
|
-
"properties": {
|
|
176
|
-
"href": {
|
|
177
|
-
"description": "A string containing the link's URL.",
|
|
178
|
-
"type": "string",
|
|
179
|
-
"format": "uri"
|
|
180
|
-
},
|
|
181
|
-
"meta": {
|
|
182
|
-
"$ref": "#/definitions/meta"
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
]
|
|
187
|
-
},
|
|
188
|
-
|
|
189
|
-
"attributes": {
|
|
190
|
-
"description": "Members of the attributes object (\"attributes\") represent information about the resource object in which it's defined.",
|
|
191
|
-
"type": "object",
|
|
192
|
-
"patternProperties": {
|
|
193
|
-
"^(?!relationships$|links$)\\w[-\\w_]*$": {
|
|
194
|
-
"description": "Attributes may contain any valid JSON value."
|
|
195
|
-
}
|
|
196
|
-
},
|
|
197
|
-
"additionalProperties": false
|
|
198
|
-
},
|
|
199
|
-
|
|
200
|
-
"relationships": {
|
|
201
|
-
"description": "Members of the relationships object (\"relationships\") represent references from the resource object in which it's defined to other resource objects.",
|
|
202
|
-
"type": "object",
|
|
203
|
-
"patternProperties": {
|
|
204
|
-
"^\\w[-\\w_]*$": {
|
|
205
|
-
"properties": {
|
|
206
|
-
"links": {
|
|
207
|
-
"$ref": "#/definitions/links"
|
|
208
|
-
},
|
|
209
|
-
"data": {
|
|
210
|
-
"description": "Member, whose value represents \"resource linkage\".",
|
|
211
|
-
"oneOf": [
|
|
212
|
-
{
|
|
213
|
-
"$ref": "#/definitions/relationshipToOne"
|
|
214
|
-
},
|
|
215
|
-
{
|
|
216
|
-
"$ref": "#/definitions/relationshipToMany"
|
|
217
|
-
}
|
|
218
|
-
]
|
|
219
|
-
},
|
|
220
|
-
"meta": {
|
|
221
|
-
"$ref": "#/definitions/meta"
|
|
222
|
-
}
|
|
223
|
-
},
|
|
224
|
-
"additionalProperties": false
|
|
225
|
-
}
|
|
226
|
-
},
|
|
227
|
-
"additionalProperties": false
|
|
228
|
-
},
|
|
229
|
-
"relationshipToOne": {
|
|
230
|
-
"description": "References to other resource objects in a to-one (\"relationship\"). Relationships can be specified by including a member in a resource's links object.",
|
|
231
|
-
"anyOf": [
|
|
232
|
-
{
|
|
233
|
-
"$ref": "#/definitions/empty"
|
|
234
|
-
},
|
|
235
|
-
{
|
|
236
|
-
"$ref": "#/definitions/linkage"
|
|
237
|
-
}
|
|
238
|
-
]
|
|
239
|
-
},
|
|
240
|
-
"relationshipToMany": {
|
|
241
|
-
"description": "An array of objects each containing \"type\" and \"id\" members for to-many relationships.",
|
|
242
|
-
"type": "array",
|
|
243
|
-
"items": {
|
|
244
|
-
"$ref": "#/definitions/linkage"
|
|
245
|
-
},
|
|
246
|
-
"uniqueItems": true
|
|
247
|
-
},
|
|
248
|
-
"empty": {
|
|
249
|
-
"description": "Describes an empty to-one relationship.",
|
|
250
|
-
"type": "null"
|
|
251
|
-
},
|
|
252
|
-
"linkage": {
|
|
253
|
-
"description": "The \"type\" and \"id\" to non-empty members.",
|
|
254
|
-
"type": "object",
|
|
255
|
-
"required": [
|
|
256
|
-
"type",
|
|
257
|
-
"id"
|
|
258
|
-
],
|
|
259
|
-
"properties": {
|
|
260
|
-
"type": {
|
|
261
|
-
"type": "string"
|
|
262
|
-
},
|
|
263
|
-
"id": {
|
|
264
|
-
"type": "string"
|
|
265
|
-
},
|
|
266
|
-
"meta": {
|
|
267
|
-
"$ref": "#/definitions/meta"
|
|
268
|
-
}
|
|
269
|
-
},
|
|
270
|
-
"additionalProperties": false
|
|
271
|
-
},
|
|
272
|
-
"pagination": {
|
|
273
|
-
"type": "object",
|
|
274
|
-
"properties": {
|
|
275
|
-
"first": {
|
|
276
|
-
"description": "The first page of data",
|
|
277
|
-
"oneOf": [
|
|
278
|
-
{ "type": "string", "format": "uri" },
|
|
279
|
-
{ "type": "null" }
|
|
280
|
-
]
|
|
281
|
-
},
|
|
282
|
-
"last": {
|
|
283
|
-
"description": "The last page of data",
|
|
284
|
-
"oneOf": [
|
|
285
|
-
{ "type": "string", "format": "uri" },
|
|
286
|
-
{ "type": "null" }
|
|
287
|
-
]
|
|
288
|
-
},
|
|
289
|
-
"prev": {
|
|
290
|
-
"description": "The previous page of data",
|
|
291
|
-
"oneOf": [
|
|
292
|
-
{ "type": "string", "format": "uri" },
|
|
293
|
-
{ "type": "null" }
|
|
294
|
-
]
|
|
295
|
-
},
|
|
296
|
-
"next": {
|
|
297
|
-
"description": "The next page of data",
|
|
298
|
-
"oneOf": [
|
|
299
|
-
{ "type": "string", "format": "uri" },
|
|
300
|
-
{ "type": "null" }
|
|
301
|
-
]
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
},
|
|
305
|
-
|
|
306
|
-
"jsonapi": {
|
|
307
|
-
"description": "An object describing the server's implementation",
|
|
308
|
-
"type": "object",
|
|
309
|
-
"properties": {
|
|
310
|
-
"version": {
|
|
311
|
-
"type": "string"
|
|
312
|
-
},
|
|
313
|
-
"meta": {
|
|
314
|
-
"$ref": "#/definitions/meta"
|
|
315
|
-
}
|
|
316
|
-
},
|
|
317
|
-
"additionalProperties": false
|
|
318
|
-
},
|
|
319
|
-
|
|
320
|
-
"error": {
|
|
321
|
-
"type": "object",
|
|
322
|
-
"properties": {
|
|
323
|
-
"id": {
|
|
324
|
-
"description": "A unique identifier for this particular occurrence of the problem.",
|
|
325
|
-
"type": "string"
|
|
326
|
-
},
|
|
327
|
-
"links": {
|
|
328
|
-
"$ref": "#/definitions/links"
|
|
329
|
-
},
|
|
330
|
-
"status": {
|
|
331
|
-
"description": "The HTTP status code applicable to this problem, expressed as a string value.",
|
|
332
|
-
"type": "string"
|
|
333
|
-
},
|
|
334
|
-
"code": {
|
|
335
|
-
"description": "An application-specific error code, expressed as a string value.",
|
|
336
|
-
"type": "string"
|
|
337
|
-
},
|
|
338
|
-
"title": {
|
|
339
|
-
"description": "A short, human-readable summary of the problem. It **SHOULD NOT** change from occurrence to occurrence of the problem, except for purposes of localization.",
|
|
340
|
-
"type": "string"
|
|
341
|
-
},
|
|
342
|
-
"detail": {
|
|
343
|
-
"description": "A human-readable explanation specific to this occurrence of the problem.",
|
|
344
|
-
"type": "string"
|
|
345
|
-
},
|
|
346
|
-
"source": {
|
|
347
|
-
"type": "object",
|
|
348
|
-
"properties": {
|
|
349
|
-
"pointer": {
|
|
350
|
-
"description": "A JSON Pointer [RFC6901] to the associated entity in the request document [e.g. \"/data\" for a primary data object, or \"/data/attributes/title\" for a specific attribute].",
|
|
351
|
-
"type": "string"
|
|
352
|
-
},
|
|
353
|
-
"parameter": {
|
|
354
|
-
"description": "A string indicating which query parameter caused the error.",
|
|
355
|
-
"type": "string"
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
},
|
|
359
|
-
"meta": {
|
|
360
|
-
"$ref": "#/definitions/meta"
|
|
361
|
-
}
|
|
362
|
-
},
|
|
363
|
-
"additionalProperties": false
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
}
|