dry-swagger 0.4.2 → 0.5.0

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: b13a0cda4f7abf7bc7e5438b5b4be9e91f2a06f46abc38d431ce8f6df9ba20ed
4
- data.tar.gz: 7d3d42835269384bd81df3a687528e23a125bdbb40a30d54b34d0c517fbe5993
3
+ metadata.gz: 5179183ed4122bcf5042f8aa812b6830b1126ae00d4eb2975bd17203af2bf375
4
+ data.tar.gz: 9eb614eaaaca4752a39f9a77ef2fac3dffb4df2179a0c7e7f427d13f9f02333e
5
5
  SHA512:
6
- metadata.gz: ffb3be97689579101a317b3e3747b6fd033a2d66bd37644135a029a6a5ca228e4bec4974d2a59ae1339a720b11f5de2bb32aa9f94e7f6be3dac4e9b87b699a7d
7
- data.tar.gz: e7164332db7a8c1e07048632180c1a4576ea4608e530fc4545ca5358e2169dc2db16236df17ed7356e8888bd8ed492ed196688df9113905521e7a94801d69ecb
6
+ metadata.gz: 58dea258473d5fea4c521d699a2332ec8a444d15504b81ff3eefba0ef50c08371fe5d9b3fd7e2d3331613375f625bdb3ea54073dde31cfc20eafdc0d29e49202
7
+ data.tar.gz: 2ec6862a8021cb836101cac89b394482507a6b48021cfbf304b63c8753fe0bcefb822730a194ae869da6c83dc34e965af771a58b6052dc729cea0d528a97b6a4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dry-swagger (0.4.2)
4
+ dry-swagger (0.5.0)
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,48 @@ 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])
53
42
  end
43
+ {
44
+ oneOf: attributes_hash.map{ |it| generate_field_properties(it) },
45
+ type: :object,
46
+ properties: properties,
47
+ example: 'Dynamic Field. See Model Definitions'
48
+ }
49
+ else
50
+ if attributes_hash[:type] == 'array'
51
+ items = generate_documentation(attributes_hash.fetch(:keys))
52
+ items = @config.enable_nullable_validation ?
53
+ items.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
54
+ items.merge(@config.nullable_type => true)
55
+ documentation = { type: :array, items: items }
56
+ elsif attributes_hash[:array] && attributes_hash.fetch(:type) != 'array'
57
+ items = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash.fetch(:type))
58
+ items = @config.enable_nullable_validation ?
59
+ items.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
60
+ items.merge(@config.nullable_type => true)
61
+ documentation = { type: :array, items: items }
62
+ elsif attributes_hash[:type] == 'hash'
63
+ raise Errors::MissingHashSchemaError.new unless attributes_hash[:keys]
64
+ documentation = generate_documentation(attributes_hash.fetch(:keys))
65
+ else
66
+ documentation = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash.fetch(:type))
67
+ if attributes_hash[:enum] && @config.enable_enums
68
+ documentation = documentation.merge(enum: attributes_hash.fetch(:enum))
69
+ end
54
70
 
55
- if attributes_hash[:description] && @config.enable_descriptions
56
- documentation = documentation.merge(description: attributes_hash.fetch(:description))
71
+ if attributes_hash[:description] && @config.enable_descriptions
72
+ documentation = documentation.merge(description: attributes_hash.fetch(:description))
73
+ end
57
74
  end
58
- end
59
75
 
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)
76
+ @config.enable_nullable_validation ?
77
+ documentation.merge(@config.nullable_type => attributes_hash.fetch(@config.nullable_type, false)) :
78
+ documentation.merge(@config.nullable_type => true)
79
+ end
63
80
 
64
81
  rescue KeyError
65
82
  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 = {})
@@ -113,8 +119,10 @@ module Dry
113
119
  if node[0][0].equal?(:constrained)
114
120
  opts[:nullable] = true
115
121
  visit(node[1], opts) # ignore NilClass constrained
116
- elsif node[0][0].equal?(:struct)
117
- visit(node[0], opts) # grab left operand, this means if you have it defined as DTO1 | DTO2, it will grab DTO1
122
+ elsif node[0][0].equal?(:struct) && node[1][0].equal?(:struct)
123
+ opts[:oneOf] = true
124
+ visit(node[0], opts)
125
+ visit(node[1], opts)
118
126
  end
119
127
  end
120
128
 
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Swagger
3
- VERSION = "0.4.2"
3
+ VERSION = "0.5.0"
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.2
4
+ version: 0.5.0
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