jsonapi_swagger_helpers 0.3.0 → 0.4.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 +4 -4
- data/lib/jsonapi_swagger_helpers.rb +1 -0
- data/lib/jsonapi_swagger_helpers/configuration.rb +10 -8
- data/lib/jsonapi_swagger_helpers/create_action.rb +19 -17
- data/lib/jsonapi_swagger_helpers/destroy_action.rb +14 -12
- data/lib/jsonapi_swagger_helpers/errors.rb +22 -0
- data/lib/jsonapi_swagger_helpers/index_action.rb +31 -29
- data/lib/jsonapi_swagger_helpers/payload_definition.rb +45 -43
- data/lib/jsonapi_swagger_helpers/readable.rb +64 -62
- data/lib/jsonapi_swagger_helpers/show_action.rb +29 -27
- data/lib/jsonapi_swagger_helpers/update_action.rb +20 -18
- data/lib/jsonapi_swagger_helpers/util.rb +130 -128
- data/lib/jsonapi_swagger_helpers/version.rb +1 -1
- data/lib/jsonapi_swagger_helpers/writeable.rb +66 -64
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e5e96630c96f25da98ee7a9ff41551e282da46d
|
4
|
+
data.tar.gz: 144a9a7a03e9f768864958a9d0ae29a9410a533b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53242cf8560b129aa133be07917aa1cc38ecd15d7151a69d29e4d1d80118f63e90834c8d00310fd236754beb04088c17674c5c3b336982159d4f625c6bd9b350
|
7
|
+
data.tar.gz: 9b07e484928616360f8a1415c9ce7b650edf803628a208ac295d2a847a131241f8bf051217e84f66dfa3bb5e522d3b240c4dcb20059f34342cab9f54ba1a0f05
|
@@ -1,10 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
class Configuration
|
3
|
+
def type_mapping
|
4
|
+
@type_mapping ||= {
|
5
|
+
string: [String],
|
6
|
+
integer: [Integer, Bignum],
|
7
|
+
float: [Float],
|
8
|
+
boolean: [TrueClass, FalseClass]
|
9
|
+
}
|
10
|
+
end
|
9
11
|
end
|
10
12
|
end
|
@@ -1,25 +1,27 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
class CreateAction
|
3
|
+
include JsonapiSwaggerHelpers::Writeable
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def action_name
|
6
|
+
:create
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
def generate
|
10
|
+
_self = self
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
define_schema
|
13
|
+
@node.operation :post do
|
14
|
+
key :description, _self.description
|
15
|
+
key :operationId, _self.operation_id
|
16
|
+
key :tags, _self.all_tags
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
parameter do
|
19
|
+
key :name, :payload
|
20
|
+
key :in, :body
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
schema do
|
23
|
+
key :'$ref', :"#{_self.strong_resource.name}_create"
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -1,19 +1,21 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
class DestroyAction
|
3
|
+
include JsonapiSwaggerHelpers::Writeable
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def action_name
|
6
|
+
:destroy
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
def generate
|
10
|
+
_self = self
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
@node.operation :delete do
|
13
|
+
key :description, _self.description
|
14
|
+
key :operationId, _self.operation_id
|
15
|
+
key :tags, _self.tags
|
15
16
|
|
16
|
-
|
17
|
+
_self.util.id_in_url(self)
|
18
|
+
end
|
17
19
|
end
|
18
20
|
end
|
19
21
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
module Errors
|
3
|
+
class TypeNotFound < StandardError
|
4
|
+
def initialize(payload_name, attribute)
|
5
|
+
@payload_name = payload_name
|
6
|
+
@attribute = attribute
|
7
|
+
end
|
8
|
+
|
9
|
+
def message
|
10
|
+
<<-STR
|
11
|
+
Could not find type mapping for payload "#{@payload_name}", key "#{@attribute}".
|
12
|
+
|
13
|
+
To add a custom mapping:
|
14
|
+
|
15
|
+
JsonapiSwaggerHelpers.config do |c|
|
16
|
+
c.type_mapping[:string] << MyCustomType
|
17
|
+
end
|
18
|
+
STR
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,42 +1,44 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
class IndexAction
|
3
|
+
include JsonapiSwaggerHelpers::Readable
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def action_name
|
6
|
+
:index
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
def generate
|
10
|
+
_self = self
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
@node.operation :get do
|
13
|
+
key :description, _self.full_description
|
14
|
+
key :operationId, _self.operation_id
|
15
|
+
key :tags, _self.all_tags
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
_self.util.jsonapi_sorting(self)
|
18
|
+
_self.util.jsonapi_pagination(self)
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
_self.util.each_filter(_self.resource) do |filter_label|
|
21
|
+
_self.util.jsonapi_filter(self, filter_label)
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
_self.each_stat do |stat_name, calculations|
|
25
|
+
_self.util.jsonapi_stat(self, stat_name, calculations)
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
+
_self.util.jsonapi_fields(self, _self.jsonapi_type)
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
if _self.has_extra_fields?
|
31
|
+
_self.util.jsonapi_extra_fields(self, _self.resource)
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
34
|
+
if _self.has_sideloads?
|
35
|
+
_self.util.jsonapi_includes(self)
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
_self.each_association do |association_name, association_resource|
|
38
|
+
_self.util.each_filter(association_resource, association_name) do |filter_label|
|
39
|
+
_self.util.jsonapi_filter(self, filter_label)
|
40
|
+
_self.util.jsonapi_fields(self, association_resource.config[:type])
|
41
|
+
end
|
40
42
|
end
|
41
43
|
end
|
42
44
|
end
|
@@ -1,55 +1,57 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
class PayloadDefinition
|
3
|
+
attr_reader :payload
|
4
|
+
|
5
|
+
# Given a spec payload like:
|
6
|
+
#
|
7
|
+
# key(:name, String)
|
8
|
+
#
|
9
|
+
# Return the corresponding swagger type, ie :string
|
10
|
+
# If a key has multiple types, we'll pick the first swagger type that matches:
|
11
|
+
#
|
12
|
+
# key(:total, [String, Integer]) => :string
|
13
|
+
def self.swagger_type_for(payload_name, attribute, type)
|
14
|
+
types = Array(type)
|
15
|
+
return :string if types.empty?
|
16
|
+
|
17
|
+
type_mapping.each_pair do |swagger_type, klasses|
|
18
|
+
if types.any? { |t| klasses.include?(t) }
|
19
|
+
return swagger_type
|
20
|
+
end
|
19
21
|
end
|
20
|
-
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
raise JsonapiSwaggerHelpers::Errors::TypeNotFound
|
24
|
+
.new(payload_name, attribute)
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def self.type_mapping
|
28
|
+
JsonapiSwaggerHelpers.config.type_mapping
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def initialize(payload)
|
32
|
+
@payload = payload
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
def context
|
36
|
+
JsonapiSwaggerHelpers.docs_controller
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
39
|
+
def jsonapi_type
|
40
|
+
payload.type
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
43
|
+
def generate
|
44
|
+
_self = self
|
44
45
|
|
45
|
-
|
46
|
-
|
46
|
+
context.send(:swagger_schema, payload.name) do
|
47
|
+
payload = _self.payload
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
payload.keys.each_pair do |attribute, config|
|
50
|
+
property attribute do
|
51
|
+
type = _self.class.swagger_type_for(payload.name, attribute, config[:type])
|
52
|
+
key :type, type
|
53
|
+
key :description, config[:description]
|
54
|
+
end
|
53
55
|
end
|
54
56
|
end
|
55
57
|
end
|
@@ -1,83 +1,85 @@
|
|
1
|
-
module JsonapiSwaggerHelpers
|
2
|
-
|
3
|
-
klass
|
4
|
-
|
5
|
-
:
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
module Readable
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
attr_reader :node,
|
6
|
+
:controller,
|
7
|
+
:resource,
|
8
|
+
:description,
|
9
|
+
:tags
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
def initialize(node, controller, description: nil, tags: [])
|
14
|
+
@node = node
|
15
|
+
@controller = controller
|
16
|
+
@resource = controller._jsonapi_compliable
|
17
|
+
@description = description || default_description
|
18
|
+
@tags = tags
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
def default_description
|
22
|
+
"#{action_name.capitalize} Action"
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
def operation_id
|
26
|
+
"#{controller.name.gsub('::', '-')}-#{action_name}"
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
def util
|
30
|
+
JsonapiSwaggerHelpers::Util
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
def include_directive
|
34
|
+
util.include_directive_for(controller, action_name)
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
def has_sideloads?
|
38
|
+
include_directive.keys.length > 0
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
def has_extra_fields?
|
42
|
+
resource.config[:extra_fields].keys.length > 1
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
def full_description
|
46
|
+
"#{description}<br /><br />#{util.sideload_label(include_directive)}"
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
def all_tags
|
50
|
+
tags + payload_tags
|
51
|
+
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
def payload_tags
|
54
|
+
util.payload_tags_for(resource, include_directive.to_hash)
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
def operation_id
|
58
|
+
"#{controller.name.gsub('::', '-')}-#{action_name}"
|
59
|
+
end
|
59
60
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
def each_stat
|
62
|
+
resource.config[:stats].each_pair do |stat_name, opts|
|
63
|
+
calculations = opts.calculations.keys - [:keys]
|
64
|
+
calculations = calculations.join(', ')
|
64
65
|
|
65
|
-
|
66
|
+
yield stat_name, calculations
|
67
|
+
end
|
66
68
|
end
|
67
|
-
end
|
68
69
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
70
|
+
def each_association
|
71
|
+
resource_map = util.all_resources(resource, include_directive)
|
72
|
+
resource_map.each_pair do |association_name, association_resource|
|
73
|
+
yield association_name, association_resource
|
74
|
+
end
|
73
75
|
end
|
74
|
-
end
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
77
|
+
def jsonapi_type
|
78
|
+
resource.config[:type]
|
79
|
+
end
|
79
80
|
|
80
|
-
|
81
|
-
|
81
|
+
def generate
|
82
|
+
raise 'override me'
|
83
|
+
end
|
82
84
|
end
|
83
85
|
end
|
@@ -1,39 +1,41 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
class ShowAction
|
3
|
+
include JsonapiSwaggerHelpers::Readable
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def action_name
|
6
|
+
:show
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
def generate
|
10
|
+
_self = self
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
@node.operation :get do
|
13
|
+
key :description, _self.full_description
|
14
|
+
key :operationId, _self.operation_id
|
15
|
+
key :tags, _self.all_tags
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
_self.util.id_in_url(self)
|
18
|
+
_self.util.jsonapi_fields(self, _self.jsonapi_type)
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
if _self.has_extra_fields?
|
21
|
+
_self.util.jsonapi_extra_fields(self, _self.resource)
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
_self.each_stat do |stat_name, calculations|
|
25
|
+
_self.util.jsonapi_stat(self, stat_name, calculations)
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
if _self.has_sideloads?
|
29
|
+
_self.util.jsonapi_includes(self)
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
_self.each_association do |association_name, association_resource|
|
32
|
+
_self.util.each_filter(association_resource, association_name) do |filter_label|
|
33
|
+
_self.util.jsonapi_filter(self, filter_label)
|
34
|
+
_self.util.jsonapi_fields(self, association_resource.config[:type])
|
34
35
|
|
35
|
-
|
36
|
-
|
36
|
+
if association_resource.config[:extra_fields].keys.length > 0
|
37
|
+
_self.util.jsonapi_extra_fields(self, association_resource)
|
38
|
+
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
@@ -1,27 +1,29 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
class UpdateAction
|
3
|
+
include JsonapiSwaggerHelpers::Writeable
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def action_name
|
6
|
+
:update
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
def generate
|
10
|
+
_self = self
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
define_schema
|
13
|
+
@node.operation :put do
|
14
|
+
key :description, _self.description
|
15
|
+
key :operationId, _self.operation_id
|
16
|
+
key :tags, _self.all_tags
|
16
17
|
|
17
|
-
|
18
|
+
_self.util.id_in_url(self)
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
parameter do
|
21
|
+
key :name, :payload
|
22
|
+
key :in, :body
|
22
23
|
|
23
|
-
|
24
|
-
|
24
|
+
schema do
|
25
|
+
key :'$ref', :"#{_self.strong_resource.name}_update"
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
27
29
|
end
|
@@ -1,165 +1,167 @@
|
|
1
|
-
module JsonapiSwaggerHelpers
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
class Util
|
3
|
+
def self.controller_for(path)
|
4
|
+
path = path.sub('{id}', '1')
|
5
|
+
route = Rails.application.routes.recognize_path(path)
|
6
|
+
"#{route[:controller]}_controller".classify.constantize
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
def self.sideload_label(include_directive)
|
10
|
+
sideloads = include_directive.to_string.split(",").sort.join(",<br/>")
|
10
11
|
|
11
|
-
|
12
|
-
<label>
|
13
|
-
|
14
|
-
|
15
|
-
</label>
|
16
|
-
|
17
|
-
|
12
|
+
<<-HTML
|
13
|
+
<label>
|
14
|
+
Possible sideloads:
|
15
|
+
<span class="possible-sideloads">#{sideloads}</span>
|
16
|
+
</label>
|
17
|
+
HTML
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
def self.each_filter(resource, association_name = nil)
|
21
|
+
resource.config[:filters].each_pair do |filter_name, opts|
|
22
|
+
if association_name
|
23
|
+
yield "filter[#{association_name}][#{filter_name}]"
|
24
|
+
else
|
25
|
+
yield "filter[#{filter_name}]"
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
27
|
-
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
def self.all_resources(resource, include_directive, memo = {})
|
31
|
+
resource.sideloading.sideloads.each_pair do |name, sideload|
|
32
|
+
next if memo[name] || !include_directive.key?(name)
|
32
33
|
|
33
|
-
|
34
|
-
|
34
|
+
memo[name] = sideload.resource.class
|
35
|
+
all_resources(sideload.resource.class, include_directive[name], memo)
|
36
|
+
end
|
37
|
+
memo
|
35
38
|
end
|
36
|
-
memo
|
37
|
-
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
def self.include_directive_for(controller, action)
|
41
|
+
resource_class = controller._jsonapi_compliable
|
42
|
+
includes = resource_class.sideloading.to_hash[:base]
|
43
|
+
whitelist = resource_class.config[:sideload_whitelist]
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
if whitelist && whitelist[action]
|
46
|
+
includes = JsonapiCompliable::Util::IncludeParams
|
47
|
+
.scrub(includes, whitelist[action])
|
48
|
+
end
|
48
49
|
|
49
|
-
|
50
|
-
|
50
|
+
JSONAPI::IncludeDirective.new(includes)
|
51
|
+
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
def self.payloads_for(resource, include_hash)
|
54
|
+
[].tap do |payloads|
|
55
|
+
payloads << JsonapiSpecHelpers::Payload.by_type(resource.config[:type])
|
55
56
|
|
56
|
-
|
57
|
-
|
57
|
+
include_hash.each_pair do |name, nested|
|
58
|
+
sideload = resource.sideloading.sideloads[name]
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
if sideload.polymorphic?
|
61
|
+
sideload.polymorphic_groups.each_pair do |type, sl|
|
62
|
+
payloads << payloads_for(sl.resource_class, nested)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
sideload_resource = sideload.resource_class
|
66
|
+
payloads << payloads_for(sideload_resource, nested)
|
62
67
|
end
|
63
|
-
else
|
64
|
-
sideload_resource = sideload.resource_class
|
65
|
-
payloads << payloads_for(sideload_resource, nested)
|
66
68
|
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.payload_tags_for(resource, include_hash)
|
72
|
-
payloads_for(resource, include_hash).map { |p| "payload-#{p.name}" }
|
73
|
-
end
|
69
|
+
end.flatten.uniq(&:name)
|
70
|
+
end
|
74
71
|
|
75
|
-
|
76
|
-
|
77
|
-
key :description, '<a href="http://jsonapi.org/format/#fetching-filtering">JSONAPI Filter</a>'
|
78
|
-
key :name, label
|
79
|
-
key :in, :query
|
80
|
-
key :type, :string
|
81
|
-
key :required, false
|
72
|
+
def self.payload_tags_for(resource, include_hash)
|
73
|
+
payloads_for(resource, include_hash).map { |p| "payload-#{p.name}" }
|
82
74
|
end
|
83
|
-
end
|
84
75
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
76
|
+
def self.jsonapi_filter(node, label)
|
77
|
+
node.parameter do
|
78
|
+
key :description, '<a href="http://jsonapi.org/format/#fetching-filtering">JSONAPI Filter</a>'
|
79
|
+
key :name, label
|
80
|
+
key :in, :query
|
81
|
+
key :type, :string
|
82
|
+
key :required, false
|
83
|
+
end
|
92
84
|
end
|
93
|
-
end
|
94
85
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
86
|
+
def self.jsonapi_sorting(node)
|
87
|
+
node.parameter do
|
88
|
+
key :description, '<a href="http://jsonapi.org/format/#fetching-sorting">JSONAPI Sorting</a>'
|
89
|
+
key :name, :sort
|
90
|
+
key :in, :query
|
91
|
+
key :type, :string
|
92
|
+
key :required, false
|
93
|
+
end
|
102
94
|
end
|
103
95
|
|
104
|
-
node
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
96
|
+
def self.jsonapi_pagination(node)
|
97
|
+
node.parameter do
|
98
|
+
key :description, '<a href="http://jsonapi.org/format/#fetching-pagination">JSONAPI Page Size</a>'
|
99
|
+
key :name, "page[size]"
|
100
|
+
key :in, :query
|
101
|
+
key :type, :string
|
102
|
+
key :required, false
|
103
|
+
end
|
104
|
+
|
105
|
+
node.parameter do
|
106
|
+
key :description, '<a href="http://jsonapi.org/format/#fetching-pagination">JSONAPI Page Number</a>'
|
107
|
+
key :name, "page[number]"
|
108
|
+
key :in, :query
|
109
|
+
key :type, :string
|
110
|
+
key :required, false
|
111
|
+
end
|
110
112
|
end
|
111
|
-
end
|
112
113
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
114
|
+
def self.jsonapi_stat(node, name, calculations)
|
115
|
+
node.parameter do
|
116
|
+
key :name, "stats[#{name}]"
|
117
|
+
key :description, "<a href=\"https://jsonapi-suite.github.io/jsonapi_suite/how-to-return-statistics\">JSONAPI Stats</a><br /><b>Possible Calculations:</b> #{calculations}"
|
118
|
+
key :in, :query
|
119
|
+
key :type, :string
|
120
|
+
key :required, false
|
121
|
+
end
|
120
122
|
end
|
121
|
-
end
|
122
123
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
124
|
+
def self.jsonapi_includes(node)
|
125
|
+
node.parameter do
|
126
|
+
key :description, '<a href="http://jsonapi.org/format/#fetching-includes">JSONAPI Includes</a>'
|
127
|
+
key :name, :include
|
128
|
+
key :in, :query
|
129
|
+
key :type, :string
|
130
|
+
key :required, false
|
131
|
+
end
|
130
132
|
end
|
131
|
-
end
|
132
133
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
134
|
+
def self.jsonapi_fields(node, jsonapi_type)
|
135
|
+
node.parameter do
|
136
|
+
key :description, '<a href="http://jsonapi.org/format/#fetching-sparse-fieldsets">JSONAPI Sparse Fieldset</a>'
|
137
|
+
key :name, "fields[#{jsonapi_type}]"
|
138
|
+
key :in, :query
|
139
|
+
key :type, :string
|
140
|
+
key :required, false
|
141
|
+
end
|
140
142
|
end
|
141
|
-
end
|
142
143
|
|
143
|
-
|
144
|
-
|
144
|
+
def self.jsonapi_extra_fields(node, resource)
|
145
|
+
jsonapi_type = resource.config[:type]
|
145
146
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
147
|
+
extra_field_names = resource.config[:extra_fields].keys.join(',')
|
148
|
+
node.parameter do
|
149
|
+
key :description, "<a href=\"https://jsonapi-suite.github.io/jsonapi_suite/how-to-conditionally-render-fields\">JSONAPI Extra Fields</a><br /><b>Possible Fields:</b> #{extra_field_names}"
|
150
|
+
key :name, "extra_fields[#{jsonapi_type}]"
|
151
|
+
key :in, :query
|
152
|
+
key :type, :string
|
153
|
+
key :required, false
|
154
|
+
end
|
153
155
|
end
|
154
|
-
end
|
155
156
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
157
|
+
def self.id_in_url(node)
|
158
|
+
node.parameter do
|
159
|
+
key :name, :id
|
160
|
+
key :in, :path
|
161
|
+
key :type, :string
|
162
|
+
key :required, true
|
163
|
+
key :description, 'record id'
|
164
|
+
end
|
163
165
|
end
|
164
166
|
end
|
165
167
|
end
|
@@ -1,89 +1,91 @@
|
|
1
|
-
module JsonapiSwaggerHelpers
|
2
|
-
|
3
|
-
klass
|
4
|
-
|
5
|
-
:
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
module JsonapiSwaggerHelpers
|
2
|
+
module Writeable
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
attr_reader :node,
|
6
|
+
:controller,
|
7
|
+
:resource,
|
8
|
+
:description,
|
9
|
+
:tags
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def initialize(node, controller, description: nil, tags: [])
|
13
|
-
@node = node
|
14
|
-
@controller = controller
|
15
|
-
@resource = controller._jsonapi_compliable
|
16
|
-
@description = description || default_description
|
17
|
-
@tags = tags
|
18
|
-
end
|
19
12
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
13
|
+
def initialize(node, controller, description: nil, tags: [])
|
14
|
+
@node = node
|
15
|
+
@controller = controller
|
16
|
+
@resource = controller._jsonapi_compliable
|
17
|
+
@description = description || default_description
|
18
|
+
@tags = tags
|
19
|
+
end
|
27
20
|
|
28
|
-
|
29
|
-
|
30
|
-
|
21
|
+
def util
|
22
|
+
JsonapiSwaggerHelpers::Util
|
23
|
+
end
|
31
24
|
|
32
|
-
|
33
|
-
|
34
|
-
|
25
|
+
def action_name
|
26
|
+
raise 'override me'
|
27
|
+
end
|
35
28
|
|
36
|
-
|
37
|
-
|
38
|
-
|
29
|
+
def default_description
|
30
|
+
"#{action_name.to_s.capitalize} Action"
|
31
|
+
end
|
39
32
|
|
40
|
-
|
41
|
-
|
33
|
+
def operation_id
|
34
|
+
"#{controller.name.gsub('::', '-')}-#{action_name}"
|
35
|
+
end
|
42
36
|
|
43
|
-
|
44
|
-
tags
|
37
|
+
def all_tags
|
38
|
+
tags + payload_tags
|
45
39
|
end
|
46
40
|
|
47
|
-
|
48
|
-
|
41
|
+
def payload_tags
|
42
|
+
tags = [:"payload-#{strong_resource.name}_#{action_name}"]
|
49
43
|
|
50
|
-
|
51
|
-
|
52
|
-
|
44
|
+
strong_resource.relations.each_pair do |relation_name, relation_config|
|
45
|
+
tags << :"payload-#{strong_resource.name}_#{relation_name}_#{action_name}"
|
46
|
+
end
|
53
47
|
|
54
|
-
|
55
|
-
|
56
|
-
end
|
48
|
+
tags
|
49
|
+
end
|
57
50
|
|
58
|
-
|
59
|
-
|
60
|
-
context.send(:swagger_schema, :"#{strong_resource.name}_#{action_name}") do
|
61
|
-
_self.strong_resource.attributes.each_pair do |attribute, config|
|
62
|
-
property attribute do
|
63
|
-
key :type, config[:type] # TODO - swagger type?
|
64
|
-
end
|
65
|
-
end
|
51
|
+
def context
|
52
|
+
JsonapiSwaggerHelpers.docs_controller
|
66
53
|
end
|
67
54
|
|
68
|
-
|
69
|
-
|
70
|
-
|
55
|
+
def strong_resource
|
56
|
+
controller._strong_resources[action_name]
|
57
|
+
end
|
58
|
+
|
59
|
+
def define_schema
|
60
|
+
_self = self
|
61
|
+
context.send(:swagger_schema, :"#{strong_resource.name}_#{action_name}") do
|
62
|
+
_self.strong_resource.attributes.each_pair do |attribute, config|
|
71
63
|
property attribute do
|
72
64
|
key :type, config[:type] # TODO - swagger type?
|
73
65
|
end
|
74
66
|
end
|
75
67
|
end
|
68
|
+
|
69
|
+
_self.strong_resource.relations.each_pair do |relation_name, relation_config|
|
70
|
+
context.send(:swagger_schema, :"#{strong_resource.name}_#{relation_name}_#{action_name}") do
|
71
|
+
relation_config[:resource].attributes.each_pair do |attribute, config|
|
72
|
+
property attribute do
|
73
|
+
key :type, config[:type] # TODO - swagger type?
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
76
78
|
end
|
77
|
-
end
|
78
79
|
|
79
|
-
|
80
|
-
|
80
|
+
def generate
|
81
|
+
_self = self
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
83
|
+
define_schema
|
84
|
+
@node.operation :post do
|
85
|
+
key :description, _self.description
|
86
|
+
key :operationId, _self.operation_id
|
87
|
+
key :tags, _self.all_tags
|
88
|
+
end
|
87
89
|
end
|
88
90
|
end
|
89
91
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi_swagger_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lee Richmond
|
@@ -102,6 +102,7 @@ files:
|
|
102
102
|
- lib/jsonapi_swagger_helpers/create_action.rb
|
103
103
|
- lib/jsonapi_swagger_helpers/destroy_action.rb
|
104
104
|
- lib/jsonapi_swagger_helpers/docs_controller_mixin.rb
|
105
|
+
- lib/jsonapi_swagger_helpers/errors.rb
|
105
106
|
- lib/jsonapi_swagger_helpers/index_action.rb
|
106
107
|
- lib/jsonapi_swagger_helpers/payload_definition.rb
|
107
108
|
- lib/jsonapi_swagger_helpers/readable.rb
|