dry-swagger 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +63 -10
- data/lib/dry/swagger/documentation_generator.rb +25 -14
- data/lib/dry/swagger/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76187fb16daf0690d5cea5b3725d6c0a50ea8a69fa5172557e233c4832ab776d
|
4
|
+
data.tar.gz: 8c0c9bd3ba2574e8f6b081cf7248b85f03ce12cc0240cf08bce6e09962eaf067
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc5e7e55e448a6964f12038de02d5f3b90c16cb7d950c3154dbe3df863cc3c14f76838c556ac8c538a3da50ed5f62b30b0266a5da3d3f8d83ce44546d8f18cd2
|
7
|
+
data.tar.gz: 0e3cc570dda791eaed872f7ea98f2404b16c80cbe73caadfdfd6e1e68bbdec576e339c839e70454fafa99757d2e57f4c81493be3514e0555ad079e611fc7dafd
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -33,7 +33,7 @@ Or install it yourself as:
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
Dry::Swagger::ContractParser.new.call(Contract)
|
36
|
+
Dry::Swagger::ContractParser.new.call(Contract).to_swagger
|
37
37
|
=> {
|
38
38
|
"type": "object",
|
39
39
|
"properties": {
|
@@ -81,7 +81,7 @@ Or install it yourself as:
|
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
84
|
-
Dry::Swagger::ContractParser.new.call(Contract)
|
84
|
+
Dry::Swagger::ContractParser.new.call(Contract).to_swagger
|
85
85
|
=> {
|
86
86
|
"type": "object",
|
87
87
|
"properties": {
|
@@ -135,7 +135,7 @@ Or install it yourself as:
|
|
135
135
|
attribute? :optional_nullable_string, Types::String.optional
|
136
136
|
end
|
137
137
|
|
138
|
-
Dry::Swagger::StructParser.new.call(DTO)
|
138
|
+
Dry::Swagger::StructParser.new.call(DTO).to_swagger
|
139
139
|
=> {
|
140
140
|
"type": "object",
|
141
141
|
"properties": {
|
@@ -170,7 +170,22 @@ Or install it yourself as:
|
|
170
170
|
]
|
171
171
|
}
|
172
172
|
#### With nested fields
|
173
|
-
|
173
|
+
class NestedDTO < Dry::Struct
|
174
|
+
attribute :required_string, Types::String
|
175
|
+
attribute :required_nullable_string, Types::String.optional
|
176
|
+
attribute :required_string_with_enum, Types::String.enum('enum1')
|
177
|
+
attribute? :optional_string, Types::String
|
178
|
+
attribute? :optional_nullable_string, Types::String.optional
|
179
|
+
end
|
180
|
+
|
181
|
+
class DTO < Dry::Struct
|
182
|
+
attribute :array_of_integer, Types::Array.of(Types::Integer)
|
183
|
+
attribute :array_of_objects, Types::Array.of(NestedDTO)
|
184
|
+
attribute :dto, NestedDTO
|
185
|
+
end
|
186
|
+
|
187
|
+
Dry::Swagger::StructParser.new.call(DTO).to_swagger
|
188
|
+
=> {
|
174
189
|
"type": "object",
|
175
190
|
"properties": {
|
176
191
|
"array_of_integer": {
|
@@ -258,6 +273,44 @@ Or install it yourself as:
|
|
258
273
|
"dto"
|
259
274
|
]
|
260
275
|
}
|
276
|
+
## Overriding fields on run time
|
277
|
+
You can also modify the fields during runtime by passing a block after the .call() method.
|
278
|
+
|
279
|
+
For example:
|
280
|
+
|
281
|
+
Dry::Swagger::StructParser.new.call(DTO) do |it|
|
282
|
+
# types = string/integer/hash/array
|
283
|
+
|
284
|
+
# Remove a field
|
285
|
+
its.keys = it.keys.except(:field_name)
|
286
|
+
|
287
|
+
# Add new field on root level
|
288
|
+
it.keys[:new_field_name] = { type: type, required: true/false, :it.config.nullable_type=>true/false }
|
289
|
+
|
290
|
+
# Add a new field in nested hash/array
|
291
|
+
it.keys[:nested_field][:keys][:new_field_name] = {
|
292
|
+
type: type, required: true/false, :it.config.nullable_type=>true/false
|
293
|
+
}
|
294
|
+
|
295
|
+
# Remove a field in nested hash/array
|
296
|
+
it.keys = it.keys[:nested_field][:keys].except(:field_name)
|
297
|
+
|
298
|
+
# Add an array or hash
|
299
|
+
it.keys[:nested_field] = {
|
300
|
+
type: "array/hash", required: true/false, :it.config.nullable_type=> true/false, keys: {
|
301
|
+
# List all nested fields
|
302
|
+
new_field: { type: :type, required: true/false, :it.config.nullable_type=>true/false }
|
303
|
+
}
|
304
|
+
}
|
305
|
+
|
306
|
+
# Add an Array of primitive types, type field needs to be the element type(string, integer, float),
|
307
|
+
and add an array: true flag
|
308
|
+
|
309
|
+
it.keys[:array_field_name] = {
|
310
|
+
type: type, array: true, required: true/false, :it.config.nullable_type=> true/false
|
311
|
+
}
|
312
|
+
|
313
|
+
end.to_swagger()
|
261
314
|
## Custom Configuration For Your Project
|
262
315
|
You can override default configurations by creating a file in config/initializers/dry-swagger.rb and changing the following values.
|
263
316
|
|
@@ -270,12 +323,12 @@ You can override default configurations by creating a file in config/initializer
|
|
270
323
|
end
|
271
324
|
|
272
325
|
Dry::Swagger::Config::ContractConfiguration.configuration do |config|
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
326
|
+
config.enable_required_validation = true / false
|
327
|
+
config.enable_nullable_validation = true / false
|
328
|
+
config.enable_enums = true / false
|
329
|
+
config.enable_descriptions = true / false
|
330
|
+
config.nullable_type = :"x-nullable" / :nullable
|
331
|
+
end
|
279
332
|
|
280
333
|
By default, all these settings are true, and nullable_type is :"x-nullable".
|
281
334
|
## Development
|
@@ -19,7 +19,7 @@ module Dry
|
|
19
19
|
documentation = { properties: {}, required: [] }
|
20
20
|
fields.each do |field_name, attributes_hash|
|
21
21
|
documentation[:properties][field_name] = generate_field_properties(attributes_hash)
|
22
|
-
documentation[:required] << field_name if attributes_hash
|
22
|
+
documentation[:required] << field_name if attributes_hash.fetch(:required, true) && @config.enable_required_validation
|
23
23
|
|
24
24
|
rescue Errors::MissingTypeError => e
|
25
25
|
raise StandardError.new e.message % { field_name: field_name, valid_types: SWAGGER_FIELD_TYPE_DEFINITIONS.keys, attributes_hash: attributes_hash }
|
@@ -32,24 +32,35 @@ module Dry
|
|
32
32
|
|
33
33
|
def generate_field_properties(attributes_hash)
|
34
34
|
if attributes_hash[:type] == 'array'
|
35
|
-
items = generate_documentation(attributes_hash
|
36
|
-
items
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
35
|
+
items = generate_documentation(attributes_hash.fetch(:keys))
|
36
|
+
items = @config.enable_nullable_validation ?
|
37
|
+
items.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
|
38
|
+
items.merge(@config.nullable_type => true)
|
39
|
+
documentation = { type: :array, items: items }
|
40
|
+
elsif attributes_hash[:array] && attributes_hash.fetch(:type) != 'array'
|
41
|
+
items = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash.fetch(:type))
|
42
|
+
items = @config.enable_nullable_validation ?
|
43
|
+
items.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
|
44
|
+
items.merge(@config.nullable_type => true)
|
45
|
+
documentation = { type: :array, items: items }
|
42
46
|
elsif attributes_hash[:type] == 'hash'
|
43
47
|
raise Errors::MissingHashSchemaError.new unless attributes_hash[:keys]
|
44
|
-
generate_documentation(attributes_hash
|
48
|
+
documentation = generate_documentation(attributes_hash.fetch(:keys))
|
45
49
|
else
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
50
|
+
documentation = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash.fetch(:type))
|
51
|
+
if attributes_hash[:enum] && @config.enable_enums
|
52
|
+
documentation = documentation.merge(enum: attributes_hash.fetch(:enum))
|
53
|
+
end
|
54
|
+
|
55
|
+
if attributes_hash[:description] && @config.enable_descriptions
|
56
|
+
documentation = documentation.merge(description: attributes_hash.fetch(:description))
|
57
|
+
end
|
51
58
|
end
|
52
59
|
|
60
|
+
@config.enable_nullable_validation ?
|
61
|
+
documentation.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
|
62
|
+
documentation.merge(@config.nullable_type => true)
|
63
|
+
|
53
64
|
rescue KeyError
|
54
65
|
raise Errors::MissingTypeError.new
|
55
66
|
end
|
data/lib/dry/swagger/version.rb
CHANGED