dry-swagger 0.4.1 → 0.5.2
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/lib/dry/swagger/documentation_generator.rb +56 -26
- data/lib/dry/swagger/struct_parser.rb +15 -4
- data/lib/dry/swagger/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ed13f9dd94583c53a6397262cd798a8a8897b4ac7d7f1c4484116751ff37e5d
|
4
|
+
data.tar.gz: 777014e503f1f3febde2662275cf858a423806671e0ee4a843be3af43287df10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16a4528fbed7c9dfaba78a4757f489f49e80ffef3801b8cf5a10a49c13951530789ffb2ef1e89daba94b618ef8c3348cb4d4790e4e26db420eef74548cc51664
|
7
|
+
data.tar.gz: a3411261ed52732f5d85fa7402fe232fbd4596b0d4618e22316d8f7f37ee70715481340a9256969a81418ba6c99b2321bcd09570ca371a9ec9c27702c48b5f2b
|
data/Gemfile.lock
CHANGED
@@ -19,7 +19,11 @@ 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
|
-
|
22
|
+
if attributes_hash.is_a?(Hash)
|
23
|
+
documentation[:required] << field_name if attributes_hash.fetch(:required, true) && @config.enable_required_validation
|
24
|
+
else
|
25
|
+
documentation[:required] << field_name if attributes_hash[0].fetch(:required, true) && @config.enable_required_validation
|
26
|
+
end
|
23
27
|
|
24
28
|
rescue Errors::MissingTypeError => e
|
25
29
|
raise StandardError.new e.message % { field_name: field_name, valid_types: SWAGGER_FIELD_TYPE_DEFINITIONS.keys, attributes_hash: attributes_hash }
|
@@ -31,35 +35,61 @@ module Dry
|
|
31
35
|
end
|
32
36
|
|
33
37
|
def generate_field_properties(attributes_hash)
|
34
|
-
if attributes_hash
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
38
|
+
if attributes_hash.is_a?(Array)
|
39
|
+
properties = {}
|
40
|
+
attributes_hash.each_with_index do |_, index|
|
41
|
+
properties["definition_#{index + 1}"] = generate_field_properties(attributes_hash[index])
|
42
|
+
end
|
43
|
+
if attributes_hash[0][:type] == 'array'
|
44
|
+
attributes_hash.each { |definition| definition[:type] = 'hash'}
|
45
|
+
{
|
46
|
+
type: :array,
|
47
|
+
items: {
|
48
|
+
type: :object,
|
49
|
+
properties: properties,
|
50
|
+
oneOf: attributes_hash.map{ |it| generate_field_properties(it) },
|
51
|
+
example: 'Dynamic Field. See Model Definitions'
|
52
|
+
},
|
53
|
+
}
|
54
|
+
else
|
55
|
+
{
|
56
|
+
oneOf: attributes_hash.map{ |it| generate_field_properties(it) },
|
57
|
+
type: :object,
|
58
|
+
properties: properties,
|
59
|
+
example: 'Dynamic Field. See Model Definitions'
|
60
|
+
}
|
53
61
|
end
|
62
|
+
else
|
63
|
+
if attributes_hash[:type] == 'array'
|
64
|
+
items = generate_documentation(attributes_hash.fetch(:keys))
|
65
|
+
items = @config.enable_nullable_validation ?
|
66
|
+
items.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
|
67
|
+
items.merge(@config.nullable_type => true)
|
68
|
+
documentation = { type: :array, items: items }
|
69
|
+
elsif attributes_hash[:array] && attributes_hash.fetch(:type) != 'array'
|
70
|
+
items = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash.fetch(:type))
|
71
|
+
items = @config.enable_nullable_validation ?
|
72
|
+
items.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
|
73
|
+
items.merge(@config.nullable_type => true)
|
74
|
+
documentation = { type: :array, items: items }
|
75
|
+
elsif attributes_hash[:type] == 'hash'
|
76
|
+
raise Errors::MissingHashSchemaError.new unless attributes_hash[:keys]
|
77
|
+
documentation = generate_documentation(attributes_hash.fetch(:keys))
|
78
|
+
else
|
79
|
+
documentation = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash.fetch(:type))
|
80
|
+
if attributes_hash[:enum] && @config.enable_enums
|
81
|
+
documentation = documentation.merge(enum: attributes_hash.fetch(:enum))
|
82
|
+
end
|
54
83
|
|
55
|
-
|
56
|
-
|
84
|
+
if attributes_hash[:description] && @config.enable_descriptions
|
85
|
+
documentation = documentation.merge(description: attributes_hash.fetch(:description))
|
86
|
+
end
|
57
87
|
end
|
58
|
-
end
|
59
88
|
|
60
|
-
|
61
|
-
|
62
|
-
|
89
|
+
@config.enable_nullable_validation ?
|
90
|
+
documentation.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
|
91
|
+
documentation.merge(@config.nullable_type => true)
|
92
|
+
end
|
63
93
|
|
64
94
|
rescue KeyError
|
65
95
|
raise Errors::MissingTypeError.new
|
@@ -15,7 +15,7 @@ module Dry
|
|
15
15
|
|
16
16
|
def initialize
|
17
17
|
@keys = {}
|
18
|
-
@config = Config::StructConfiguration
|
18
|
+
@config = Dry::Swagger::Config::StructConfiguration
|
19
19
|
end
|
20
20
|
|
21
21
|
def to_h
|
@@ -54,12 +54,18 @@ module Dry
|
|
54
54
|
|
55
55
|
type = opts[:array]? 'array' : 'hash'
|
56
56
|
|
57
|
-
|
57
|
+
definition = {
|
58
58
|
type: type,
|
59
59
|
required: required,
|
60
60
|
@config.nullable_type => nullable,
|
61
61
|
**target_info
|
62
62
|
}
|
63
|
+
|
64
|
+
if opts[:oneOf]
|
65
|
+
keys[key] = keys[key] ? keys[key] << definition : [definition]
|
66
|
+
else
|
67
|
+
keys[key] = definition
|
68
|
+
end
|
63
69
|
end
|
64
70
|
|
65
71
|
def visit_key(node, opts = {})
|
@@ -110,8 +116,13 @@ module Dry
|
|
110
116
|
end
|
111
117
|
|
112
118
|
def visit_sum(node, opts = {})
|
113
|
-
|
114
|
-
|
119
|
+
if node[0][0].equal?(:constrained)
|
120
|
+
opts[:nullable] = true
|
121
|
+
visit(node[1], opts) # ignore NilClass constrained
|
122
|
+
elsif node[0][0].equal?(:struct) || node[0][0].equal?(:sum)
|
123
|
+
opts[:oneOf] = true
|
124
|
+
node.each { |child| visit(child, opts) unless child.is_a?(Hash) }
|
125
|
+
end
|
115
126
|
end
|
116
127
|
|
117
128
|
def visit_struct(node, opts = {})
|
data/lib/dry/swagger/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-swagger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jane-Terziev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-08-
|
11
|
+
date: 2021-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A parser which converts dry-validation or dry-struct into valid swagger
|
14
14
|
documentation
|