swagger-serializer 0.1.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 +7 -0
- data/.gitignore +12 -0
- data/.prettierrc +3 -0
- data/.rspec +4 -0
- data/.rubocop.yml +25 -0
- data/.rubocop_airbnb.yml +2 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +6 -0
- data/LICENSE +20 -0
- data/README.md +162 -0
- data/Rakefile +9 -0
- data/bin/console +10 -0
- data/bin/fmt +9 -0
- data/bin/setup +8 -0
- data/lib/swagger/schema/component.rb +17 -0
- data/lib/swagger/schema/content.rb +25 -0
- data/lib/swagger/schema/handle_servers.rb +20 -0
- data/lib/swagger/schema/header.rb +7 -0
- data/lib/swagger/schema/headers.rb +37 -0
- data/lib/swagger/schema/media_type/deserializer.rb +12 -0
- data/lib/swagger/schema/media_type/validator.rb +18 -0
- data/lib/swagger/schema/media_type.rb +30 -0
- data/lib/swagger/schema/operation.rb +35 -0
- data/lib/swagger/schema/parameter.rb +15 -0
- data/lib/swagger/schema/parameter_base.rb +31 -0
- data/lib/swagger/schema/parameters/deserializer.rb +33 -0
- data/lib/swagger/schema/parameters/validator.rb +18 -0
- data/lib/swagger/schema/parameters.rb +73 -0
- data/lib/swagger/schema/path_item.rb +33 -0
- data/lib/swagger/schema/request_body.rb +21 -0
- data/lib/swagger/schema/response.rb +31 -0
- data/lib/swagger/schema/responses.rb +16 -0
- data/lib/swagger/schema/schema_accessor.rb +23 -0
- data/lib/swagger/schema/server.rb +25 -0
- data/lib/swagger/schema/util.rb +23 -0
- data/lib/swagger/schema.rb +44 -0
- data/lib/swagger/serializer/model.rb +7 -0
- data/lib/swagger/serializer/rails_controller.rb +18 -0
- data/lib/swagger/serializer/store.rb +27 -0
- data/lib/swagger/serializer/version.rb +5 -0
- data/lib/swagger/serializer.rb +36 -0
- data/swagger-serializer.gemspec +44 -0
- metadata +255 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8b5999b543f38d6b343d1b788895eb70ca6510fd3d1be4a640ec1f4f9aeeb598
|
4
|
+
data.tar.gz: 426e6c6d4e1ddac1a033f577a5e1531b59b8b8f5a791cda738ec11e5dc97e2fd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2801936e0147191a061513a68fc783770515a90d506f368124deaacad1fb0b4411ae7341b898647187e59b59389a616324c5de017cb7bb6b52e16c796242b5a0
|
7
|
+
data.tar.gz: 385677aed112aeb96f2e7399c9698f35886886d5e39a1bd7e2c2f1b767589329b41b1ac1f92eda5289e0008dfcdf64cc370a28b665b26c77f6a74e1aeae44b81
|
data/.gitignore
ADDED
data/.prettierrc
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
inherit_from:
|
2
|
+
- .rubocop_airbnb.yml
|
3
|
+
inherit_gem:
|
4
|
+
rubocop-config-prettier: config/rubocop.yml
|
5
|
+
# prettier
|
6
|
+
Style/PercentLiteralDelimiters:
|
7
|
+
Description: Use `%`-literal delimiters consistently
|
8
|
+
StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#percent-literal-braces
|
9
|
+
Enabled: false
|
10
|
+
PreferredDelimiters:
|
11
|
+
! '%': ()
|
12
|
+
! '%i': ()
|
13
|
+
! '%q': ()
|
14
|
+
! '%Q': ()
|
15
|
+
! '%r': ! '{}'
|
16
|
+
! '%s': ()
|
17
|
+
! '%w': ()
|
18
|
+
! '%W': ()
|
19
|
+
! '%x': ()
|
20
|
+
Airbnb/SimpleModifierConditional:
|
21
|
+
Enabled: false
|
22
|
+
Airbnb/OptArgParameters:
|
23
|
+
Enabled: false
|
24
|
+
RSpec/EmptyExampleGroup:
|
25
|
+
Enabled: false
|
data/.rubocop_airbnb.yml
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2019 Narazaka
|
2
|
+
|
3
|
+
This software is provided 'as-is', without any express or implied
|
4
|
+
warranty. In no event will the authors be held liable for any damages
|
5
|
+
arising from the use of this software.
|
6
|
+
|
7
|
+
Permission is granted to anyone to use this software for any purpose,
|
8
|
+
including commercial applications, and to alter it and redistribute it
|
9
|
+
freely, subject to the following restrictions:
|
10
|
+
|
11
|
+
1. The origin of this software must not be misrepresented; you must not
|
12
|
+
claim that you wrote the original software. If you use this software
|
13
|
+
in a product, an acknowledgment in the product documentation would be
|
14
|
+
appreciated but is not required.
|
15
|
+
|
16
|
+
2. Altered source versions must be plainly marked as such, and must not be
|
17
|
+
misrepresented as being the original software.
|
18
|
+
|
19
|
+
3. This notice may not be removed or altered from any source
|
20
|
+
distribution.
|
data/README.md
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
# Swagger::Serializer
|
2
|
+
|
3
|
+
Swagger (OpenAPI 3) schema based serializer.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'swagger-serializer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install swagger-serializer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Rails
|
24
|
+
|
25
|
+
Write your OpenAPI spec.
|
26
|
+
|
27
|
+
```yaml
|
28
|
+
# swagger.yml
|
29
|
+
openapi: "3.0"
|
30
|
+
info:
|
31
|
+
title: example api
|
32
|
+
version: 0.1.0
|
33
|
+
components:
|
34
|
+
User: &User
|
35
|
+
title: User
|
36
|
+
type: object
|
37
|
+
properties:
|
38
|
+
id:
|
39
|
+
type: integer
|
40
|
+
name:
|
41
|
+
type: string
|
42
|
+
required: [id]
|
43
|
+
paths:
|
44
|
+
/users:
|
45
|
+
get:
|
46
|
+
responses:
|
47
|
+
200:
|
48
|
+
content:
|
49
|
+
application/json:
|
50
|
+
schema:
|
51
|
+
type: array
|
52
|
+
items: *User
|
53
|
+
/users/{id}:
|
54
|
+
get:
|
55
|
+
responses:
|
56
|
+
200:
|
57
|
+
content:
|
58
|
+
application/json:
|
59
|
+
schema: *User
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
Load it in initializer.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# config/initializers/swagger_serializer.rb
|
67
|
+
|
68
|
+
Swagger::Schema.load_file_to_current(
|
69
|
+
__dir__ + "/../../swagger.yml",
|
70
|
+
)
|
71
|
+
|
72
|
+
# Swagger::Schema accepts lazy load. So your swagger schema data instance may also be accepted.
|
73
|
+
#
|
74
|
+
# class SwaggerData
|
75
|
+
# def paths
|
76
|
+
# load_paths_now
|
77
|
+
# end
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# Swagger::Schema.current = Swagger::Schema.new(SwaggerData.new)
|
81
|
+
```
|
82
|
+
|
83
|
+
Use it in controllers.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
# app/controllers/application_controller.rb
|
87
|
+
class ApplicationController < ActionController::Base
|
88
|
+
include Swagger::Serializer::RailsController
|
89
|
+
|
90
|
+
def render_ok(data)
|
91
|
+
render_as_schema 200, :json, data
|
92
|
+
end
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
# app/controllers/users_controller.rb
|
98
|
+
class UsersController < ApplicationController
|
99
|
+
def index
|
100
|
+
render_ok User.all
|
101
|
+
end
|
102
|
+
|
103
|
+
def show
|
104
|
+
render_ok User.find(params[:id])
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
Would you want to customize serialization?
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
# app/serializers/base_serializer.rb
|
113
|
+
class BaseSerializer
|
114
|
+
include Swagger::Serializer
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
# app/serializers/user_serializer.rb
|
120
|
+
class UserSerializer < BaseSerializer
|
121
|
+
def name
|
122
|
+
"#{@model.name}!!!!"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
# app/models/user.rb
|
129
|
+
require_dependency "./app/serializers/user_serializer"
|
130
|
+
|
131
|
+
class User < ApplicationRecord
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
Now you can get `{ "id" => 42, "name" => "me!!!!" }`.
|
136
|
+
|
137
|
+
This serializer class detection uses the schema's `title` key.
|
138
|
+
If you want to use `Foo::BarSerializer`, set `Foo::Bar` to `title` key.
|
139
|
+
The key is configurable by `Swagger::Serializer::Store.current.inject_key = "my_inject_key"`.
|
140
|
+
|
141
|
+
Sometimes model needs direct serialize.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
# app/models/application_record.rb
|
145
|
+
class ApplicationRecord < ActiveRecord::Base
|
146
|
+
self.abstract_class = true
|
147
|
+
|
148
|
+
include Swagger::Serializer::Model
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
Now you can get serialized result by `p User.first.serialize`.
|
153
|
+
|
154
|
+
## Development
|
155
|
+
|
156
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
157
|
+
|
158
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
159
|
+
|
160
|
+
## Contributing
|
161
|
+
|
162
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Narazaka/swagger-serializer.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "swagger/serializer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
require "pry"
|
10
|
+
Pry.start
|
data/bin/fmt
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "json/schema/serializer"
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
class Schema
|
5
|
+
class Component
|
6
|
+
attr_reader :schema
|
7
|
+
|
8
|
+
def initialize(schema)
|
9
|
+
@schema = schema
|
10
|
+
end
|
11
|
+
|
12
|
+
def serializer(options = {})
|
13
|
+
JSON::Schema::Serializer.new(@schema, options)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative "./util"
|
2
|
+
require_relative "./media_type"
|
3
|
+
|
4
|
+
module Swagger
|
5
|
+
class Schema
|
6
|
+
class Content
|
7
|
+
def initialize(schema)
|
8
|
+
@schema = schema
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](type)
|
12
|
+
schema = Util.try_hash(@schema, type)
|
13
|
+
MediaType.new(schema) if schema
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.shortcut(name, type)
|
17
|
+
define_method(name) { self[type] }
|
18
|
+
end
|
19
|
+
|
20
|
+
shortcut :json, "application/json"
|
21
|
+
shortcut :xml, "application/xml"
|
22
|
+
shortcut :plain, "text/plain"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative "./util"
|
2
|
+
require_relative "./server"
|
3
|
+
|
4
|
+
module Swagger
|
5
|
+
class Schema
|
6
|
+
module HandleServers
|
7
|
+
def servers
|
8
|
+
Util.try_hash(@schema, :servers)
|
9
|
+
end
|
10
|
+
|
11
|
+
def server(index)
|
12
|
+
Server.new(servers[index])
|
13
|
+
end
|
14
|
+
|
15
|
+
def server_by_description(description)
|
16
|
+
Server.new(servers.find { |server| server.description == description })
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative "./util"
|
2
|
+
require_relative "./header"
|
3
|
+
|
4
|
+
module Swagger
|
5
|
+
class Schema
|
6
|
+
class Headers
|
7
|
+
def initialize(schema)
|
8
|
+
@schema = schema
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](name)
|
12
|
+
header = Util.try_hash(@schema, name)
|
13
|
+
Header.new(header) if header
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_json_schema
|
17
|
+
return { "type" => "object" } unless @schema
|
18
|
+
|
19
|
+
properties = {}
|
20
|
+
required = []
|
21
|
+
@schema.each do |_name, param|
|
22
|
+
name = _name.to_s
|
23
|
+
header = Header.new(param)
|
24
|
+
properties[name] = header.to_json_schema
|
25
|
+
required << name if header.required
|
26
|
+
if Util.try_hash(properties[name], :type).to_s == "string" && !parameter.allowEmptyValue &&
|
27
|
+
Util.try_hash(properties[name], :minLength).nil?
|
28
|
+
properties[name]["minLength"] = 1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
schema = { "type" => "object", "properties" => properties }
|
32
|
+
schema["required"] = required unless required.empty?
|
33
|
+
schema
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative "../util"
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
class Schema
|
5
|
+
class MediaType
|
6
|
+
class Validator
|
7
|
+
def initialize(schema)
|
8
|
+
@schema = schema
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate(data)
|
12
|
+
@parameters.to_json_schema
|
13
|
+
# validate
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "json/schema/serializer"
|
2
|
+
require_relative "./schema_accessor"
|
3
|
+
require_relative "./media_type/deserializer"
|
4
|
+
require_relative "./media_type/validator"
|
5
|
+
|
6
|
+
module Swagger
|
7
|
+
class Schema
|
8
|
+
class MediaType
|
9
|
+
extend SchemaAccessor
|
10
|
+
|
11
|
+
def initialize(schema)
|
12
|
+
@schema = schema
|
13
|
+
end
|
14
|
+
|
15
|
+
define_schema_accessor :schema, :example, :examples, :encoding
|
16
|
+
|
17
|
+
def serializer(options = {})
|
18
|
+
JSON::Schema::Serializer.new(schema, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def deserializer
|
22
|
+
Deserializer.new(schema)
|
23
|
+
end
|
24
|
+
|
25
|
+
def validator
|
26
|
+
Validator.new(schema)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative "./util"
|
2
|
+
require_relative "./path_item"
|
3
|
+
require_relative "./schema_accessor"
|
4
|
+
require_relative "./parameters"
|
5
|
+
require_relative "./request_body"
|
6
|
+
require_relative "./responses"
|
7
|
+
|
8
|
+
module Swagger
|
9
|
+
class Schema
|
10
|
+
class Operation
|
11
|
+
extend SchemaAccessor
|
12
|
+
|
13
|
+
def initialize(schema, path_item = nil)
|
14
|
+
@schema = schema
|
15
|
+
@path_item = path_item
|
16
|
+
end
|
17
|
+
|
18
|
+
define_schema_accessor %i[tags summary description externalDocs operationId deprecated callbacks security]
|
19
|
+
|
20
|
+
def parameters
|
21
|
+
Parameters.new((@path_item&.parameters || []) + (Util.try_hash(@schema, :parameters) || []))
|
22
|
+
end
|
23
|
+
|
24
|
+
def requestBody
|
25
|
+
RequestBody.new(Util.try_hash(@schema, :requestBody))
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :request_body, :requestBody
|
29
|
+
|
30
|
+
def responses
|
31
|
+
Responses.new(Util.try_hash(@schema, :responses))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative "./util"
|
2
|
+
require_relative "./schema_accessor"
|
3
|
+
require_relative "./parameter_base"
|
4
|
+
|
5
|
+
module Swagger
|
6
|
+
class Schema
|
7
|
+
class Parameter < ParameterBase
|
8
|
+
extend SchemaAccessor
|
9
|
+
|
10
|
+
define_schema_accessor :name, :in
|
11
|
+
|
12
|
+
%w[path query header cookie].each { |name| define_method(:"#{name}?") { self.in.to_s == name } }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative "./util"
|
2
|
+
require_relative "./schema_accessor"
|
3
|
+
|
4
|
+
module Swagger
|
5
|
+
class Schema
|
6
|
+
class ParameterBase
|
7
|
+
extend SchemaAccessor
|
8
|
+
|
9
|
+
def initialize(schema)
|
10
|
+
@schema = schema
|
11
|
+
end
|
12
|
+
|
13
|
+
define_schema_accessor %i[
|
14
|
+
description
|
15
|
+
required
|
16
|
+
deprecated
|
17
|
+
allowEmptyValue
|
18
|
+
style
|
19
|
+
explode
|
20
|
+
allowReserved
|
21
|
+
schema
|
22
|
+
example
|
23
|
+
examples
|
24
|
+
].freeze
|
25
|
+
|
26
|
+
def to_json_schema
|
27
|
+
schema
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "json/schema/serializer"
|
2
|
+
require_relative "../util"
|
3
|
+
|
4
|
+
module Swagger
|
5
|
+
class Schema
|
6
|
+
class Parameters < Array
|
7
|
+
class Deserializer
|
8
|
+
def initialize(parameters)
|
9
|
+
@parameters = parameters
|
10
|
+
end
|
11
|
+
|
12
|
+
def data(data)
|
13
|
+
DataDeserializer.new(@parameters, @data)
|
14
|
+
end
|
15
|
+
|
16
|
+
class DataDeserializer
|
17
|
+
def initialize(parameters, data)
|
18
|
+
@parameters = parameters
|
19
|
+
@data = data
|
20
|
+
end
|
21
|
+
|
22
|
+
def [](name)
|
23
|
+
parameter = @parameters.by_name(name)
|
24
|
+
value = Util.try_hash(data, name)
|
25
|
+
return nil if value.nil? && !parameter.required && !parameter.path?
|
26
|
+
|
27
|
+
JSON::Schema::Serializer.new(parameter.to_json_schema).serialize(value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative "../util"
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
class Schema
|
5
|
+
class Parameters < Array
|
6
|
+
class Validator
|
7
|
+
def initialize(parameters)
|
8
|
+
@parameters = parameters
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate(data)
|
12
|
+
@parameters.to_json_schema
|
13
|
+
# validate
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|