media_types-serialization 1.0.1 → 1.2.0
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 +4 -4
- data/.github/workflows/ci.yml +7 -3
- data/.prettierrc +1 -0
- data/CHANGELOG.md +31 -7
- data/CODE_OF_CONDUCT.md +11 -11
- data/Gemfile.lock +64 -70
- data/README.md +98 -53
- data/lib/media_types/serialization.rb +84 -11
- data/lib/media_types/serialization/base.rb +8 -4
- data/lib/media_types/serialization/error.rb +11 -3
- data/lib/media_types/serialization/fake_validator.rb +1 -1
- data/lib/media_types/serialization/serialization_registration.rb +19 -9
- data/lib/media_types/serialization/serializers/problem_serializer.rb +23 -10
- data/lib/media_types/serialization/utils/accept_header.rb +77 -0
- data/lib/media_types/serialization/utils/accept_language_header.rb +82 -0
- data/lib/media_types/serialization/utils/header_list.rb +89 -0
- data/lib/media_types/serialization/version.rb +1 -1
- data/media_types-serialization.gemspec +3 -5
- metadata +14 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c72aa611b03ba3537f1c12eb44b31596f58d21ba3fe0dd5596b8d37d613d132
|
4
|
+
data.tar.gz: '084a4733e9d015fe1816399772d3fcfaed898368507d9785df89bdb296b34171'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67d43c2377d4ac849997886fa547d9b6c22ee7880eca6e1acfebd73db3cbe18a189823ca79bec0a009ab2feb83aa6b7d2d4a7e50ac44e9e0f37207fe4c07bb87
|
7
|
+
data.tar.gz: 2d9825fca6e6d77f84609b691d5dcd113424b41adf971efe87fe7bcd56220dd34a6aaec6a7ce905474131ed398e5fed8decf11f4e2209752b3a9cfbfbd80933f
|
data/.github/workflows/ci.yml
CHANGED
@@ -5,7 +5,7 @@ on:
|
|
5
5
|
branches:
|
6
6
|
- master
|
7
7
|
push:
|
8
|
-
branches:
|
8
|
+
branches:
|
9
9
|
- master
|
10
10
|
- depfu/*
|
11
11
|
- release/*
|
@@ -16,12 +16,16 @@ jobs:
|
|
16
16
|
|
17
17
|
runs-on: ubuntu-latest
|
18
18
|
|
19
|
+
strategy:
|
20
|
+
matrix:
|
21
|
+
ruby-version: [2.7.x, 2.6.x]
|
22
|
+
|
19
23
|
steps:
|
20
24
|
- uses: actions/checkout@v1
|
21
|
-
- name: Set up Ruby
|
25
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
22
26
|
uses: actions/setup-ruby@v1
|
23
27
|
with:
|
24
|
-
ruby-version:
|
28
|
+
ruby-version: ${{ matrix.ruby-version }}
|
25
29
|
- name: Build and test with Rake
|
26
30
|
run: |
|
27
31
|
gem install bundler
|
data/.prettierrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{}
|
data/CHANGELOG.md
CHANGED
@@ -1,19 +1,43 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.2.0
|
4
|
+
|
5
|
+
- ✨ Add `view:` to `output_html` which renders a specific rails view.
|
6
|
+
|
7
|
+
## 1.1.0
|
8
|
+
|
9
|
+
- ✨ Add _allow_output_html_: Fallback to rails rendering.
|
10
|
+
- ✨ Add _allow_output_docs_: Useful to add a documentation description to endpoints that you can normally only POST to.
|
11
|
+
- ✨ Add _output_error_: Implements missing content-language support.
|
12
|
+
- ✨ Add _scoped freeze_io! support_: Useful for gradual adoption of mediatypes on existing routes.
|
13
|
+
- ✨ Add _alias variant reporting_: Allows reporting what the original matched media type was even when impersonating a different media type.
|
14
|
+
- ✨ Improve README: small improvements to make it easier to adopt and upgrade existing codebase.
|
15
|
+
- ✨ Reduce number of (external) dependencies
|
16
|
+
- 🐛 Fix incorrect output on encoding errors.
|
17
|
+
- 🐛 Fix message in various alias error messages.
|
18
|
+
|
19
|
+
## 1.0.3
|
20
|
+
|
21
|
+
- 🐛 Unvalidated serializers would put the view part of the identifier before the version. This was not in line with validated serializers.
|
22
|
+
|
23
|
+
## 1.0.2
|
24
|
+
|
25
|
+
- 🐛 Explicitly set all oj parameters when decoding as well.
|
26
|
+
|
3
27
|
## 1.0.1
|
4
28
|
|
5
|
-
|
6
|
-
|
29
|
+
- 🐛 Explicitly set all oj and json parameters to ensure correct behavior with changed defaults.
|
30
|
+
- 🐛 Fix serializer not deserializing as symbols.
|
7
31
|
|
8
32
|
## 1.0.0
|
9
33
|
|
10
34
|
- ✨ Add support for input deserialization.
|
11
|
-
- ✨
|
12
|
-
- ✨
|
13
|
-
- ✨
|
14
|
-
- ✨
|
35
|
+
- ✨ Add serializer DSL to be more in line with validation gem.
|
36
|
+
- ✨ Add ability to make a serializer without a validator.
|
37
|
+
- ✨ Add error serializer that emits [`application/problem+json`](https://tools.ietf.org/html/rfc7231).
|
38
|
+
- ✨ Reduce number of dependencies.
|
15
39
|
- ✨ Validators no longer need to be registered to be used.
|
16
|
-
- ✨
|
40
|
+
- ✨ Add a [wiki where errors can be documented](https://docs.delftsolutions.nl). Feel free to make pages for your own namespaced errors.
|
17
41
|
- 💔 Serializer definition API has backwards incompatible changes.
|
18
42
|
- 💔 API viewer is now no longer registered as html but accessible with the `?api_viewer=last` query parameter.
|
19
43
|
- 💔 Validators can no longer be registered for use in `format do`.
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -14,21 +14,21 @@ orientation.
|
|
14
14
|
Examples of behavior that contributes to creating a positive environment
|
15
15
|
include:
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
- Using welcoming and inclusive language
|
18
|
+
- Being respectful of differing viewpoints and experiences
|
19
|
+
- Gracefully accepting constructive criticism
|
20
|
+
- Focusing on what is best for the community
|
21
|
+
- Showing empathy towards other community members
|
22
22
|
|
23
23
|
Examples of unacceptable behavior by participants include:
|
24
24
|
|
25
|
-
|
26
|
-
advances
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
- The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
- Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
- Public or private harassment
|
29
|
+
- Publishing others' private information, such as a physical or electronic
|
30
30
|
address, without explicit permission
|
31
|
-
|
31
|
+
- Other conduct which could reasonably be considered inappropriate in a
|
32
32
|
professional setting
|
33
33
|
|
34
34
|
## Our Responsibilities
|
data/Gemfile.lock
CHANGED
@@ -1,134 +1,128 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
media_types-serialization (1.0
|
4
|
+
media_types-serialization (1.1.0)
|
5
5
|
actionpack (>= 4.0.0)
|
6
6
|
activesupport (>= 4.0.0)
|
7
|
-
http_headers-accept (>= 0.2.2, < 1.0.0)
|
8
|
-
http_headers-link (< 1.0.0)
|
9
7
|
media_types (>= 2.0.0, < 3.0.0)
|
10
8
|
|
11
9
|
GEM
|
12
10
|
remote: https://rubygems.org/
|
13
11
|
specs:
|
14
|
-
actioncable (5.2.
|
15
|
-
actionpack (= 5.2.
|
12
|
+
actioncable (5.2.6)
|
13
|
+
actionpack (= 5.2.6)
|
16
14
|
nio4r (~> 2.0)
|
17
15
|
websocket-driver (>= 0.6.1)
|
18
|
-
actionmailer (5.2.
|
19
|
-
actionpack (= 5.2.
|
20
|
-
actionview (= 5.2.
|
21
|
-
activejob (= 5.2.
|
16
|
+
actionmailer (5.2.6)
|
17
|
+
actionpack (= 5.2.6)
|
18
|
+
actionview (= 5.2.6)
|
19
|
+
activejob (= 5.2.6)
|
22
20
|
mail (~> 2.5, >= 2.5.4)
|
23
21
|
rails-dom-testing (~> 2.0)
|
24
|
-
actionpack (5.2.
|
25
|
-
actionview (= 5.2.
|
26
|
-
activesupport (= 5.2.
|
22
|
+
actionpack (5.2.6)
|
23
|
+
actionview (= 5.2.6)
|
24
|
+
activesupport (= 5.2.6)
|
27
25
|
rack (~> 2.0, >= 2.0.8)
|
28
26
|
rack-test (>= 0.6.3)
|
29
27
|
rails-dom-testing (~> 2.0)
|
30
28
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
31
|
-
actionview (5.2.
|
32
|
-
activesupport (= 5.2.
|
29
|
+
actionview (5.2.6)
|
30
|
+
activesupport (= 5.2.6)
|
33
31
|
builder (~> 3.1)
|
34
32
|
erubi (~> 1.4)
|
35
33
|
rails-dom-testing (~> 2.0)
|
36
34
|
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
37
|
-
activejob (5.2.
|
38
|
-
activesupport (= 5.2.
|
35
|
+
activejob (5.2.6)
|
36
|
+
activesupport (= 5.2.6)
|
39
37
|
globalid (>= 0.3.6)
|
40
|
-
activemodel (5.2.
|
41
|
-
activesupport (= 5.2.
|
42
|
-
activerecord (5.2.
|
43
|
-
activemodel (= 5.2.
|
44
|
-
activesupport (= 5.2.
|
38
|
+
activemodel (5.2.6)
|
39
|
+
activesupport (= 5.2.6)
|
40
|
+
activerecord (5.2.6)
|
41
|
+
activemodel (= 5.2.6)
|
42
|
+
activesupport (= 5.2.6)
|
45
43
|
arel (>= 9.0)
|
46
|
-
activestorage (5.2.
|
47
|
-
actionpack (= 5.2.
|
48
|
-
activerecord (= 5.2.
|
49
|
-
marcel (~> 0.
|
50
|
-
activesupport (5.2.
|
44
|
+
activestorage (5.2.6)
|
45
|
+
actionpack (= 5.2.6)
|
46
|
+
activerecord (= 5.2.6)
|
47
|
+
marcel (~> 1.0.0)
|
48
|
+
activesupport (5.2.6)
|
51
49
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
52
50
|
i18n (>= 0.7, < 2)
|
53
51
|
minitest (~> 5.1)
|
54
52
|
tzinfo (~> 1.1)
|
55
53
|
arel (9.0.0)
|
56
|
-
awesome_print (1.
|
54
|
+
awesome_print (1.9.2)
|
57
55
|
builder (3.2.4)
|
58
|
-
concurrent-ruby (1.1.
|
56
|
+
concurrent-ruby (1.1.9)
|
59
57
|
crass (1.0.6)
|
60
|
-
erubi (1.
|
58
|
+
erubi (1.10.0)
|
61
59
|
globalid (0.4.2)
|
62
60
|
activesupport (>= 4.2.0)
|
63
|
-
|
64
|
-
http_headers-utils (>= 0.2.0, < 1.0.0)
|
65
|
-
http_headers-link (0.2.1)
|
66
|
-
http_headers-utils (>= 0.2.0, < 1.0.0)
|
67
|
-
http_headers-utils (0.2.0)
|
68
|
-
i18n (1.8.2)
|
61
|
+
i18n (1.8.10)
|
69
62
|
concurrent-ruby (~> 1.0)
|
70
|
-
loofah (2.
|
63
|
+
loofah (2.10.0)
|
71
64
|
crass (~> 1.0.2)
|
72
65
|
nokogiri (>= 1.5.9)
|
73
66
|
mail (2.7.1)
|
74
67
|
mini_mime (>= 0.1.1)
|
75
|
-
marcel (0.
|
76
|
-
mimemagic (~> 0.3.2)
|
68
|
+
marcel (1.0.1)
|
77
69
|
media_types (2.0.1)
|
78
70
|
method_source (1.0.0)
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
nokogiri (1.
|
85
|
-
|
86
|
-
oj (3.
|
87
|
-
|
71
|
+
mini_mime (1.1.0)
|
72
|
+
minitest (5.14.4)
|
73
|
+
nio4r (2.5.7)
|
74
|
+
nokogiri (1.11.7-x64-mingw32)
|
75
|
+
racc (~> 1.4)
|
76
|
+
nokogiri (1.11.7-x86_64-linux)
|
77
|
+
racc (~> 1.4)
|
78
|
+
oj (3.12.1)
|
79
|
+
racc (1.5.2)
|
80
|
+
rack (2.2.3)
|
88
81
|
rack-test (1.1.0)
|
89
82
|
rack (>= 1.0, < 3)
|
90
|
-
rails (5.2.
|
91
|
-
actioncable (= 5.2.
|
92
|
-
actionmailer (= 5.2.
|
93
|
-
actionpack (= 5.2.
|
94
|
-
actionview (= 5.2.
|
95
|
-
activejob (= 5.2.
|
96
|
-
activemodel (= 5.2.
|
97
|
-
activerecord (= 5.2.
|
98
|
-
activestorage (= 5.2.
|
99
|
-
activesupport (= 5.2.
|
83
|
+
rails (5.2.6)
|
84
|
+
actioncable (= 5.2.6)
|
85
|
+
actionmailer (= 5.2.6)
|
86
|
+
actionpack (= 5.2.6)
|
87
|
+
actionview (= 5.2.6)
|
88
|
+
activejob (= 5.2.6)
|
89
|
+
activemodel (= 5.2.6)
|
90
|
+
activerecord (= 5.2.6)
|
91
|
+
activestorage (= 5.2.6)
|
92
|
+
activesupport (= 5.2.6)
|
100
93
|
bundler (>= 1.3.0)
|
101
|
-
railties (= 5.2.
|
94
|
+
railties (= 5.2.6)
|
102
95
|
sprockets-rails (>= 2.0.0)
|
103
96
|
rails-dom-testing (2.0.3)
|
104
97
|
activesupport (>= 4.2.0)
|
105
98
|
nokogiri (>= 1.6)
|
106
99
|
rails-html-sanitizer (1.3.0)
|
107
100
|
loofah (~> 2.3)
|
108
|
-
railties (5.2.
|
109
|
-
actionpack (= 5.2.
|
110
|
-
activesupport (= 5.2.
|
101
|
+
railties (5.2.6)
|
102
|
+
actionpack (= 5.2.6)
|
103
|
+
activesupport (= 5.2.6)
|
111
104
|
method_source
|
112
105
|
rake (>= 0.8.7)
|
113
106
|
thor (>= 0.19.0, < 2.0)
|
114
|
-
rake (13.0.
|
115
|
-
sprockets (4.0.
|
107
|
+
rake (13.0.6)
|
108
|
+
sprockets (4.0.2)
|
116
109
|
concurrent-ruby (~> 1.0)
|
117
110
|
rack (> 1, < 3)
|
118
|
-
sprockets-rails (3.2.
|
111
|
+
sprockets-rails (3.2.2)
|
119
112
|
actionpack (>= 4.0)
|
120
113
|
activesupport (>= 4.0)
|
121
114
|
sprockets (>= 3.0.0)
|
122
|
-
thor (1.0
|
115
|
+
thor (1.1.0)
|
123
116
|
thread_safe (0.3.6)
|
124
|
-
tzinfo (1.2.
|
117
|
+
tzinfo (1.2.9)
|
125
118
|
thread_safe (~> 0.1)
|
126
|
-
websocket-driver (0.7.
|
119
|
+
websocket-driver (0.7.5)
|
127
120
|
websocket-extensions (>= 0.1.0)
|
128
|
-
websocket-extensions (0.1.
|
121
|
+
websocket-extensions (0.1.5)
|
129
122
|
|
130
123
|
PLATFORMS
|
131
|
-
|
124
|
+
x64-mingw32
|
125
|
+
x86_64-linux
|
132
126
|
|
133
127
|
DEPENDENCIES
|
134
128
|
awesome_print
|
@@ -140,4 +134,4 @@ DEPENDENCIES
|
|
140
134
|
rake (~> 13.0)
|
141
135
|
|
142
136
|
BUNDLED WITH
|
143
|
-
|
137
|
+
2.2.7
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
[](https://badge.fury.io/rb/media_types-serialization)
|
5
5
|
[](http://opensource.org/licenses/MIT)
|
6
6
|
|
7
|
-
`respond_to` on steroids. Add
|
7
|
+
`respond_to` on steroids. Add [HATEOAS](https://docs.delftsolutions.nl/wiki/HATEOAS_API) compatible serialization and deserialization to your Rails projects.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -57,6 +57,60 @@ BookSerializer.serialize(book, 'vnd.acme.book.v1+json', context: nil)
|
|
57
57
|
# => { "book": { "title": "Everything, abridged" } }
|
58
58
|
```
|
59
59
|
|
60
|
+
### Controller integration
|
61
|
+
|
62
|
+
You can integrate the serialization system in rails, giving you automatic [Content-Type negotiation](https://en.wikipedia.org/wiki/Content_negotiation) using the `Accept` header:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
require 'media_types/serialization'
|
66
|
+
|
67
|
+
class BookController < ActionController::API
|
68
|
+
include MediaTypes::Serialization
|
69
|
+
|
70
|
+
allow_output_serializer(BookSerializer, only: %i[show])
|
71
|
+
freeze_io!
|
72
|
+
|
73
|
+
def show
|
74
|
+
book = Book.new
|
75
|
+
book.title = 'Everything, abridged'
|
76
|
+
|
77
|
+
render_media book
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
While using the controller integration the context will always be set to the current controller. This allows you to construct urls.
|
83
|
+
|
84
|
+
### Adding HATEOAS responses to existing routes
|
85
|
+
|
86
|
+
When creating a mobile application it's often useful to allow the app to request a non-html representation of a specific url. If you have an existing route:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
class BookController < ApplicationController
|
90
|
+
def show
|
91
|
+
@book = Book.new
|
92
|
+
|
93
|
+
# Use view corresponding to the controller
|
94
|
+
end
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
You can add a json representation as follows:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class BookController < ApplicationController
|
102
|
+
allow_output_serializer(BookSerializer, only: %i[show])
|
103
|
+
allow_output_html
|
104
|
+
freeze_io!
|
105
|
+
|
106
|
+
def show
|
107
|
+
@book = Book.new
|
108
|
+
|
109
|
+
render_media @book
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
60
114
|
### Validations
|
61
115
|
|
62
116
|
Right now the serializer does not validate incoming or outgoing information. This can cause issues when you accidentally emit non-conforming data that people start to depend on. To make sure you don't do that you can specify a [Media Type validator](https://github.com/SleeplessByte/media-types-ruby):
|
@@ -98,30 +152,6 @@ end
|
|
98
152
|
|
99
153
|
For more information, see the [Media Types docs](https://github.com/SleeplessByte/media-types-ruby).
|
100
154
|
|
101
|
-
### Controller integration
|
102
|
-
|
103
|
-
You can integrate the serialization system in rails, giving you automatic [Content-Type negotiation](https://en.wikipedia.org/wiki/Content_negotiation) using the `Accept` header:
|
104
|
-
|
105
|
-
```ruby
|
106
|
-
require 'media_types/serialization'
|
107
|
-
|
108
|
-
class BookController < ActionController::API
|
109
|
-
include MediaTypes::Serialization
|
110
|
-
|
111
|
-
allow_output_serializer(BookSerializer, only: %i[show])
|
112
|
-
freeze_io!
|
113
|
-
|
114
|
-
def show
|
115
|
-
book = Book.new
|
116
|
-
book.title = 'Everything, abridged'
|
117
|
-
|
118
|
-
render_media book
|
119
|
-
end
|
120
|
-
end
|
121
|
-
```
|
122
|
-
|
123
|
-
While using the controller integration the context will always be set to the current controller. This allows you to construct urls.
|
124
|
-
|
125
155
|
### Versioning
|
126
156
|
|
127
157
|
To help with supporting older versions, serializers have a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) to construct json objects:
|
@@ -149,7 +179,7 @@ BookSerializer.serialize(book, BookValidator.version(2), context: nil)
|
|
149
179
|
|
150
180
|
### Links
|
151
181
|
|
152
|
-
When making [HATEOAS](https://
|
182
|
+
When making [HATEOAS](https://docs.delftsolutions.nl/wiki/HATEOAS_API) compliant applications it's very useful to include `Link` headers in your response so clients can use a `HEAD` request instead of having to fetch the entire resource. Serializers have convenience methods to help with this:
|
153
183
|
|
154
184
|
```ruby
|
155
185
|
class BookSerializer < MediaTypes::Serialization::Base
|
@@ -206,7 +236,7 @@ class BookSerializer < MediaTypes::Serialization::Base
|
|
206
236
|
output view: :index, version: 3 do |arr, version, context|
|
207
237
|
attribute :books do
|
208
238
|
link :self, href: context.book_index_url
|
209
|
-
|
239
|
+
|
210
240
|
index arr, version: version
|
211
241
|
end
|
212
242
|
end
|
@@ -248,15 +278,15 @@ class BookSerializer < MediaTypes::Serialization::Base
|
|
248
278
|
output view: :index, version: 3 do |arr, version, context|
|
249
279
|
attribute :books do
|
250
280
|
link :self, href: context.book_index_url
|
251
|
-
|
281
|
+
|
252
282
|
index arr, version: version
|
253
283
|
end
|
254
284
|
end
|
255
|
-
|
285
|
+
|
256
286
|
output view: :collection, version: 3 do |arr, version, context|
|
257
287
|
attribute :books do
|
258
288
|
link :self, href: context.book_collection_url
|
259
|
-
|
289
|
+
|
260
290
|
collection arr, version: version
|
261
291
|
end
|
262
292
|
end
|
@@ -309,8 +339,8 @@ class BookController < ActionController::API
|
|
309
339
|
allow_output_serializer(BookSerializer, only: %i[show])
|
310
340
|
allow_input_serializer(BookSerializer, only: %i[create])
|
311
341
|
freeze_io!
|
312
|
-
|
313
|
-
def show
|
342
|
+
|
343
|
+
def show
|
314
344
|
book = Book.new
|
315
345
|
book.title = 'Everything, abridged'
|
316
346
|
|
@@ -318,7 +348,7 @@ class BookController < ActionController::API
|
|
318
348
|
end
|
319
349
|
|
320
350
|
def create
|
321
|
-
json = deserialize(request
|
351
|
+
json = deserialize(request) # does validation for us
|
322
352
|
puts json
|
323
353
|
end
|
324
354
|
end
|
@@ -355,8 +385,8 @@ class BookController < ActionController::API
|
|
355
385
|
allow_output_serializer(BookSerializer, only: %i[show])
|
356
386
|
allow_input_serializer(BookSerializer, only: %i[create])
|
357
387
|
freeze_io!
|
358
|
-
|
359
|
-
def show
|
388
|
+
|
389
|
+
def show
|
360
390
|
book = Book.new
|
361
391
|
book.title = 'Everything, abridged'
|
362
392
|
|
@@ -364,7 +394,7 @@ class BookController < ActionController::API
|
|
364
394
|
end
|
365
395
|
|
366
396
|
def create
|
367
|
-
book = deserialize(request
|
397
|
+
book = deserialize(request)
|
368
398
|
book.save!
|
369
399
|
|
370
400
|
render media: serialize_media(book), content_type request.format.to_s
|
@@ -385,7 +415,7 @@ class BookSerializer < MediaTypes::Serialization::Base
|
|
385
415
|
output_raw view: :raw, version: 3 do |obj, version, context|
|
386
416
|
hidden do
|
387
417
|
# Make sure links are only set in the headers, not in the body.
|
388
|
-
|
418
|
+
|
389
419
|
link :self, href: context.book_url(obj)
|
390
420
|
end
|
391
421
|
|
@@ -454,14 +484,12 @@ class BookController < ActionController::API
|
|
454
484
|
include MediaTypes::Serialization
|
455
485
|
|
456
486
|
allow_api_viewer
|
457
|
-
|
458
|
-
allow_output_serializer(MediaTypes::ApiViewer)
|
459
487
|
|
460
488
|
allow_output_serializer(BookSerializer, only: %i[show])
|
461
489
|
allow_input_serializer(BookSerializer, only: %i[create])
|
462
490
|
freeze_io!
|
463
|
-
|
464
|
-
def show
|
491
|
+
|
492
|
+
def show
|
465
493
|
book = Book.new
|
466
494
|
book.title = 'Everything, abridged'
|
467
495
|
|
@@ -469,7 +497,7 @@ class BookController < ActionController::API
|
|
469
497
|
end
|
470
498
|
|
471
499
|
def create
|
472
|
-
json = deserialize(request
|
500
|
+
json = deserialize(request) # does validation for us
|
473
501
|
puts json
|
474
502
|
end
|
475
503
|
end
|
@@ -489,14 +517,14 @@ class BookSerializer < MediaTypes::Serialization::Base
|
|
489
517
|
attribute :description, obj.description if version >= 2
|
490
518
|
end
|
491
519
|
end
|
492
|
-
|
520
|
+
|
493
521
|
output_raw view: :html do |obj, context|
|
494
522
|
render_view 'book/show', context: context, assigns: {
|
495
523
|
title: obj.title,
|
496
524
|
description: obj.description
|
497
525
|
}
|
498
526
|
end
|
499
|
-
|
527
|
+
|
500
528
|
output_alias 'text/html', view: :html
|
501
529
|
end
|
502
530
|
```
|
@@ -518,11 +546,11 @@ class BookController < ActionController::API
|
|
518
546
|
|
519
547
|
freeze_io!
|
520
548
|
|
521
|
-
# ...
|
549
|
+
# ...
|
522
550
|
end
|
523
551
|
```
|
524
552
|
|
525
|
-
The exception you specified will be rescued by the controller and will be displayed to the user along with a link to the shared wiki page for that error type. Feel free to add instructions there on how clients should solve this problem. You can find more information at:
|
553
|
+
The exception you specified will be rescued by the controller and will be displayed to the user along with a link to the shared wiki page for that error type. Feel free to add instructions there on how clients should solve this problem. You can find more information at: [https://docs.delftsolutions.nl/wiki/Error](https://docs.delftsolutions.nl/wiki/Error)
|
526
554
|
If you want to override this url you can use the `p.url(href)` function.
|
527
555
|
|
528
556
|
By default the `message` property of the error is used to fill the `details` field. You can override this by using the `p.override_details(description, lang:)` function.
|
@@ -531,9 +559,7 @@ Custom attributes can be added using the `p.attribute(name, value)` function.
|
|
531
559
|
|
532
560
|
### Related
|
533
561
|
|
534
|
-
- [`MediaTypes`](https://github.com/SleeplessByte/media-types-ruby): :gem: Library to create media type
|
535
|
-
- [`MediaTypes::Deserialization`](https://github.com/XPBytes/media_types-deserialization): :cyclone: Add media types supported deserialization using your favourite parser, and media type validation.
|
536
|
-
- [`MediaTypes::Validation`](https://github.com/XPBytes/media_types-validation): :heavy_exclamation_mark: Response validations according to a media-type
|
562
|
+
- [`MediaTypes`](https://github.com/SleeplessByte/media-types-ruby): :gem: Library to create media type validators.
|
537
563
|
|
538
564
|
## API
|
539
565
|
|
@@ -565,14 +591,18 @@ The block should return an object to convert into JSON.
|
|
565
591
|
|
566
592
|
This has the same behavior as `output` but should return a string instead of an object. Output is not validated.
|
567
593
|
|
568
|
-
#### `output_alias( media_type_identifier, view: )`
|
594
|
+
#### `output_alias( media_type_identifier, view:, hide_variant: false )`
|
595
|
+
|
596
|
+
Defines a legacy mapping. This will make the deserializer parse the media type `media_type_identifier` as if it was version `nil` of the specified view. If view is undefined it will use the output serializer without a view defined.
|
569
597
|
|
570
|
-
|
598
|
+
Response will have a content type equal to `[media_type_identifier]; variant=[mapped_media_type_identifier]`. If `hide_variant:` is true, the content type emitted will only be `[media_type_identifier]`.
|
571
599
|
|
572
|
-
#### `output_alias_optional( media_type_identifier, view: )`
|
600
|
+
#### `output_alias_optional( media_type_identifier, view:, hide_variant: false )`
|
573
601
|
|
574
602
|
Has the same behavior as `output_alias` but can be used by multiple serializers. The serializer that is loaded last in the controller 'wins' control over this media type identifier. If any of the serializers have an `output_alias` defined with the same media type identifier that one will win instead.
|
575
603
|
|
604
|
+
Response will have a content type equal to `[media_type_identifier]; variant=[mapped_media_type_identifier]`. If `hide_variant:` is true, the content type emitted will only be `[media_type_identifier]`.
|
605
|
+
|
576
606
|
#### `input( view:, version:, versions: ) do |obj, version, context|`
|
577
607
|
|
578
608
|
Defines a deserialization block. Either version or versions can be set. View should be a symbol or unset.
|
@@ -658,6 +688,20 @@ Configure the controller to allow the client to request responses emitted by the
|
|
658
688
|
|
659
689
|
Accepts the same filters as `before_action`.
|
660
690
|
|
691
|
+
#### `allow_output_html( as: nil, view: nil, layout: nil, **filters )`
|
692
|
+
|
693
|
+
Allows falling back to the default Rails view rendering when the client asks for the media type in the `as:` parameter or `text/html` if `as:` is unset.
|
694
|
+
|
695
|
+
The `Content-Type` of the response will be `text/html` if the `as:` parameter is unset. If the `as:` parameter is set, it will include it in the variant parameter: `text/html; variant=application/vnd.xpbytes.borderless`.
|
696
|
+
|
697
|
+
Accepts the same filters as `before_action`. You can set the template to use using the `view:` parameter.
|
698
|
+
|
699
|
+
#### `allow_output_docs( description, **filters )`
|
700
|
+
|
701
|
+
Outputs the specified description as help information.
|
702
|
+
|
703
|
+
Accepts the same filters as `before_action`.
|
704
|
+
|
661
705
|
#### `allow_input_serializer( serializer, views: nil, **filters )`
|
662
706
|
|
663
707
|
Configure the controller to allow the client to send bodies with a `Content-Type` that can be deserialized using the specified serializer. Optionally allows you to specify which views to allow by passing an array in the views parameter.
|
@@ -692,7 +736,7 @@ Clears the list of serializers used to render the error when the client supplies
|
|
692
736
|
|
693
737
|
Enables rendering the api viewer when adding the `api_viewer=last` query parameter to the url.
|
694
738
|
|
695
|
-
#### `freeze_io
|
739
|
+
#### `freeze_io!(**filter_opts)`
|
696
740
|
|
697
741
|
Registers serialization and deserialization in the controller. This function must be called before using the controller.
|
698
742
|
|
@@ -742,6 +786,7 @@ Does the same as `deserialize( request )` but gives the client an error page if
|
|
742
786
|
Returns the serializer class that will handle the given request.
|
743
787
|
|
744
788
|
## Customization
|
789
|
+
|
745
790
|
The easiest way to customize the look and feel of the built in pages is to provide your own logo and background in an initializer:
|
746
791
|
|
747
792
|
```ruby
|