openapi2ruby 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f4c6123cb3514c275e9f51e81a3776e10072141ea644cb686d626e40d3ec2d0
4
- data.tar.gz: 88c01d745890bb64911f5d1392b890ee97622c36f1f25f159e2bb09050b037e1
3
+ metadata.gz: 2ce32df5e809f2e8d1ca8a8541e1aba92bdbfcb0b33b04dd8f8dada8f96ee1e1
4
+ data.tar.gz: bf4ae95664894cecde89c5a53c1e0c8e76184e431ee277bd1d65884a0fde0270
5
5
  SHA512:
6
- metadata.gz: 3f643a49e8afd59229c59265ba9e3b35d75e1364bcdaff49735d23f508a272f00a86dc442a698efd928968452db64625c066580be42ceed0e686634f54f741c6
7
- data.tar.gz: 0e16178a67ba986da2c49654ed875ef956a401b380c588d6eed1f10cac1dc3ff46286f6e7936ae2c0d73ef4cc81c151b9cb0a9385d45b36781616a9355053375
6
+ metadata.gz: 953267ea4d81f51a158499950d0598266663fd0c127b1217eaa2e2d6c7afe8d08caea17b34d678bf9c8fb5ed9343d368986d3359ec3cd87b670f6625a8e14c50
7
+ data.tar.gz: d4e8858dc138fc7c8eba1f83f785907c3b561ad970ec4f2740efc50b0836e69dd52f7e37dd9b81e041973d5c2c693b6eceacdc064c69c754a371f75c61c585bd
data/.gitignore CHANGED
@@ -46,9 +46,9 @@ build-iPhoneSimulator/
46
46
 
47
47
  # for a library or gem, you might want to ignore these files since the code is
48
48
  # intended to run in multiple environments; otherwise, check them in:
49
- # Gemfile.lock
50
- # .ruby-version
51
- # .ruby-gemset
49
+ Gemfile.lock
50
+ .ruby-version
51
+ .ruby-gemset
52
52
 
53
53
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
54
54
  .rvmrc
data/.travis.yml CHANGED
@@ -3,9 +3,10 @@ sudo: false
3
3
  cache: bundler
4
4
  rvm:
5
5
  - 2.3.0
6
- - 2.3.7
7
- - 2.4.4
8
- - 2.5.1
6
+ - 2.3.8
7
+ - 2.4.5
8
+ - 2.5.3
9
+ - 2.6.1
9
10
  - ruby-head
10
11
  matrix:
11
12
  fast_finish: true
@@ -4,7 +4,7 @@ module Openapi2ruby
4
4
  class Cli < Thor
5
5
  desc 'parse', 'load openapi.yaml'
6
6
  def parse(path)
7
- puts 'Loading...'
7
+ puts 'Loading OpenAPI yaml file...'
8
8
  raise "File not found. #{path}" unless File.exist?(path)
9
9
 
10
10
  openapi = Openapi2ruby::Parser.parse(path)
@@ -15,12 +15,13 @@ module Openapi2ruby
15
15
  option :out, required: true, type: :string
16
16
  desc 'generate', 'load openapi.yaml and generate serializer'
17
17
  def generate(path)
18
- puts 'Loading...'
18
+ puts 'Loading OpenAPI yaml file...'
19
19
  raise "File not found. #{path}" unless File.exist?(path)
20
20
 
21
21
  openapi = Openapi2ruby::Parser.parse(path)
22
22
  openapi.schemas.each do |schema|
23
- Openapi2ruby::Generator.generate(schema, options[:out], options[:template])
23
+ serializer = Openapi2ruby::Generator.generate(schema, options[:out], options[:template])
24
+ puts "Created: #{serializer}"
24
25
  end
25
26
  end
26
27
  end
@@ -28,6 +28,7 @@ module Openapi2ruby
28
28
 
29
29
  output_file = Pathname.new(output_path).join("#{@schema.name.underscore}_serializer.rb")
30
30
  File.open(output_file.to_s, 'w') { |file| file << generated_class }
31
+ output_file.to_s
31
32
  end
32
33
  end
33
34
  end
@@ -34,5 +34,16 @@ module Openapi2ruby
34
34
  return false if requireds.nil?
35
35
  requireds.include?(property.name)
36
36
  end
37
+
38
+ def one_ofs
39
+ return [] if properties.empty?
40
+ properties.each_with_object([]) do |value, results|
41
+ if value.one_of?
42
+ results << value.one_of_refs
43
+ else
44
+ results
45
+ end
46
+ end
47
+ end
37
48
  end
38
49
  end
@@ -8,6 +8,14 @@ module Openapi2ruby
8
8
  @items = content[:definition]['items']
9
9
  @format = content[:definition]['format']
10
10
  @ref = content[:definition]['$ref']
11
+ if content[:definition]['oneOf']
12
+ @type = 'oneOf'
13
+ @one_of_refs = content[:definition]['oneOf'].map do |content|
14
+ content['$ref'].split('/').last
15
+ end
16
+ else
17
+ @one_of_refs = []
18
+ end
11
19
  end
12
20
 
13
21
  # OpenAPI schema ref property name
@@ -38,10 +46,20 @@ module Openapi2ruby
38
46
  # OpenAPI schema property types
39
47
  # @return [Array[Class]]
40
48
  def types
49
+ return [Hash] if one_of?
41
50
  return [ref] if @type.nil?
51
+
42
52
  converted_types
43
53
  end
44
54
 
55
+ def one_of?
56
+ @type == 'oneOf' && !@one_of_refs.empty?
57
+ end
58
+
59
+ def one_of_refs
60
+ @one_of_refs
61
+ end
62
+
45
63
  private
46
64
 
47
65
  # OpenAPI schema property types in Ruby
@@ -19,8 +19,17 @@ class <%= @schema.name %>Serializer < ActiveModel::Serializer
19
19
  <%- if @schema.required?(p) %>
20
20
  required_check(<%= ":#{p.name}" %>)
21
21
  <%- end %>
22
+ <%- if p.one_of? -%>
23
+ type_check(<%= ":#{p.name}"-%>, [
24
+ <%- p.one_of_refs.map do |ref| -%>
25
+ <%= ref -%>,
26
+ <%- end -%>
27
+ ])
28
+ one_of_<%=- p.name %>(object.<%=- p.name %>)
29
+ <%- else -%>
22
30
  type_check(<%= ":#{p.name}, #{p.types}" -%>)
23
31
  object.<%=- p.name %>
32
+ <%- end -%>
24
33
  end
25
34
  <%- end %>
26
35
  private
@@ -32,4 +41,11 @@ class <%= @schema.name %>Serializer < ActiveModel::Serializer
32
41
  def type_check(name, types)
33
42
  raise "Field type is invalid. #{name}" unless types.include?(object.send(name).class)
34
43
  end
44
+ <%- @schema.properties.select{ |p| p.one_of? }.each do |p| %>
45
+ def one_of_<%=- p.name %>(one_of_value)
46
+ serializer = ActiveModelSerializers::SerializableResource.new(one_of_value).serializer
47
+ raise "Undefined serializer one_of ref: #{one_of_value.class}" if serializer.nil?
48
+ serializer.new(one_of_value)
49
+ end
50
+ <%- end -%>
35
51
  end
@@ -1,3 +1,3 @@
1
1
  module Openapi2ruby
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/openapi2ruby.gemspec CHANGED
@@ -25,8 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency 'activesupport'
26
26
  spec.add_dependency 'thor', '~> 0.20'
27
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'
28
+ spec.add_development_dependency "bundler"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rspec"
32
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi2ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takanamito
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-24 00:00:00.000000000 Z
11
+ date: 2019-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -42,46 +42,32 @@ dependencies:
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.15'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.15'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
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
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
73
  - - ">="
@@ -106,7 +92,6 @@ files:
106
92
  - ".travis.yml"
107
93
  - CODE_OF_CONDUCT.md
108
94
  - Gemfile
109
- - Gemfile.lock
110
95
  - LICENSE.txt
111
96
  - README.md
112
97
  - Rakefile
@@ -143,8 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
128
  - !ruby/object:Gem::Version
144
129
  version: '0'
145
130
  requirements: []
146
- rubyforge_project:
147
- rubygems_version: 2.7.6
131
+ rubygems_version: 3.0.1
148
132
  signing_key:
149
133
  specification_version: 4
150
134
  summary: A simple ruby class generator from OpenAPI schema
data/Gemfile.lock DELETED
@@ -1,56 +0,0 @@
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