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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76187fb16daf0690d5cea5b3725d6c0a50ea8a69fa5172557e233c4832ab776d
4
- data.tar.gz: 8c0c9bd3ba2574e8f6b081cf7248b85f03ce12cc0240cf08bce6e09962eaf067
3
+ metadata.gz: 7ed13f9dd94583c53a6397262cd798a8a8897b4ac7d7f1c4484116751ff37e5d
4
+ data.tar.gz: 777014e503f1f3febde2662275cf858a423806671e0ee4a843be3af43287df10
5
5
  SHA512:
6
- metadata.gz: fc5e7e55e448a6964f12038de02d5f3b90c16cb7d950c3154dbe3df863cc3c14f76838c556ac8c538a3da50ed5f62b30b0266a5da3d3f8d83ce44546d8f18cd2
7
- data.tar.gz: 0e3cc570dda791eaed872f7ea98f2404b16c80cbe73caadfdfd6e1e68bbdec576e339c839e70454fafa99757d2e57f4c81493be3514e0555ad079e611fc7dafd
6
+ metadata.gz: 16a4528fbed7c9dfaba78a4757f489f49e80ffef3801b8cf5a10a49c13951530789ffb2ef1e89daba94b618ef8c3348cb4d4790e4e26db420eef74548cc51664
7
+ data.tar.gz: a3411261ed52732f5d85fa7402fe232fbd4596b0d4618e22316d8f7f37ee70715481340a9256969a81418ba6c99b2321bcd09570ca371a9ec9c27702c48b5f2b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dry-swagger (0.4.1)
4
+ dry-swagger (0.5.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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
- documentation[:required] << field_name if attributes_hash.fetch(:required, true) && @config.enable_required_validation
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[:type] == 'array'
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 }
46
- elsif attributes_hash[:type] == 'hash'
47
- raise Errors::MissingHashSchemaError.new unless attributes_hash[:keys]
48
- documentation = generate_documentation(attributes_hash.fetch(:keys))
49
- else
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))
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
- if attributes_hash[:description] && @config.enable_descriptions
56
- documentation = documentation.merge(description: attributes_hash.fetch(:description))
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
- @config.enable_nullable_validation ?
61
- documentation.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
62
- documentation.merge(@config.nullable_type => true)
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
- keys[key] = {
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
- opts[:nullable] = true
114
- visit(node[1], opts)
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 = {})
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Swagger
3
- VERSION = "0.4.1"
3
+ VERSION = "0.5.2"
4
4
  end
5
5
  end
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.1
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-01 00:00:00.000000000 Z
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