swagger-serializer 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +112 -12
- data/lib/swagger/serializer/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3b95443bcdbd807222b38fe709504b87c73ded014428c3b097e477f798352b1
|
4
|
+
data.tar.gz: 74dc8b92f4b88bbb6e631e099cb2a5ccc0700386836356e6b222b21d0f247a80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66c21502bb24dafdf67de2b0c8d29204f25c5269fd0025ad0385f6cd04588363edd87d51a08e4c5460ff271bb63dc85a059dd29cc0a30f0d0d3f1e16fc5118d3
|
7
|
+
data.tar.gz: 7629f949146f38f9065b6401aecd7953b0086bfa4544b1d29b90c1400fd2e7746f81439109f92520c41da7771e83e8db46f28cff6ebc52a08500dc41f7eafe92
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -23,13 +23,123 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
-
### Rails
|
26
|
+
### Rails with swagger-dsl
|
27
|
+
|
28
|
+
use "[swagger-dsl](https://github.com/Narazaka/swagger-dsl)"!
|
29
|
+
|
30
|
+
Load it in initializer.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# config/initializers/swagger_serializer.rb
|
34
|
+
|
35
|
+
if Rails.application.config.eager_load
|
36
|
+
Rails.application.config.after_initialize do
|
37
|
+
Swagger::Schema.current = Swagger::Schema.new(Swagger::DSL.current.resolved)
|
38
|
+
end
|
39
|
+
else
|
40
|
+
Swagger::Schema.current = Swagger::Schema.new(Swagger::DSL.current)
|
41
|
+
Swagger::Serializer::Store.current.options[:resolver] = Swagger::DSL.current.resolver
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Use it in controllers.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# app/controllers/application_controller.rb
|
49
|
+
class ApplicationController < ActionController::Base
|
50
|
+
include Swagger::Serializer::RailsController
|
51
|
+
|
52
|
+
def render_ok(data)
|
53
|
+
render_as_schema 200, :json, data
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# app/controllers/users_controller.rb
|
60
|
+
class UsersController < ApplicationController
|
61
|
+
swagger :index do
|
62
|
+
render 200 do
|
63
|
+
array! { cref! UserSerializer }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def index
|
68
|
+
render_ok User.all
|
69
|
+
end
|
70
|
+
|
71
|
+
swagger :show do
|
72
|
+
params do
|
73
|
+
path :id, schema: :integer, required: true
|
74
|
+
end
|
75
|
+
|
76
|
+
render 200 do
|
77
|
+
cref! UserSerializer
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def show
|
82
|
+
render_ok User.find(params[:id])
|
83
|
+
end
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
Would you want to customize serialization?
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# app/serializers/base_serializer.rb
|
91
|
+
class BaseSerializer
|
92
|
+
include Swagger::Serializer
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
# app/serializers/user_serializer.rb
|
98
|
+
class UserSerializer < BaseSerializer
|
99
|
+
swagger do
|
100
|
+
id :integer
|
101
|
+
name :string, optional: true
|
102
|
+
end
|
103
|
+
|
104
|
+
def name
|
105
|
+
"#{@model.name}!!!!"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
Now you can get `{ "id" => 42, "name" => "me!!!!" }`.
|
111
|
+
|
112
|
+
This serializer class detection uses the schema's `title` key.
|
113
|
+
If you want to use `Foo::BarSerializer`, set `Foo::Bar` to `title` key.
|
114
|
+
|
115
|
+
The key is configurable by
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
# in config/initializers/swagger_serializer.rb
|
119
|
+
Swagger::Serializer::Store.current.options[:inject_key] = "my_inject_key"
|
120
|
+
Swagger::DSL.current.config.inject_key = "my_inject_key"
|
121
|
+
```
|
122
|
+
|
123
|
+
Sometimes model needs direct serialize.
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
# app/models/application_record.rb
|
127
|
+
class ApplicationRecord < ActiveRecord::Base
|
128
|
+
self.abstract_class = true
|
129
|
+
|
130
|
+
include Swagger::Serializer::Model
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
Now you can get serialized result by `p User.first.serialize`.
|
135
|
+
|
136
|
+
### Rails with raw schema
|
27
137
|
|
28
138
|
Write your OpenAPI spec.
|
29
139
|
|
30
140
|
```yaml
|
31
141
|
# swagger.yml
|
32
|
-
openapi:
|
142
|
+
openapi: 3.0.0
|
33
143
|
info:
|
34
144
|
title: example api
|
35
145
|
version: 0.1.0
|
@@ -71,16 +181,6 @@ Load it in initializer.
|
|
71
181
|
Swagger::Schema.load_file_to_current(
|
72
182
|
__dir__ + "/../../swagger.yml",
|
73
183
|
)
|
74
|
-
|
75
|
-
# Swagger::Schema accepts lazy load. So your swagger schema data instance may also be accepted.
|
76
|
-
#
|
77
|
-
# class SwaggerData
|
78
|
-
# def paths
|
79
|
-
# load_paths_now
|
80
|
-
# end
|
81
|
-
# end
|
82
|
-
#
|
83
|
-
# Swagger::Schema.current = Swagger::Schema.new(SwaggerData.new)
|
84
184
|
```
|
85
185
|
|
86
186
|
Use it in controllers.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swagger-serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Narazaka
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema-serializer
|
@@ -234,7 +234,7 @@ metadata:
|
|
234
234
|
homepage_uri: https://github.com/Narazaka/swagger-serializer
|
235
235
|
source_code_uri: https://github.com/Narazaka/swagger-serializer.git
|
236
236
|
changelog_uri: https://github.com/Narazaka/swagger-serializer/blob/master/CHANGELOG.md
|
237
|
-
documentation_uri: https://www.rubydoc.info/gems/swagger-serializer/0.4.
|
237
|
+
documentation_uri: https://www.rubydoc.info/gems/swagger-serializer/0.4.1
|
238
238
|
post_install_message:
|
239
239
|
rdoc_options: []
|
240
240
|
require_paths:
|