sober_swag 0.18.0 → 0.22.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +15 -0
- data/.github/workflows/benchmark.yml +39 -0
- data/.github/workflows/lint.yml +2 -4
- data/.github/workflows/ruby.yml +1 -1
- data/.gitignore +3 -0
- data/.rubocop.yml +6 -1
- data/.yardopts +7 -0
- data/CHANGELOG.md +22 -0
- data/Gemfile +12 -0
- data/README.md +1 -1
- data/bench/benchmark.rb +34 -0
- data/bench/benchmarks/basic_field_serializer.rb +21 -0
- data/bench/benchmarks/view_selection.rb +47 -0
- data/bin/console +30 -10
- data/docs/reporting.md +190 -0
- data/docs/serializers.md +4 -1
- data/example/Gemfile +2 -2
- data/example/Gemfile.lock +116 -123
- data/example/app/controllers/application_controller.rb +4 -0
- data/example/app/controllers/people_controller.rb +44 -28
- data/example/app/output_objects/identified_output.rb +7 -0
- data/example/app/output_objects/person_output_object.rb +37 -11
- data/example/app/output_objects/post_output_object.rb +0 -4
- data/example/app/output_objects/reporting_post_output.rb +18 -0
- data/example/bin/rspec +29 -0
- data/example/config/environments/production.rb +1 -1
- data/example/spec/requests/people/create_spec.rb +3 -2
- data/example/spec/requests/people/index_spec.rb +1 -1
- data/lib/sober_swag/compiler/path.rb +45 -4
- data/lib/sober_swag/compiler/paths.rb +20 -0
- data/lib/sober_swag/compiler/primitive.rb +17 -0
- data/lib/sober_swag/compiler/type.rb +105 -22
- data/lib/sober_swag/compiler.rb +87 -15
- data/lib/sober_swag/controller/route.rb +147 -28
- data/lib/sober_swag/controller.rb +57 -17
- data/lib/sober_swag/input_object.rb +124 -7
- data/lib/sober_swag/nodes/array.rb +19 -0
- data/lib/sober_swag/nodes/attribute.rb +45 -4
- data/lib/sober_swag/nodes/base.rb +27 -7
- data/lib/sober_swag/nodes/binary.rb +30 -13
- data/lib/sober_swag/nodes/enum.rb +16 -1
- data/lib/sober_swag/nodes/list.rb +20 -0
- data/lib/sober_swag/nodes/nullable_primitive.rb +3 -0
- data/lib/sober_swag/nodes/object.rb +4 -1
- data/lib/sober_swag/nodes/one_of.rb +11 -3
- data/lib/sober_swag/nodes/primitive.rb +34 -2
- data/lib/sober_swag/nodes/sum.rb +8 -0
- data/lib/sober_swag/output_object/definition.rb +57 -1
- data/lib/sober_swag/output_object/field.rb +31 -11
- data/lib/sober_swag/output_object/field_syntax.rb +19 -3
- data/lib/sober_swag/output_object/view.rb +46 -1
- data/lib/sober_swag/output_object.rb +40 -19
- data/lib/sober_swag/parser.rb +7 -1
- data/lib/sober_swag/reporting/compiler.rb +39 -0
- data/lib/sober_swag/reporting/input/base.rb +11 -0
- data/lib/sober_swag/reporting/input/bool.rb +19 -0
- data/lib/sober_swag/reporting/input/converting/bool.rb +24 -0
- data/lib/sober_swag/reporting/input/converting/date.rb +30 -0
- data/lib/sober_swag/reporting/input/converting/date_time.rb +28 -0
- data/lib/sober_swag/reporting/input/converting/decimal.rb +24 -0
- data/lib/sober_swag/reporting/input/converting/integer.rb +19 -0
- data/lib/sober_swag/reporting/input/converting.rb +16 -0
- data/lib/sober_swag/reporting/input/defer.rb +29 -0
- data/lib/sober_swag/reporting/input/described.rb +38 -0
- data/lib/sober_swag/reporting/input/dictionary.rb +37 -0
- data/lib/sober_swag/reporting/input/either.rb +51 -0
- data/lib/sober_swag/reporting/input/enum.rb +44 -0
- data/lib/sober_swag/reporting/input/format.rb +39 -0
- data/lib/sober_swag/reporting/input/interface.rb +87 -0
- data/lib/sober_swag/reporting/input/list.rb +44 -0
- data/lib/sober_swag/reporting/input/mapped.rb +36 -0
- data/lib/sober_swag/reporting/input/merge_objects.rb +72 -0
- data/lib/sober_swag/reporting/input/null.rb +34 -0
- data/lib/sober_swag/reporting/input/number.rb +19 -0
- data/lib/sober_swag/reporting/input/object/property.rb +53 -0
- data/lib/sober_swag/reporting/input/object.rb +100 -0
- data/lib/sober_swag/reporting/input/pattern.rb +46 -0
- data/lib/sober_swag/reporting/input/referenced.rb +38 -0
- data/lib/sober_swag/reporting/input/struct.rb +271 -0
- data/lib/sober_swag/reporting/input/text.rb +42 -0
- data/lib/sober_swag/reporting/input.rb +54 -0
- data/lib/sober_swag/reporting/invalid_schema_error.rb +21 -0
- data/lib/sober_swag/reporting/output/base.rb +25 -0
- data/lib/sober_swag/reporting/output/bool.rb +25 -0
- data/lib/sober_swag/reporting/output/defer.rb +69 -0
- data/lib/sober_swag/reporting/output/described.rb +42 -0
- data/lib/sober_swag/reporting/output/dictionary.rb +46 -0
- data/lib/sober_swag/reporting/output/interface.rb +83 -0
- data/lib/sober_swag/reporting/output/list.rb +54 -0
- data/lib/sober_swag/reporting/output/merge_objects.rb +97 -0
- data/lib/sober_swag/reporting/output/null.rb +25 -0
- data/lib/sober_swag/reporting/output/number.rb +25 -0
- data/lib/sober_swag/reporting/output/object/property.rb +45 -0
- data/lib/sober_swag/reporting/output/object.rb +54 -0
- data/lib/sober_swag/reporting/output/partitioned.rb +77 -0
- data/lib/sober_swag/reporting/output/pattern.rb +50 -0
- data/lib/sober_swag/reporting/output/referenced.rb +42 -0
- data/lib/sober_swag/reporting/output/struct.rb +262 -0
- data/lib/sober_swag/reporting/output/text.rb +25 -0
- data/lib/sober_swag/reporting/output/via_map.rb +67 -0
- data/lib/sober_swag/reporting/output/viewed.rb +72 -0
- data/lib/sober_swag/reporting/output.rb +54 -0
- data/lib/sober_swag/reporting/report/base.rb +57 -0
- data/lib/sober_swag/reporting/report/either.rb +36 -0
- data/lib/sober_swag/reporting/report/error.rb +15 -0
- data/lib/sober_swag/reporting/report/list.rb +28 -0
- data/lib/sober_swag/reporting/report/merged_object.rb +25 -0
- data/lib/sober_swag/reporting/report/object.rb +29 -0
- data/lib/sober_swag/reporting/report/output.rb +14 -0
- data/lib/sober_swag/reporting/report/value.rb +28 -0
- data/lib/sober_swag/reporting/report.rb +16 -0
- data/lib/sober_swag/reporting.rb +11 -0
- data/lib/sober_swag/serializer/array.rb +27 -3
- data/lib/sober_swag/serializer/base.rb +75 -25
- data/lib/sober_swag/serializer/conditional.rb +33 -1
- data/lib/sober_swag/serializer/field_list.rb +23 -5
- data/lib/sober_swag/serializer/hash.rb +53 -0
- data/lib/sober_swag/serializer/mapped.rb +10 -1
- data/lib/sober_swag/serializer/optional.rb +18 -1
- data/lib/sober_swag/serializer/primitive.rb +3 -0
- data/lib/sober_swag/serializer.rb +1 -0
- data/lib/sober_swag/server.rb +27 -11
- data/lib/sober_swag/type/named.rb +14 -0
- data/lib/sober_swag/types/comma_array.rb +4 -0
- data/lib/sober_swag/version.rb +1 -1
- data/lib/sober_swag.rb +7 -1
- metadata +72 -2
@@ -2,7 +2,13 @@ module SoberSwag
|
|
2
2
|
module Controller
|
3
3
|
##
|
4
4
|
# Describe a single controller endpoint.
|
5
|
-
class Route
|
5
|
+
class Route # rubocop:disable Metrics/ClassLength
|
6
|
+
##
|
7
|
+
# @param method [Symbol] the HTTP method to get
|
8
|
+
# @param action_name [Symbol] the name of the rails action
|
9
|
+
# (the name of the controller method, usually)
|
10
|
+
# @param path [String] an OpenAPI V3 path template,
|
11
|
+
# which should [match this format](https://swagger.io/docs/specification/describing-parameters/#path-parameters)
|
6
12
|
def initialize(method, action_name, path)
|
7
13
|
@method = method
|
8
14
|
@path = path
|
@@ -12,20 +18,56 @@ module SoberSwag
|
|
12
18
|
@tags = []
|
13
19
|
end
|
14
20
|
|
15
|
-
|
21
|
+
##
|
22
|
+
# A hash of response code -> response serializer
|
23
|
+
# @return [Hash{Symbol => SoberSwag::Serializer::Base}]
|
24
|
+
# response code to response serializer
|
25
|
+
attr_reader :response_serializers
|
26
|
+
|
27
|
+
##
|
28
|
+
# A hash of response code -> response description
|
29
|
+
# @return [Hash{Symbol => String}]
|
30
|
+
# response code to response description
|
31
|
+
attr_reader :response_descriptions
|
32
|
+
|
33
|
+
##
|
34
|
+
# The HTTP method of this route.
|
35
|
+
# @return [Symbol]
|
36
|
+
attr_reader :method
|
37
|
+
|
38
|
+
##
|
39
|
+
# The swagger path specifier of this route.
|
40
|
+
# @return [String]
|
41
|
+
attr_reader :path
|
42
|
+
|
43
|
+
##
|
44
|
+
# The name of the rails action (usually the controller method) of this route.
|
45
|
+
# @return [Symbol]
|
46
|
+
attr_reader :action_name
|
16
47
|
|
17
48
|
##
|
18
49
|
# What to parse the request body into.
|
50
|
+
# @return [Class] a swagger-able class type for a request body.
|
19
51
|
attr_reader :request_body_class
|
20
52
|
##
|
21
53
|
# What to parse the request query_params into.
|
54
|
+
# @return [Class] a swagger-able class type for query parameters.
|
22
55
|
attr_reader :query_params_class
|
23
56
|
##
|
24
57
|
# What to parse the path params into.
|
58
|
+
# @return [Class] a swagger-able class type for path parameters.
|
25
59
|
attr_reader :path_params_class
|
26
60
|
|
27
61
|
##
|
28
62
|
# Standard swagger tags.
|
63
|
+
#
|
64
|
+
# @overload tags()
|
65
|
+
# Get the tags for this route.
|
66
|
+
# @return [Array<String,Symbol>] the tags.
|
67
|
+
# @overload tags(*args)
|
68
|
+
# Set the tags for this route.
|
69
|
+
# @param tags [Array<String,Symbol>] the tags to set
|
70
|
+
# @return [Array<String,Symbol>] the tags used
|
29
71
|
def tags(*args)
|
30
72
|
return @tags if args.empty?
|
31
73
|
|
@@ -34,10 +76,18 @@ module SoberSwag
|
|
34
76
|
|
35
77
|
##
|
36
78
|
# Define the request body, using SoberSwag's type-definition scheme.
|
37
|
-
# The block passed will be used to define the body of a new
|
38
|
-
#
|
39
|
-
|
40
|
-
|
79
|
+
# The block passed will be used to define the body of a new subclass of `base` (defaulted to {SoberSwag::InputObject}.)
|
80
|
+
# @overload request_body(base)
|
81
|
+
# Give a Swagger-able type that will be used to parse the request body, and used in generated docs.
|
82
|
+
# @param base [Class] a swagger-able class
|
83
|
+
# @overload request_body(base = SoberSwag::InputObject, &block)
|
84
|
+
# Define a Swagger-able type inline to use to parse the request body.
|
85
|
+
# @see SoberSwag.input_object
|
86
|
+
# @overload request_body(base = SoberSwag::Reporting::Input::Struct, reporting: true, &block)
|
87
|
+
# Define a swagger-able type inline, using the new reporting system.
|
88
|
+
# @see SoberSwag::Reporting::Input::Struct
|
89
|
+
def request_body(base = SoberSwag::InputObject, reporting: false, &block)
|
90
|
+
@request_body_class = make_input_object!(base, reporting: reporting, &block)
|
41
91
|
action_module.const_set('RequestBody', @request_body_class)
|
42
92
|
end
|
43
93
|
|
@@ -48,11 +98,17 @@ module SoberSwag
|
|
48
98
|
end
|
49
99
|
|
50
100
|
##
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
|
55
|
-
|
101
|
+
# @overload query_params(base)
|
102
|
+
# Give a Swagger-able type that will be used to parse the query params, and used in generated docs.
|
103
|
+
# @param base [Class] a swagger-able class
|
104
|
+
# @overload query_params(base = SoberSwag::InputObject, &block)
|
105
|
+
# Define a Swagger-able type inline to use to parse the query params.
|
106
|
+
# @see SoberSwag.input_object
|
107
|
+
# @overload query_params(base = SoberSwag::Reporting::Input::Struct, reporting: true, &block)
|
108
|
+
# Define a swagger-able type inline, using the new reporting system.
|
109
|
+
# @see SoberSwag::Reporting::Input::Struct
|
110
|
+
def query_params(base = SoberSwag::InputObject, reporting: false, &block)
|
111
|
+
@query_params_class = make_input_object!(base, reporting: reporting, &block)
|
56
112
|
action_module.const_set('QueryParams', @query_params_class)
|
57
113
|
end
|
58
114
|
|
@@ -63,11 +119,17 @@ module SoberSwag
|
|
63
119
|
end
|
64
120
|
|
65
121
|
##
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
|
70
|
-
|
122
|
+
# @overload path_params(base)
|
123
|
+
# Give a Swagger-able type that will be used to parse the path params, and used in generated docs.
|
124
|
+
# @param base [Class] a swagger-able class
|
125
|
+
# @overload path_params(base = SoberSwag::InputObject, &block)
|
126
|
+
# Define a Swagger-able type inline to use to parse the path params.
|
127
|
+
# @see SoberSwag.input_object
|
128
|
+
# @overload path_params(base = SoberSwag::Reporting::Input::Struct, reporting: true, &block)
|
129
|
+
# Define a swagger-able type inline, using the new reporting system.
|
130
|
+
# @see SoberSwag::Reporting::Input::Struct
|
131
|
+
def path_params(base = SoberSwag::InputObject, reporting: false, &block)
|
132
|
+
@path_params_class = make_input_object!(base, reporting: reporting, &block)
|
71
133
|
action_module.const_set('PathParams', @path_params_class)
|
72
134
|
end
|
73
135
|
|
@@ -78,19 +140,27 @@ module SoberSwag
|
|
78
140
|
end
|
79
141
|
|
80
142
|
##
|
81
|
-
#
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
143
|
+
# @overload description()
|
144
|
+
# Get a description of this route object.
|
145
|
+
# @return [String] markdown-formatted description
|
146
|
+
# @overload description(desc)
|
147
|
+
# Set the description of this route object.
|
148
|
+
# @param desc [String] markdown-formatted description
|
149
|
+
# @return [String] `desc`.
|
88
150
|
def description(desc = nil)
|
89
151
|
return @description if desc.nil?
|
90
152
|
|
91
153
|
@description = desc
|
92
154
|
end
|
93
155
|
|
156
|
+
##
|
157
|
+
# @overload summary()
|
158
|
+
# Get the summary of this route object, a short string that identifies
|
159
|
+
# what it does.
|
160
|
+
# @return [String] markdown-formatted summary
|
161
|
+
# @overload summary(sum)
|
162
|
+
# Set a short, markdown-formatted summary of what this route does.
|
163
|
+
# @param sum [String] markdown-formatted summary
|
94
164
|
def summary(sum = nil)
|
95
165
|
return @summary if sum.nil?
|
96
166
|
|
@@ -100,14 +170,35 @@ module SoberSwag
|
|
100
170
|
##
|
101
171
|
# The container module for all the constants this will eventually define.
|
102
172
|
# Each class generated by this Route will be defined within this module.
|
173
|
+
# @return [Module] the module under which constants will be defined.
|
103
174
|
def action_module
|
104
175
|
@action_module ||= Module.new
|
105
176
|
end
|
106
177
|
|
107
178
|
##
|
108
|
-
#
|
109
|
-
#
|
110
|
-
# {SoberSwag::OutputObject.define}
|
179
|
+
# @overload response(status_code, description, &block)
|
180
|
+
# Define a new response from this route, by defining a serializer inline.
|
181
|
+
# This serializer will be defined as if with {SoberSwag::OutputObject.define}
|
182
|
+
#
|
183
|
+
# Generally, you want to define your serializers elsewhere for independent testing and such.
|
184
|
+
# However, if you have a really quick thing to serialize, this works.
|
185
|
+
# @param status_code [Symbol]
|
186
|
+
# the name of the HTTP status of this response.
|
187
|
+
# @param description [String]
|
188
|
+
# a description of what this response is, markdown-formatted
|
189
|
+
# @param block [Proc]
|
190
|
+
# passed to {SoberSwag::OutputObject.define}
|
191
|
+
#
|
192
|
+
# @overload response(status_code, description, serializer)
|
193
|
+
# Define a new response from this route, with an existing serializer.
|
194
|
+
# The generated swagger will document this response's format using the serializer.
|
195
|
+
#
|
196
|
+
# @param status_code [Symbol]
|
197
|
+
# the name of the HTTP status of this response
|
198
|
+
# @param description [String]
|
199
|
+
# a description of what this response is, markdown-formatted
|
200
|
+
# @param serializer [SoberSwag::Serializer::Base] a serializer to use for the
|
201
|
+
# body of this response
|
111
202
|
def response(status_code, description, serializer = nil, &block)
|
112
203
|
status_key = Rack::Utils.status_code(status_code)
|
113
204
|
|
@@ -120,7 +211,8 @@ module SoberSwag
|
|
120
211
|
end
|
121
212
|
|
122
213
|
##
|
123
|
-
# What you should call the module of this action in your controller
|
214
|
+
# What you should call the module of this action in your controller.
|
215
|
+
# @return [String]
|
124
216
|
def action_module_name
|
125
217
|
action_name.to_s.classify
|
126
218
|
end
|
@@ -131,7 +223,34 @@ module SoberSwag
|
|
131
223
|
@response_module ||= Module.new.tap { |m| action_module.const_set(:Response, m) }
|
132
224
|
end
|
133
225
|
|
134
|
-
def make_input_object!(base, &block)
|
226
|
+
def make_input_object!(base, reporting:, &block)
|
227
|
+
if reporting
|
228
|
+
make_reporting_input!(
|
229
|
+
base == SoberSwag::InputObject ? SoberSwag::Reporting::Input::Struct : base,
|
230
|
+
&block
|
231
|
+
)
|
232
|
+
else
|
233
|
+
make_non_reporting_input!(base, &block)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
def make_reporting_input!(base, &block)
|
238
|
+
if block
|
239
|
+
raise ArgumentError, 'non-class passed along with block' unless base.is_a?(Class)
|
240
|
+
|
241
|
+
make_reporting_input_struct!(base, &block)
|
242
|
+
else
|
243
|
+
base
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
def make_reporting_input_struct!(base, &block)
|
248
|
+
raise ArgumentError, 'base class must be a soberswag reporting class!' unless base <= SoberSwag::Reporting::Input::Struct
|
249
|
+
|
250
|
+
Class.new(base, &block)
|
251
|
+
end
|
252
|
+
|
253
|
+
def make_non_reporting_input!(base, &block)
|
135
254
|
if base.is_a?(Class)
|
136
255
|
make_input_class(base, block)
|
137
256
|
elsif block
|
@@ -2,7 +2,8 @@ require 'active_support/concern'
|
|
2
2
|
|
3
3
|
module SoberSwag
|
4
4
|
##
|
5
|
-
#
|
5
|
+
# This module can be included in any subclass of `ActionController` or `ActionController::API` to make it `SoberSwag`-able.
|
6
|
+
# This means that you can use the mechanics of SoberSwag to define a type-safe API, with generated Swagger documentation!
|
6
7
|
module Controller
|
7
8
|
extend ActiveSupport::Concern
|
8
9
|
|
@@ -17,14 +18,18 @@ module SoberSwag
|
|
17
18
|
include ::Dry::Types()
|
18
19
|
end
|
19
20
|
|
20
|
-
|
21
|
+
##
|
22
|
+
# Module containing class methods.
|
23
|
+
# Any class that `include`s {SoberSwag::Controller} will also `extend` {SoberSwag::Controller::ClassMethods}.
|
24
|
+
module ClassMethods
|
21
25
|
##
|
22
26
|
# Define a new action with the given HTTP method, action name, and path.
|
23
|
-
# This will
|
27
|
+
# This will eventually delegate to making an actual method on your controller,
|
24
28
|
# so you can use controllers as you wish with no harm.
|
25
29
|
#
|
26
30
|
# This method takes a block, evaluated in the context of a {SoberSwag::Controller::Route}.
|
27
31
|
# Used like:
|
32
|
+
#
|
28
33
|
# define(:get, :show, '/posts/{id}') do
|
29
34
|
# path_params do
|
30
35
|
# attribute :id, Types::Integer
|
@@ -36,32 +41,46 @@ module SoberSwag
|
|
36
41
|
# end
|
37
42
|
#
|
38
43
|
# This will define an "action module" on this class to contain the generated types.
|
39
|
-
# In the above example, the following constants will be
|
40
|
-
#
|
41
|
-
#
|
44
|
+
# In the above example, the following constants will be defined on the controller:
|
45
|
+
#
|
46
|
+
# - `PostsController::Show` - the container module for everything in this action
|
47
|
+
# - `PostsController::Show::PathParams` - the dry-struct type for the path attribute.
|
48
|
+
#
|
42
49
|
# So, in the same controller, you can refer to Show::PathParams to get the type created by the 'path_params' block above.
|
50
|
+
#
|
51
|
+
# The block given evaluates in the context of `SoberSwag::Controller::Route`.
|
52
|
+
#
|
53
|
+
# @todo Explore parsing the `path` parameter from rails routes so we can avoid forcing the duplicate boilerplate.
|
54
|
+
#
|
55
|
+
# @param method [Symbol] the HTTP method of this route
|
56
|
+
# @param action [Symbol] the name of the controller method this maps onto
|
57
|
+
# @param path [String] an OpenAPI v3 Path Specifier
|
43
58
|
def define(method, action, path, &block)
|
44
59
|
r = Route.new(method, action, path)
|
45
60
|
r.instance_eval(&block)
|
46
61
|
const_set(r.action_module_name, r.action_module)
|
47
62
|
defined_routes << r
|
48
|
-
define_method(action, r.action) if r.action
|
49
63
|
end
|
50
64
|
|
51
65
|
##
|
52
66
|
# All the routes that this controller knows about.
|
67
|
+
# @return [Array<SoberSwag::Controller::Route>
|
53
68
|
def defined_routes
|
54
69
|
@defined_routes ||= []
|
55
70
|
end
|
56
71
|
|
57
72
|
##
|
58
73
|
# Find a route with the given name.
|
74
|
+
# @param name [Symbol] the name
|
75
|
+
# @return [SoberSwag::Controller::Route]
|
59
76
|
def find_route(name)
|
60
77
|
defined_routes.find { |r| r.action_name.to_s == name.to_s }
|
61
78
|
end
|
62
79
|
|
63
80
|
##
|
64
|
-
#
|
81
|
+
# Get the OpenAPI v3 definition for this controller.
|
82
|
+
#
|
83
|
+
# @return [Hash]
|
65
84
|
def swagger_info
|
66
85
|
@swagger_info ||=
|
67
86
|
begin
|
@@ -74,14 +93,19 @@ module SoberSwag
|
|
74
93
|
end
|
75
94
|
end
|
76
95
|
|
96
|
+
included do |base|
|
97
|
+
base.extend ClassMethods
|
98
|
+
end
|
99
|
+
|
77
100
|
##
|
78
|
-
#
|
101
|
+
# ActiveController action to get the swagger definition for this API.
|
102
|
+
# It renders a JSON of the OpenAPI v3 schema for this API.
|
79
103
|
def swagger
|
80
104
|
render json: self.class.swagger_info
|
81
105
|
end
|
82
106
|
|
83
107
|
##
|
84
|
-
# Get the path parameters, parsed into the type you defined with {SoberSwag::Controller
|
108
|
+
# Get the path parameters, parsed into the type you defined with {SoberSwag::Controller::ClassMethods#define}
|
85
109
|
# @raise [UndefinedPathError] if there's no path params defined for this route
|
86
110
|
# @raise [Dry::Struct::Error] if we cannot convert the path params to the defined type.
|
87
111
|
def parsed_path
|
@@ -90,12 +114,12 @@ module SoberSwag
|
|
90
114
|
r = current_action_def
|
91
115
|
raise UndefinedPathError unless r&.path_params_class
|
92
116
|
|
93
|
-
r.path_params_class
|
117
|
+
build_parsed_sober_swag(r.path_params_class, request.path_parameters)
|
94
118
|
end
|
95
119
|
end
|
96
120
|
|
97
121
|
##
|
98
|
-
# Get the request body, parsed into the type you defined with {SoberSwag::Controller
|
122
|
+
# Get the request body, parsed into the type you defined with {SoberSwag::Controller::ClassMethods#define}.
|
99
123
|
# @raise [UndefinedBodyError] if there's no request body defined for this route
|
100
124
|
# @raise [Dry::Struct::Error] if we cannot convert the path params to the defined type.
|
101
125
|
def parsed_body
|
@@ -104,12 +128,12 @@ module SoberSwag
|
|
104
128
|
r = current_action_def
|
105
129
|
raise UndefinedBodyError unless r&.request_body_class
|
106
130
|
|
107
|
-
r.request_body_class
|
131
|
+
build_parsed_sober_swag(r.request_body_class, body_params)
|
108
132
|
end
|
109
133
|
end
|
110
134
|
|
111
135
|
##
|
112
|
-
# Get the query params, parsed into the type you defined with {SoberSwag::Controller
|
136
|
+
# Get the query params, parsed into the type you defined with {SoberSwag::Controller::ClassMethods#define}
|
113
137
|
# @raise [UndefinedQueryError] if there's no query params defined for this route
|
114
138
|
# @raise [Dry::Struct::Error] if we cannot convert the path params to the defined type.
|
115
139
|
def parsed_query
|
@@ -118,7 +142,7 @@ module SoberSwag
|
|
118
142
|
r = current_action_def
|
119
143
|
raise UndefinedQueryError unless r&.query_params_class
|
120
144
|
|
121
|
-
r.query_params_class
|
145
|
+
build_parsed_sober_swag(r.query_params_class, request.query_parameters)
|
122
146
|
end
|
123
147
|
end
|
124
148
|
|
@@ -130,8 +154,13 @@ module SoberSwag
|
|
130
154
|
def respond!(status, entity, serializer_opts: {}, rails_opts: {})
|
131
155
|
r = current_action_def
|
132
156
|
serializer = r.response_serializers[Rack::Utils.status_code(status)]
|
133
|
-
|
134
|
-
|
157
|
+
if serializer.respond_to?(:reporting?) && serializer.reporting?
|
158
|
+
serializer = serializer.view(serializer_opts[:view].to_sym) if serializer_opts.key?(:view)
|
159
|
+
render json: serializer.call(entity), status: status, **rails_opts
|
160
|
+
else
|
161
|
+
serializer ||= serializer.new if serializer.respond_to?(:new)
|
162
|
+
render json: serializer.serialize(entity, serializer_opts), status: status, **rails_opts
|
163
|
+
end
|
135
164
|
end
|
136
165
|
|
137
166
|
##
|
@@ -140,6 +169,8 @@ module SoberSwag
|
|
140
169
|
# This kinda violates the "be liberal in what you accept" principle,
|
141
170
|
# but it keeps the docs honest: parameters sent in the body *must* be
|
142
171
|
# in the body.
|
172
|
+
#
|
173
|
+
# @return [Hash]
|
143
174
|
def body_params
|
144
175
|
request.request_parameters
|
145
176
|
end
|
@@ -147,9 +178,18 @@ module SoberSwag
|
|
147
178
|
##
|
148
179
|
# Get the action-definition for the current action.
|
149
180
|
# Under the hood, delegates to the `:action` key of rails params.
|
181
|
+
# @return [SoberSwag::Controller::Route]
|
150
182
|
def current_action_def
|
151
183
|
self.class.find_route(params[:action])
|
152
184
|
end
|
185
|
+
|
186
|
+
def build_parsed_sober_swag(parser, params)
|
187
|
+
if parser.respond_to?(:call!)
|
188
|
+
parser.call!(params)
|
189
|
+
else
|
190
|
+
parser.call(params)
|
191
|
+
end
|
192
|
+
end
|
153
193
|
end
|
154
194
|
end
|
155
195
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module SoberSwag
|
2
2
|
##
|
3
|
-
# A variant of Dry::Struct that allows you to set a "model name" that is
|
3
|
+
# A variant of Dry::Struct that allows you to set a "model name" that is publicly visible.
|
4
4
|
# If you do not set one, it will be the Ruby class name, with any '::' replaced with a '.'.
|
5
5
|
#
|
6
6
|
# This otherwise behaves exactly like a Dry::Struct.
|
@@ -12,24 +12,101 @@ module SoberSwag
|
|
12
12
|
class << self
|
13
13
|
##
|
14
14
|
# The name to use for this type in external documentation.
|
15
|
-
|
16
|
-
|
15
|
+
#
|
16
|
+
# @param new_ident [String] what to call this InputObject in external documentation.
|
17
|
+
def identifier(new_ident = nil)
|
18
|
+
@identifier = new_ident if new_ident
|
17
19
|
|
18
20
|
@identifier || name.to_s.gsub('::', '.')
|
19
21
|
end
|
20
22
|
|
23
|
+
##
|
24
|
+
# @overload attribute(key, parent = SoberSwag::InputObject, &block)
|
25
|
+
# Defines an attribute as a direct sub-object.
|
26
|
+
# This block will be called as in {SoberSwag.input_object}.
|
27
|
+
# This might be useful in a case like the following:
|
28
|
+
#
|
29
|
+
# ```ruby
|
30
|
+
# class Classroom < SoberSwag::InputObject
|
31
|
+
# attribute :biographical_detail do
|
32
|
+
# attribute :student_name, primitive(:String)
|
33
|
+
# end
|
34
|
+
# end
|
35
|
+
# ```
|
36
|
+
#
|
37
|
+
# @param key [Symbol] the attribute name
|
38
|
+
# @param parent [Class] the parent class to use for the sub-object
|
39
|
+
# @overload attribute(key, type)
|
40
|
+
# Defines a new attribute with the given type.
|
41
|
+
# @param key [Symbol] the attribute name
|
42
|
+
# @param type the attribute type
|
21
43
|
def attribute(key, parent = SoberSwag::InputObject, &block)
|
22
44
|
raise ArgumentError, "parent class #{parent} is not an input object type!" unless valid_field_def?(parent, block)
|
23
45
|
|
24
46
|
super(key, parent, &block)
|
25
47
|
end
|
26
48
|
|
49
|
+
##
|
50
|
+
# Add on an attribute which only ever parses from a constant value.
|
51
|
+
# By default, this attribute will be called `type`, but you can override it with the kwarg.
|
52
|
+
# This is useful in situations where you want to emulate a sum type.
|
53
|
+
# For example, if you want to make an API endpoint that can either accept or reject proposals
|
54
|
+
#
|
55
|
+
# ```ruby
|
56
|
+
#
|
57
|
+
# ApiInputType = SoberSwag.input_object {
|
58
|
+
# identifier 'AcceptProposal'
|
59
|
+
# type_attribute 'accept'
|
60
|
+
# attribute(:message, primitive(:String))
|
61
|
+
# } | SoberSwag.input_object {
|
62
|
+
# identifier 'RejectProposal'
|
63
|
+
# type_attribute 'reject'
|
64
|
+
# attribute(:message, primitive(:String))
|
65
|
+
# }
|
66
|
+
# ```
|
67
|
+
#
|
68
|
+
# Under the hood, this basically looks like:
|
69
|
+
#
|
70
|
+
# ```ruby
|
71
|
+
# type_attribute 'archive'
|
72
|
+
# # is equivalent to
|
73
|
+
#
|
74
|
+
# attribute(:type, SoberSwag::Types::String.enum('archive'))
|
75
|
+
# ```
|
76
|
+
#
|
77
|
+
# @param value [String,Symbol] the value to parse
|
78
|
+
# @param attribute_key [Symbol] what key to use
|
79
|
+
def type_attribute(value, attribute_key: :type)
|
80
|
+
attribute(attribute_key, SoberSwag::Types::String.enum(value.to_s))
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# @overload attribute(key, parent = SoberSwag::InputObject, &block)
|
85
|
+
# Defines an optional attribute by defining a sub-object inline.
|
86
|
+
# This differs from a nil-able attribute as it can be *not provided*, while nilable attributes must be set to `null`.
|
87
|
+
#
|
88
|
+
# Yields to the block like in {SoberSwag.input_object}
|
89
|
+
#
|
90
|
+
# @param key [Symbol] the attribute name
|
91
|
+
# @param parent [Class] the parent class to use for the sub-object
|
92
|
+
# @overload attribute(key, type)
|
93
|
+
# Defines an optional attribute with a given type.
|
94
|
+
# This differs from a nil-able attribute as it can be *not provided*, while nilable attributes must be set to `null`.
|
95
|
+
#
|
96
|
+
# @param key [Symbol] the attribute name
|
97
|
+
# @param type the attribute type, another parsable object.
|
27
98
|
def attribute?(key, parent = SoberSwag::InputObject, &block)
|
28
99
|
raise ArgumentError, "parent class #{parent} is not an input object type!" unless valid_field_def?(parent, block)
|
29
100
|
|
30
101
|
super(key, parent, &block)
|
31
102
|
end
|
32
103
|
|
104
|
+
##
|
105
|
+
# Add metadata keys, like `:description`, to the defined type.
|
106
|
+
# Note: does NOT mutate the type, returns a new type with the metadata added.
|
107
|
+
#
|
108
|
+
# @param args [Hash] the argument values
|
109
|
+
# @return [SoberSwag::InputObject] the new input object class
|
33
110
|
def meta(*args)
|
34
111
|
original = self
|
35
112
|
|
@@ -42,8 +119,25 @@ module SoberSwag
|
|
42
119
|
end
|
43
120
|
|
44
121
|
##
|
45
|
-
#
|
46
|
-
#
|
122
|
+
# Convenience method: you can use `.primitive` get a primitive parser for a given type.
|
123
|
+
# This lets you write:
|
124
|
+
#
|
125
|
+
# ```ruby
|
126
|
+
# class Foo < SoberSwag::InputObject
|
127
|
+
# attribute :bar, primitive(:String)
|
128
|
+
# end
|
129
|
+
# ```
|
130
|
+
#
|
131
|
+
# instead of
|
132
|
+
#
|
133
|
+
# ```ruby
|
134
|
+
# class Foo < SoberSwag::InputObject
|
135
|
+
# attribute :bar, SoberSwag::Types::String
|
136
|
+
# end
|
137
|
+
# ```
|
138
|
+
#
|
139
|
+
# @param args [Symbol] a symbol
|
140
|
+
# @return a primitive parser
|
47
141
|
def primitive(*args)
|
48
142
|
if args.length == 1
|
49
143
|
SoberSwag::Types.const_get(args.first)
|
@@ -52,8 +146,31 @@ module SoberSwag
|
|
52
146
|
end
|
53
147
|
end
|
54
148
|
|
55
|
-
|
56
|
-
|
149
|
+
##
|
150
|
+
# Convenience method: you can use `.param` to get a parameter parser of a given type.
|
151
|
+
# Said parsers are more loose: for example, `param(:Integer)` will parse the string `"10"` into `10`, while
|
152
|
+
# `primitive(:Integer)` will throw an error.
|
153
|
+
#
|
154
|
+
# This method lets you write:
|
155
|
+
#
|
156
|
+
# ```ruby
|
157
|
+
# class Foo < SoberSwag::InputObject
|
158
|
+
# attribute :bar, param(:Integer)
|
159
|
+
# end
|
160
|
+
# ```
|
161
|
+
#
|
162
|
+
# instead of
|
163
|
+
#
|
164
|
+
# ```ruby
|
165
|
+
# class Foo < SoberSwag::InputObject
|
166
|
+
# attribute :bar, SoberSwag::Types::Param::Integer
|
167
|
+
# end
|
168
|
+
# ```
|
169
|
+
#
|
170
|
+
# @param name [Symbol] the name of the parameter type to get
|
171
|
+
# @return a parameter parser
|
172
|
+
def param(name)
|
173
|
+
SoberSwag::Types::Params.const_get(name)
|
57
174
|
end
|
58
175
|
|
59
176
|
private
|
@@ -10,18 +10,37 @@ module SoberSwag
|
|
10
10
|
|
11
11
|
attr_reader :elements
|
12
12
|
|
13
|
+
##
|
14
|
+
# @see SoberSwag::Nodes::Array#map
|
15
|
+
#
|
13
16
|
def map(&block)
|
14
17
|
self.class.new(elements.map { |elem| elem.map(&block) })
|
15
18
|
end
|
16
19
|
|
20
|
+
##
|
21
|
+
# @see SoberSwag::Nodes::Array#cata
|
22
|
+
#
|
23
|
+
# The block will be called with each element contained in this array node in turn, then called with a {SoberSwag::Nodes::Array} constructed
|
24
|
+
# from the resulting values.
|
25
|
+
#
|
26
|
+
# @return whatever the block yields.
|
17
27
|
def cata(&block)
|
18
28
|
block.call(self.class.new(elements.map { |elem| elem.cata(&block) }))
|
19
29
|
end
|
20
30
|
|
31
|
+
##
|
32
|
+
# Deconstructs into the elements.
|
33
|
+
#
|
34
|
+
# @return [Array<SoberSwag::Nodes::Base>]
|
21
35
|
def deconstruct
|
22
36
|
@elements
|
23
37
|
end
|
24
38
|
|
39
|
+
##
|
40
|
+
# Deconstruction for pattern-matching
|
41
|
+
#
|
42
|
+
# @return [Hash{Symbol => ::Array<SoberSwag::Nodes::Base>}]
|
43
|
+
# a hash with the elements in the `:elements` key.
|
25
44
|
def deconstruct_keys(_keys)
|
26
45
|
{ elements: @elements }
|
27
46
|
end
|