zero-rails_openapi 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ require 'open_api/config'
2
+
1
3
  module OpenApi
2
4
  module Generator
3
5
  def self.included(base)
@@ -12,12 +14,12 @@ module OpenApi
12
14
  if api_name.present?
13
15
  [{ api_name => generate_doc(api_name) }]
14
16
  else
15
- OpenApi.apis.keys.map { |api_key| { api_key => generate_doc(api_key) } }.reduce({ }, :merge)
17
+ Config.docs.keys.map { |api_key| { api_key => generate_doc(api_key) } }.reduce({ }, :merge)
16
18
  end
17
19
  end
18
20
 
19
21
  def generate_doc(api_name)
20
- settings = OpenApi.apis[api_name]
22
+ settings = Config.docs[api_name]
21
23
  doc = { openapi: '3.0.0' }.merge(settings.slice :info, :servers).merge(
22
24
  security: settings[:global_security], tags: [ ], paths: { },
23
25
  components: {
@@ -38,17 +40,33 @@ module OpenApi
38
40
  ActiveSupport::HashWithIndifferentAccess.new(doc.delete_if { |_, v| v.blank? })
39
41
  end
40
42
 
41
- def write_docs
43
+ def write_docs(generate_files = true)
42
44
  docs = generate_docs
43
- output_path = OpenApi.config.file_output_path
45
+ return unless generate_files
46
+ output_path = Config.file_output_path
44
47
  FileUtils.mkdir_p output_path
45
- docs.each do |api_name, doc|
46
- File.open("#{output_path}/#{api_name}.json", 'w') { |file| file.write JSON.pretty_generate doc }
48
+ max_length = docs.keys.map(&:size).sort.last
49
+ puts '[ZRO] * * * * * *'
50
+ docs.each do |doc_name, doc|
51
+ puts "[ZRO] `%#{max_length}s.json` is generated." % "#{doc_name}"
52
+ File.open("#{output_path}/#{doc_name}.json", 'w') { |file| file.write JSON.pretty_generate doc }
47
53
  end
48
54
  # pp $open_apis
49
55
  end
50
56
  end
51
57
 
58
+ def self.generate_builder_file(option)
59
+ return unless Config.generate_jbuilder_file
60
+ return unless option[:builder]
61
+ dir_path = "app/views/#{option[:path]}"
62
+ FileUtils.mkdir_p dir_path
63
+ file_path = "#{dir_path}/#{option[:action]}.json.jbuilder"
64
+ File.open(file_path, 'w') do |file|
65
+ file.write Config.jbuilder_templates[option[:builder]]
66
+ puts "[ZRO] JBuilder file generated: #{option[:path]}/#{option[:action]}"
67
+ end unless !Config.overwrite_jbuilder_file && File::exists?(file_path)
68
+ end
69
+
52
70
  def self.generate_routes_list
53
71
  # ref https://github.com/rails/rails/blob/master/railties/lib/rails/tasks/routes.rake
54
72
  require './config/routes'
@@ -0,0 +1,35 @@
1
+ module OpenApi
2
+ module DSL
3
+ module Helpers
4
+ def load_schema(model)
5
+ # About `show_attrs`, see:
6
+ # (1) BuilderSupport module: https://github.com/zhandao/zero-rails/blob/master/app/models/concerns/builder_support.rb
7
+ # (2) config in model: https://github.com/zhandao/zero-rails/tree/master/app/models/good.rb
8
+ # (3) jbuilder file: https://github.com/zhandao/zero-rails/blob/mster/app/views/api/v1/goods/index.json.jbuilder
9
+ # in a word, BuilderSupport let you control the `output fields and nested association infos` very easily.
10
+ if model&.respond_to? :show_attrs
11
+ columns = model.columns.map(&:name).map(&:to_sym)
12
+ model&.show_attrs&.map do |attr|
13
+ if columns.include? attr
14
+ index = columns.index attr
15
+ type = model.columns[index].sql_type_metadata.type.to_s.camelize
16
+ type = 'DateTime' if type == 'Datetime'
17
+ { attr => Object.const_get(type) }
18
+ elsif attr.match? /_info/
19
+ # TODO: 如何获知关系是 many?因为不能只判断结尾是否 ‘s’
20
+ assoc_model = Object.const_get(attr.to_s.split('_').first.singularize.camelize)
21
+ { attr => load_schema(assoc_model) }
22
+ end
23
+ end
24
+ else
25
+ model&.columns&.map do |column|
26
+ name = column.name.to_sym
27
+ type = column.sql_type_metadata.type.to_s.camelize
28
+ type = 'DateTime' if type == 'Datetime'
29
+ { name => Object.const_get(type) }
30
+ end
31
+ end&.compact&.reduce({ }, :merge)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module OpenApi
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zero-rails_openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhandao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-13 00:00:00.000000000 Z
11
+ date: 2017-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,8 +75,10 @@ files:
75
75
  - bin/console
76
76
  - bin/setup
77
77
  - documentation/examples/auto_gen_desc.rb
78
- - documentation/examples/auto_gen_dsl.rb
78
+ - documentation/examples/auto_gen_doc.rb
79
+ - documentation/examples/example_output_doc.json
79
80
  - documentation/examples/examples_controller.rb
81
+ - documentation/examples/goods_doc.rb
80
82
  - documentation/examples/open_api.rb
81
83
  - documentation/parameter.md
82
84
  - lib/oas_objs/helpers.rb
@@ -91,6 +93,7 @@ files:
91
93
  - lib/open_api/dsl.rb
92
94
  - lib/open_api/dsl_inside_block.rb
93
95
  - lib/open_api/generator.rb
96
+ - lib/open_api/helpers.rb
94
97
  - lib/open_api/version.rb
95
98
  - zero-rails_openapi.gemspec
96
99
  homepage: https://github.com/zhandao/zero-rails_openapi
@@ -113,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
116
  version: '0'
114
117
  requirements: []
115
118
  rubyforge_project:
116
- rubygems_version: 2.6.13
119
+ rubygems_version: 2.6.12
117
120
  signing_key:
118
121
  specification_version: 4
119
122
  summary: Generate the OpenAPI Specification 3 documentation for Rails application.
@@ -1,42 +0,0 @@
1
- require 'open_api/generator'
2
-
3
- # Usage: add `include AutoGenDSL` to base controller.
4
- module AutoGenDSL
5
- def self.included(base)
6
- base.extend ClassMethods
7
- end
8
-
9
- module ClassMethods
10
- def inherited(subclass)
11
- super
12
- subclass.class_eval do
13
- break unless self.name.match? /sController|sDoc/
14
- ctrl_path "api/#{self.name.sub('Doc', '').downcase.gsub('::', '/')}" if self.name.match? /sDoc/
15
- open_api_dry
16
- end
17
- end
18
-
19
- private
20
-
21
- def open_api_dry
22
- ctrl_path = try(:controller_path) || instance_variable_get('@_ctrl_path')
23
- ::OpenApi::Generator.get_actions_by_ctrl_path(ctrl_path)&.each do |action|
24
- api_dry action do
25
- # Token in Header
26
- if !action_path.match?(/NoVerificationController/) && !%w[create login].include?(action)
27
- header! 'Token', String, desc: 'user token'
28
- end
29
-
30
- # Common :index parameters
31
- if !action_path.match?(/NotDRYController/) && action == 'index'
32
- query :page, Integer, desc: 'page'
33
- query :per_page, Integer, desc: 'per'
34
- end
35
-
36
- # OAS require at least one response on each api.
37
- default_response 'default response', :json
38
- end
39
- end
40
- end
41
- end
42
- end