openapi2ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +54 -0
- data/.travis.yml +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +56 -0
- data/LICENSE.txt +21 -0
- data/README.md +118 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/openapi2ruby +4 -0
- data/bin/setup +8 -0
- data/exe/openapi2ruby +4 -0
- data/lib/openapi2ruby.rb +9 -0
- data/lib/openapi2ruby/cli.rb +27 -0
- data/lib/openapi2ruby/generator.rb +33 -0
- data/lib/openapi2ruby/openapi.rb +17 -0
- data/lib/openapi2ruby/openapi/schema.rb +38 -0
- data/lib/openapi2ruby/openapi/schema/property.rb +62 -0
- data/lib/openapi2ruby/parser.rb +27 -0
- data/lib/openapi2ruby/templates/serializer.rb.erb +35 -0
- data/lib/openapi2ruby/version.rb +3 -0
- data/openapi2ruby.gemspec +32 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9f4c6123cb3514c275e9f51e81a3776e10072141ea644cb686d626e40d3ec2d0
|
4
|
+
data.tar.gz: 88c01d745890bb64911f5d1392b890ee97622c36f1f25f159e2bb09050b037e1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f643a49e8afd59229c59265ba9e3b35d75e1364bcdaff49735d23f508a272f00a86dc442a698efd928968452db64625c066580be42ceed0e686634f54f741c6
|
7
|
+
data.tar.gz: 0e16178a67ba986da2c49654ed875ef956a401b380c588d6eed1f10cac1dc3ff46286f6e7936ae2c0d73ef4cc81c151b9cb0a9385d45b36781616a9355053375
|
data/.gitignore
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/spec/tmp/*
|
10
|
+
/test/tmp/
|
11
|
+
/test/version_tmp/
|
12
|
+
/tmp/
|
13
|
+
.rspec_status
|
14
|
+
|
15
|
+
!.gitkeep
|
16
|
+
|
17
|
+
# Used by dotenv library to load environment variables.
|
18
|
+
# .env
|
19
|
+
|
20
|
+
## Specific to RubyMotion:
|
21
|
+
.dat*
|
22
|
+
.repl_history
|
23
|
+
build/
|
24
|
+
*.bridgesupport
|
25
|
+
build-iPhoneOS/
|
26
|
+
build-iPhoneSimulator/
|
27
|
+
|
28
|
+
## Specific to RubyMotion (use of CocoaPods):
|
29
|
+
#
|
30
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
31
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
32
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
33
|
+
#
|
34
|
+
# vendor/Pods/
|
35
|
+
|
36
|
+
## Documentation cache and generated files:
|
37
|
+
/.yardoc/
|
38
|
+
/_yardoc/
|
39
|
+
/doc/
|
40
|
+
/rdoc/
|
41
|
+
|
42
|
+
## Environment normalization:
|
43
|
+
/.bundle/
|
44
|
+
/vendor/bundle
|
45
|
+
/lib/bundler/man/
|
46
|
+
|
47
|
+
# for a library or gem, you might want to ignore these files since the code is
|
48
|
+
# intended to run in multiple environments; otherwise, check them in:
|
49
|
+
# Gemfile.lock
|
50
|
+
# .ruby-version
|
51
|
+
# .ruby-gemset
|
52
|
+
|
53
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
54
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
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
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
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
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at takanamito0928@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
openapi2ruby (0.1.0)
|
5
|
+
activesupport
|
6
|
+
thor (~> 0.20)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (5.2.0)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
coderay (1.1.2)
|
17
|
+
concurrent-ruby (1.0.5)
|
18
|
+
diff-lcs (1.3)
|
19
|
+
i18n (1.0.1)
|
20
|
+
concurrent-ruby (~> 1.0)
|
21
|
+
method_source (0.9.0)
|
22
|
+
minitest (5.11.3)
|
23
|
+
pry (0.11.3)
|
24
|
+
coderay (~> 1.1.0)
|
25
|
+
method_source (~> 0.9.0)
|
26
|
+
rake (10.5.0)
|
27
|
+
rspec (3.7.0)
|
28
|
+
rspec-core (~> 3.7.0)
|
29
|
+
rspec-expectations (~> 3.7.0)
|
30
|
+
rspec-mocks (~> 3.7.0)
|
31
|
+
rspec-core (3.7.1)
|
32
|
+
rspec-support (~> 3.7.0)
|
33
|
+
rspec-expectations (3.7.0)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.7.0)
|
36
|
+
rspec-mocks (3.7.0)
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
+
rspec-support (~> 3.7.0)
|
39
|
+
rspec-support (3.7.1)
|
40
|
+
thor (0.20.0)
|
41
|
+
thread_safe (0.3.6)
|
42
|
+
tzinfo (1.2.5)
|
43
|
+
thread_safe (~> 0.1)
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
bundler (~> 1.15)
|
50
|
+
openapi2ruby!
|
51
|
+
pry
|
52
|
+
rake (~> 10.0)
|
53
|
+
rspec (~> 3.0)
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 takanamito
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# Openapi2ruby
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.com/takanamito/openapi2ruby.svg?branch=master)](https://travis-ci.com/takanamito/openapi2ruby)
|
4
|
+
|
5
|
+
A library to generate ruby class from openapi.yaml.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'openapi2ruby'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install openapi2ruby
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
You can generate ruby class from openapi.yaml (Now support OpenAPI Specification 3.0 only)
|
26
|
+
|
27
|
+
For example, you uses OpenAPI-Specification [link-example schema](https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v3.0/link-example.yaml#L178-L203).
|
28
|
+
|
29
|
+
```sh
|
30
|
+
# generate ruby class
|
31
|
+
$ openapi2ruby generate path/to/link-example.yaml --out ./
|
32
|
+
|
33
|
+
$ ls
|
34
|
+
pullrequest_serializer.rb repository_serializer.rb user_serializer.rb
|
35
|
+
```
|
36
|
+
|
37
|
+
Generated class is below.
|
38
|
+
Default, this gem generates `ActiveModel::Serializer` class.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class RepositorySerializer < ActiveModel::Serializer
|
42
|
+
attributes :slug, :owner
|
43
|
+
|
44
|
+
def owner
|
45
|
+
UserSerializer.new(object.user)
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def slug
|
50
|
+
type_check(:slug, [String])
|
51
|
+
object.slug
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def type_check(name, types)
|
57
|
+
raise "Field type is invalid. #{name}" unless types.include?(object.send(name).class)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
### Use original template
|
63
|
+
|
64
|
+
If you wants to generate from other template with ERB.
|
65
|
+
You can specify it with cli option.
|
66
|
+
|
67
|
+
Write original template.
|
68
|
+
|
69
|
+
```erb
|
70
|
+
class <%= @schema.name %>
|
71
|
+
attr_accessor <%= @schema.properties.map{ |p| ":#{p.name}" }.join(', ') %>
|
72
|
+
|
73
|
+
def intiialize(args)
|
74
|
+
<%- @schema.properties.each do |p| -%>
|
75
|
+
<%= "@#{p.name} = args[:#{p.name}]" %>
|
76
|
+
<%- end -%>
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
Generate with `--template` option.
|
82
|
+
|
83
|
+
```sh
|
84
|
+
$ openapi2ruby generate path/to/link-example.yaml \
|
85
|
+
--template path/to/original_template.rb.erb \
|
86
|
+
--out ./
|
87
|
+
```
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class Repository
|
91
|
+
attr_accessor :slug, :owner
|
92
|
+
|
93
|
+
def intiialize(args)
|
94
|
+
@slug = args[:slug]
|
95
|
+
@owner = args[:owner]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
For more template value information, please check [default template](https://github.com/takanamito/openapi2ruby/blob/master/lib/openapi2ruby/templates/serializer.rb.erb).
|
101
|
+
|
102
|
+
## Development
|
103
|
+
|
104
|
+
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.
|
105
|
+
|
106
|
+
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).
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/openapi2ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
111
|
+
|
112
|
+
## License
|
113
|
+
|
114
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
115
|
+
|
116
|
+
## Code of Conduct
|
117
|
+
|
118
|
+
Everyone interacting in the Openapi2ruby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/takanamito/openapi2ruby/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/openapi2ruby
ADDED
data/bin/setup
ADDED
data/exe/openapi2ruby
ADDED
data/lib/openapi2ruby.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require "openapi2ruby/version"
|
2
|
+
require 'openapi2ruby/cli'
|
3
|
+
require 'openapi2ruby/parser'
|
4
|
+
require 'openapi2ruby/generator'
|
5
|
+
require 'openapi2ruby/openapi'
|
6
|
+
require 'openapi2ruby/openapi/schema'
|
7
|
+
require 'openapi2ruby/openapi/schema/property'
|
8
|
+
|
9
|
+
require 'active_support/core_ext/string/inflections'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'thor'
|
2
|
+
|
3
|
+
module Openapi2ruby
|
4
|
+
class Cli < Thor
|
5
|
+
desc 'parse', 'load openapi.yaml'
|
6
|
+
def parse(path)
|
7
|
+
puts 'Loading...'
|
8
|
+
raise "File not found. #{path}" unless File.exist?(path)
|
9
|
+
|
10
|
+
openapi = Openapi2ruby::Parser.parse(path)
|
11
|
+
p openapi.schemas
|
12
|
+
end
|
13
|
+
|
14
|
+
option :template, type: :string
|
15
|
+
option :out, required: true, type: :string
|
16
|
+
desc 'generate', 'load openapi.yaml and generate serializer'
|
17
|
+
def generate(path)
|
18
|
+
puts 'Loading...'
|
19
|
+
raise "File not found. #{path}" unless File.exist?(path)
|
20
|
+
|
21
|
+
openapi = Openapi2ruby::Parser.parse(path)
|
22
|
+
openapi.schemas.each do |schema|
|
23
|
+
Openapi2ruby::Generator.generate(schema, options[:out], options[:template])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
require 'erb'
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
module Openapi2ruby
|
6
|
+
class Generator
|
7
|
+
TEMPLATE_PATH = File.expand_path('../templates/serializer.rb.erb', __FILE__)
|
8
|
+
|
9
|
+
# Generate ruby class from OpenAPI schema
|
10
|
+
# @param schema [Openapi2ruby::Openapi::Schema] parsed OpenAPI schema
|
11
|
+
# @param output_path [String] parsed OpenAPI YAML
|
12
|
+
# @param template_path [String] original template path
|
13
|
+
def self.generate(schema, output_path, template_path)
|
14
|
+
new(schema).generate(output_path, template_path)
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(schema)
|
18
|
+
@schema = schema
|
19
|
+
end
|
20
|
+
|
21
|
+
# Generate ruby class from OpenAPI schema
|
22
|
+
# @param output_path [String] parsed OpenAPI YAML
|
23
|
+
# @param template_path [String] original template path
|
24
|
+
def generate(output_path, template_path)
|
25
|
+
template_path = TEMPLATE_PATH if template_path.nil?
|
26
|
+
template = File.read(template_path)
|
27
|
+
generated_class = ERB.new(template, nil, '-').result(binding)
|
28
|
+
|
29
|
+
output_file = Pathname.new(output_path).join("#{@schema.name.underscore}_serializer.rb")
|
30
|
+
File.open(output_file.to_s, 'w') { |file| file << generated_class }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Openapi2ruby
|
2
|
+
class Openapi
|
3
|
+
def initialize(content)
|
4
|
+
@content = content
|
5
|
+
end
|
6
|
+
|
7
|
+
# Creates OpenAPI Schema array
|
8
|
+
# @return [Array[Openapi2ruby::Openapi::Schema]]
|
9
|
+
def schemas
|
10
|
+
@content['components']['schemas'].each_with_object([]) do |(key, value), results|
|
11
|
+
schema_content = { name: key, definition: value}
|
12
|
+
schema = Openapi2ruby::Openapi::Schema.new(schema_content)
|
13
|
+
results << schema unless schema.properties.empty?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Openapi2ruby
|
2
|
+
class Openapi::Schema
|
3
|
+
def initialize(content)
|
4
|
+
@name = content[:name]
|
5
|
+
@definition = content[:definition]
|
6
|
+
end
|
7
|
+
|
8
|
+
# OpenAPI camelcase schema name
|
9
|
+
# @return [String]
|
10
|
+
def name
|
11
|
+
@name.camelcase
|
12
|
+
end
|
13
|
+
|
14
|
+
# OpenAPI required properties name
|
15
|
+
# @return [Array[String]]
|
16
|
+
def requireds
|
17
|
+
@definition['required']
|
18
|
+
end
|
19
|
+
|
20
|
+
# OpenAPI schema properties
|
21
|
+
# @return [Array[Openapi2ruby::Openapi::Schema]]
|
22
|
+
def properties
|
23
|
+
return [] if @definition['properties'].nil?
|
24
|
+
@definition['properties'].each_with_object([]) do |(key, value), results|
|
25
|
+
content = { name: key, definition: value }
|
26
|
+
results << Openapi2ruby::Openapi::Schema::Property.new(content)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Whether property is required or not
|
31
|
+
# @param [Openapi2ruby::Openapi::Schema::Property] OpenAPI schema property
|
32
|
+
# @return [Boolean]
|
33
|
+
def required?(property)
|
34
|
+
return false if requireds.nil?
|
35
|
+
requireds.include?(property.name)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Openapi2ruby
|
2
|
+
class Openapi::Schema::Property
|
3
|
+
attr_reader :name
|
4
|
+
|
5
|
+
def initialize(content)
|
6
|
+
@name = content[:name]
|
7
|
+
@type = content[:definition]['type']
|
8
|
+
@items = content[:definition]['items']
|
9
|
+
@format = content[:definition]['format']
|
10
|
+
@ref = content[:definition]['$ref']
|
11
|
+
end
|
12
|
+
|
13
|
+
# OpenAPI schema ref property name
|
14
|
+
# @return [String]
|
15
|
+
def ref
|
16
|
+
return @items['$ref'].split('/').last if ref_items?
|
17
|
+
@ref.split('/').last
|
18
|
+
end
|
19
|
+
|
20
|
+
# OpenAPI schema ref property class name
|
21
|
+
# @return [String]
|
22
|
+
def ref_class
|
23
|
+
ref.camelcase
|
24
|
+
end
|
25
|
+
|
26
|
+
# Whether property is ref or not
|
27
|
+
# @return [Boolean]
|
28
|
+
def ref?
|
29
|
+
!@ref.nil?
|
30
|
+
end
|
31
|
+
|
32
|
+
# Whether property has ref array items
|
33
|
+
# @return [Boolean]
|
34
|
+
def ref_items?
|
35
|
+
@type == 'array' && !@items['$ref'].nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
# OpenAPI schema property types
|
39
|
+
# @return [Array[Class]]
|
40
|
+
def types
|
41
|
+
return [ref] if @type.nil?
|
42
|
+
converted_types
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# OpenAPI schema property types in Ruby
|
48
|
+
# @return [Array[Class]]
|
49
|
+
def converted_types
|
50
|
+
case @type
|
51
|
+
when 'string', 'integer', 'array'
|
52
|
+
[Object.const_get(@type.capitalize)]
|
53
|
+
when 'number'
|
54
|
+
[Float]
|
55
|
+
when 'boolean'
|
56
|
+
[TrueClass, FalseClass]
|
57
|
+
when 'object'
|
58
|
+
[Hash]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Openapi2ruby
|
4
|
+
class Parser
|
5
|
+
# Parse openapi.yaml
|
6
|
+
# @param path [String] OpenAPI schema file path
|
7
|
+
# @return [Openapi2ruby::Openapi]
|
8
|
+
def self.parse(path)
|
9
|
+
new(path).parse
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(path)
|
13
|
+
@path = path
|
14
|
+
end
|
15
|
+
|
16
|
+
# Parse openapi.yaml
|
17
|
+
# @return [Openapi2ruby::Openapi]
|
18
|
+
def parse
|
19
|
+
Openapi.new(parse_file)
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse_file
|
23
|
+
file = File.read(@path)
|
24
|
+
YAML.load(file)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class <%= @schema.name %>Serializer < ActiveModel::Serializer
|
2
|
+
attributes <%= @schema.properties.map{ |p| ":#{p.name}" }.join(', ') -%>
|
3
|
+
|
4
|
+
<%- @schema.properties.select{ |p| p.ref? || p.ref_items? }.each do |p| %>
|
5
|
+
def <%= p.name -%>
|
6
|
+
<%- if p.ref? %>
|
7
|
+
<%= "#{p.ref_class}Serializer.new(object.#{p.ref})" %>
|
8
|
+
<%- else %>
|
9
|
+
ActiveModel::Serializer::CollectionSerializer.new(
|
10
|
+
<%= "object.#{p.name}" -%>,
|
11
|
+
serializer: <%= "#{p.ref_class}" -%>Serializer,
|
12
|
+
)
|
13
|
+
<%- end -%>
|
14
|
+
end
|
15
|
+
<%- end -%>
|
16
|
+
|
17
|
+
<%- @schema.properties.reject{ |p| p.ref? || p.ref_items? }.each do |p| %>
|
18
|
+
def <%= p.name -%>
|
19
|
+
<%- if @schema.required?(p) %>
|
20
|
+
required_check(<%= ":#{p.name}" %>)
|
21
|
+
<%- end %>
|
22
|
+
type_check(<%= ":#{p.name}, #{p.types}" -%>)
|
23
|
+
object.<%=- p.name %>
|
24
|
+
end
|
25
|
+
<%- end %>
|
26
|
+
private
|
27
|
+
<%- unless @schema.requireds.nil? %>
|
28
|
+
def required_check(name)
|
29
|
+
raise "Required field is nil. #{name}" if object.send(name).nil?
|
30
|
+
end
|
31
|
+
<%- end %>
|
32
|
+
def type_check(name, types)
|
33
|
+
raise "Field type is invalid. #{name}" unless types.include?(object.send(name).class)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "openapi2ruby/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "openapi2ruby"
|
8
|
+
spec.version = Openapi2ruby::VERSION
|
9
|
+
spec.authors = ["takanamito"]
|
10
|
+
spec.email = ["takanamito0928@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple ruby class generator from OpenAPI schema}
|
13
|
+
spec.description = %q{A simple ruby class generator from OpenAPI schema}
|
14
|
+
spec.homepage = "https://github.com/takanamito/openapi2ruby"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
spec.required_ruby_version = '~> 2.3'
|
24
|
+
|
25
|
+
spec.add_dependency 'activesupport'
|
26
|
+
spec.add_dependency 'thor', '~> 0.20'
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency 'pry'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openapi2ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- takanamito
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: thor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.20'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.20'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.15'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.15'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: A simple ruby class generator from OpenAPI schema
|
98
|
+
email:
|
99
|
+
- takanamito0928@gmail.com
|
100
|
+
executables:
|
101
|
+
- openapi2ruby
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".travis.yml"
|
107
|
+
- CODE_OF_CONDUCT.md
|
108
|
+
- Gemfile
|
109
|
+
- Gemfile.lock
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- bin/console
|
114
|
+
- bin/openapi2ruby
|
115
|
+
- bin/setup
|
116
|
+
- exe/openapi2ruby
|
117
|
+
- lib/openapi2ruby.rb
|
118
|
+
- lib/openapi2ruby/cli.rb
|
119
|
+
- lib/openapi2ruby/generator.rb
|
120
|
+
- lib/openapi2ruby/openapi.rb
|
121
|
+
- lib/openapi2ruby/openapi/schema.rb
|
122
|
+
- lib/openapi2ruby/openapi/schema/property.rb
|
123
|
+
- lib/openapi2ruby/parser.rb
|
124
|
+
- lib/openapi2ruby/templates/serializer.rb.erb
|
125
|
+
- lib/openapi2ruby/version.rb
|
126
|
+
- openapi2ruby.gemspec
|
127
|
+
homepage: https://github.com/takanamito/openapi2ruby
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
metadata: {}
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - "~>"
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '2.3'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.7.6
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: A simple ruby class generator from OpenAPI schema
|
151
|
+
test_files: []
|