swagger_api 0.1.47 → 0.1.48

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 812e8f25428409c5e5d1333c7c0291c9e78f7fa7
4
- data.tar.gz: 4ff23a9a7418a41a2ef02add83d7f2155caa0c82
3
+ metadata.gz: ebb4dd2a0633d5191515c8699a0c6b03c8441e91
4
+ data.tar.gz: f9cb561189108adfd5c7fb599ef4af095de44a6a
5
5
  SHA512:
6
- metadata.gz: 64a94addb280474318675bf9a94ec33cc1f521df2861eb36452d35cd93e89824dd7911750d813eb8e5c69fa0d102f6c4c303dcc207a941e1243fee657f9e08b6
7
- data.tar.gz: 94ff5977456c75aed5acd6e007d2bac65c77a1fe2912b126b925abd294a5821f378b7b1c61b11b42a574eacc5ff45a3920f0d8fe3b30857430a5fc6973f4607e
6
+ metadata.gz: e716f77b6772363c94b31d97a8c74ee748cccd18fae8bdb9266ef390ab9255bba31cc09910609cd9a77524e122a4f0e4efb81a88145d683ab437b855b529b23e
7
+ data.tar.gz: 6026a289c5edc66bd67e9d4ab8ececa879aeac81883b08df47055c57f8297b096b8e1fbb863d8f0be2af917670ad1b731b6005a2ace5c384fcf41e2bc618d872
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swagger_api (0.1.43)
4
+ swagger_api (0.1.47)
5
5
  active_attr
6
6
  rails (>= 5.0)
7
7
  rspec-rails
@@ -32,7 +32,7 @@ GEM
32
32
  erubi (~> 1.4)
33
33
  rails-dom-testing (~> 2.0)
34
34
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
35
- active_attr (0.10.3)
35
+ active_attr (0.11.0)
36
36
  activemodel (>= 3.0.2, < 6.0)
37
37
  activesupport (>= 3.0.2, < 6.0)
38
38
  activejob (5.2.0)
@@ -61,7 +61,7 @@ GEM
61
61
  erubi (1.7.1)
62
62
  globalid (0.4.1)
63
63
  activesupport (>= 4.2.0)
64
- i18n (1.0.0)
64
+ i18n (1.0.1)
65
65
  concurrent-ruby (~> 1.0)
66
66
  loofah (2.2.2)
67
67
  crass (~> 1.0.2)
@@ -75,10 +75,10 @@ GEM
75
75
  mini_mime (1.0.0)
76
76
  mini_portile2 (2.3.0)
77
77
  minitest (5.11.3)
78
- nio4r (2.3.0)
79
- nokogiri (1.8.2)
78
+ nio4r (2.3.1)
79
+ nokogiri (1.8.3)
80
80
  mini_portile2 (~> 2.3.0)
81
- rack (2.0.4)
81
+ rack (2.0.5)
82
82
  rack-test (1.0.0)
83
83
  rack (>= 1.0, < 3)
84
84
  rails (5.2.0)
@@ -16,7 +16,7 @@ module SwaggerApi
16
16
 
17
17
  def required
18
18
  controller.try(:columns).try(:required) ||
19
- columns.map(&:name) & clean_required_attributes
19
+ columns.map(&:name).map { |name| name.gsub(/^encrypted_/, '') } & clean_required_attributes
20
20
  end
21
21
 
22
22
  def clean_required_attributes
@@ -35,14 +35,22 @@ module SwaggerApi
35
35
  end.compact.flatten.uniq.map(&:to_s)
36
36
  end
37
37
 
38
+ def absence_attributes
39
+ @absence_attributes ||= model.validators.map do |validator|
40
+ validator.attributes if validator.is_a?(ActiveRecord::Validations::AbsenceValidator)
41
+ end.compact.flatten.uniq.map(&:to_s)
42
+ end
43
+
38
44
  def properties
39
- properties = {}
40
- columns.each do |column|
41
- properties[column.name] = ColumnSchema.new(column: column).create
42
- end
43
- properties
45
+ columns.map do |column|
46
+ next if column.name.end_with? '_iv'
47
+ next if absence_attributes.include? column.name
48
+ attribute_name = column.name.gsub(/^encrypted_/, '')
49
+ [attribute_name, ColumnSchema.new(column: column).create]
50
+ end.compact.to_h
44
51
  end
45
52
 
53
+
46
54
  def model
47
55
  controller.model.constantize
48
56
  end
@@ -1,6 +1,8 @@
1
1
  module SwaggerApi
2
2
  class Components
3
3
  include ActiveAttr::Model
4
+ include Concerns::StiSchema
5
+
4
6
  attr_accessor :controllers
5
7
 
6
8
  def create
@@ -8,7 +10,7 @@ module SwaggerApi
8
10
  @components = {}
9
11
  controllers.each do |controller|
10
12
  if controller.custom_model_file.nil?
11
- @components[controller.model] = ComponentSchema.new(controller: controller).create
13
+ @components.merge!(create_model_components_from_controller(controller))
12
14
  else
13
15
  @components.merge!(custom_json(controller.custom_model_file))
14
16
  end
@@ -20,5 +22,17 @@ module SwaggerApi
20
22
  file = File.read(custom_model_file)
21
23
  JSON.parse(file)
22
24
  end
25
+
26
+ def create_model_components_from_controller(controller)
27
+ if is_sti?(controller.model)
28
+ controller.model.constantize.descendants.map do |klass|
29
+ [klass.name, ComponentSchema.new(controller: OpenStruct.new(model: klass.name)).create]
30
+ end.to_h
31
+ else
32
+ {
33
+ controller.model => ComponentSchema.new(controller: controller).create
34
+ }
35
+ end
36
+ end
23
37
  end
24
38
  end
@@ -21,4 +21,4 @@ module SwaggerApi
21
21
  end
22
22
  end
23
23
  end
24
- end
24
+ end
@@ -0,0 +1,30 @@
1
+ module SwaggerApi
2
+ module Concerns
3
+ module StiSchema
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ def is_sti?(model)
8
+ model_klass = model&.safe_constantize || model
9
+ return false if model_klass.is_a? String
10
+ Rails.application.eager_load!
11
+ model_klass.descendants.count != 0
12
+ end
13
+
14
+ def schema(model)
15
+ if is_sti?(model)
16
+ {
17
+ oneOf: model.descendants.map do |klass|
18
+ { '$ref' => "#/components/schemas/#{klass.name}" }
19
+ end
20
+ }
21
+ else
22
+ {
23
+ '$ref' => "#/components/schemas/#{model.try(:name) || model}"
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -3,6 +3,7 @@ module SwaggerApi
3
3
  class Base
4
4
  include ActiveAttr::Model
5
5
  include SwaggerApi::Concerns::Columns
6
+ include SwaggerApi::Concerns::StiSchema
6
7
 
7
8
  attr_accessor :controller, :action
8
9
 
@@ -32,6 +33,12 @@ module SwaggerApi
32
33
  ]
33
34
  end
34
35
 
36
+ def request_body
37
+ {
38
+ '$ref' => "#/components/requestBodies/#{model_name}"
39
+ }
40
+ end
41
+
35
42
  def responses
36
43
  return @responses unless @responses.nil?
37
44
  @responses = success_response
@@ -47,9 +54,7 @@ module SwaggerApi
47
54
  description: "#{readable_action} #{model_name.downcase}'s information",
48
55
  content: {
49
56
  'application/json; charset=utf-8' => {
50
- schema: {
51
- '$ref' => "#/components/schemas/#{model_name}"
52
- }
57
+ schema: schema(model)
53
58
  }
54
59
  }
55
60
  }
@@ -8,12 +8,6 @@ module SwaggerApi
8
8
  create
9
9
  end
10
10
 
11
- def request_body
12
- {
13
- "$ref" => "#/components/requestBodies/#{model_name}"
14
- }
15
- end
16
-
17
11
  def success_response
18
12
  {
19
13
  '303' => {
@@ -7,12 +7,6 @@ module SwaggerApi
7
7
  create
8
8
  end
9
9
 
10
- def request_body
11
- {
12
- "$ref" => "#/components/requestBodies/#{model_name}"
13
- }
14
- end
15
-
16
10
  def success_response
17
11
  {
18
12
  '303' => {
@@ -1,6 +1,8 @@
1
1
  module SwaggerApi
2
2
  class RequestBodies
3
3
  include ActiveAttr::Model
4
+ include SwaggerApi::Concerns::StiSchema
5
+
4
6
  attr_accessor :controllers
5
7
 
6
8
  def create
@@ -15,12 +17,10 @@ module SwaggerApi
15
17
  {
16
18
  content: {
17
19
  'application/json' => {
18
- schema: {
19
- '$ref' => "#/components/schemas/#{controller.model}"
20
- }
20
+ schema: schema(controller.model.safe_constantize || controller.model)
21
21
  }
22
22
  },
23
- description: "audience attribute",
23
+ description: "#{controller.model} attribute",
24
24
  required: true
25
25
  }
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module SwaggerApi
2
- VERSION = "0.1.47"
2
+ VERSION = "0.1.48"
3
3
  end
data/lib/swagger_api.rb CHANGED
@@ -110,6 +110,7 @@ require 'swagger_api/railtie'
110
110
 
111
111
  require 'swagger_api/actions'
112
112
  require 'swagger_api/concerns/columns'
113
+ require 'swagger_api/concerns/sti_schema'
113
114
  require 'swagger_api/column_schema'
114
115
  require 'swagger_api/component_schema'
115
116
  require 'swagger_api/components'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.47
4
+ version: 0.1.48
5
5
  platform: ruby
6
6
  authors:
7
7
  - Full Measure Education
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-20 00:00:00.000000000 Z
11
+ date: 2018-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -121,6 +121,7 @@ files:
121
121
  - lib/swagger_api/component_schema.rb
122
122
  - lib/swagger_api/components.rb
123
123
  - lib/swagger_api/concerns/columns.rb
124
+ - lib/swagger_api/concerns/sti_schema.rb
124
125
  - lib/swagger_api/operations/base.rb
125
126
  - lib/swagger_api/operations/create.rb
126
127
  - lib/swagger_api/operations/delete.rb