dry-swagger 0.4.0 → 0.5.1

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: c52c6906b862ea3104ef5d841db24a69f38bbaa5cbda13ef553305453e1665cc
4
- data.tar.gz: 5887800bdbcccd0603aad3cecba5c593391be2644456cbf4437557aa039ca923
3
+ metadata.gz: 3b612143bf3038b32981bae55874524220f769bfe7db95ee1fc2be928f563f73
4
+ data.tar.gz: 02e71a27f38fafae4f6db24d267157467dbe1889649caf036b3eddafa8b7ef27
5
5
  SHA512:
6
- metadata.gz: a1c2b6b524bf1a06641b7769c90c40246ef27b970177ddcd81d355c7da38435ea32329eb692107dba85ba00d757e234271a5d72bed167dcc2903e1a0c0312f3e
7
- data.tar.gz: cbad39dd938d175ca9713d3a4cb834943d83ceb8efe5507e3c93e9cfb231a70251695fd40bb8eeee3a02278472fcfee603826ef04f3a627c915290f85057635d
6
+ metadata.gz: 9c3da6167caaf0f60a5d4b6f07aa00b1c964c19c8b8cc7508c7d59a3e266a34c5f54c66644030a3048e2713092ca164f4d1f82315a09a8989bfc702486bc05d8
7
+ data.tar.gz: 10c946ec34aef062dccccfedfa6fe6fb8b6ba9cfb09d0a434d7cf22d879a60177632f28c082b0dd81091cccf66204b4ded6526888b60b37234c8b8a288af0bc7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dry-swagger (0.4.0)
4
+ dry-swagger (0.5.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
- config.enable_required_validation = true / false
274
- config.enable_nullable_validation = true / false
275
- config.enable_enums = true / false
276
- config.enable_descriptions = true / false
277
- config.nullable_type = :"x-nullable" / :nullable
278
- end
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,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[:required] && @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,23 +35,60 @@ 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[:keys])
36
- items.merge(@config.nullable_type => attributes_hash[@config.nullable_type] | false) if @config.enable_nullable_validation
37
- { type: :array, items: items }
38
- elsif attributes_hash[:array] && attributes_hash[:type] != 'array'
39
- items = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash[:type])
40
- items = items.merge(@config.nullable_type => attributes_hash[@config.nullable_type] | false) if @config.enable_nullable_validation
41
- { type: :array, items: items }
42
- elsif attributes_hash[:type] == 'hash'
43
- raise Errors::MissingHashSchemaError.new unless attributes_hash[:keys]
44
- generate_documentation(attributes_hash[:keys])
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
+ }
61
+ end
45
62
  else
46
- field = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash[:type])
47
- field = field.merge(@config.nullable_type => attributes_hash[@config.nullable_type] | false) if @config.enable_nullable_validation
48
- field = field.merge(enum: attributes_hash[:enum]) if attributes_hash[:enum] if @config.enable_enums
49
- field = field.merge(description: attributes_hash[:description]) if attributes_hash[:description] if @config.enable_descriptions
50
- field
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
83
+
84
+ if attributes_hash[:description] && @config.enable_descriptions
85
+ documentation = documentation.merge(description: attributes_hash.fetch(:description))
86
+ end
87
+ end
88
+
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)
51
92
  end
52
93
 
53
94
  rescue KeyError
@@ -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,14 @@ 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[1][0].equal?(:struct)
123
+ opts[:oneOf] = true
124
+ visit(node[0], opts)
125
+ visit(node[1], opts)
126
+ end
115
127
  end
116
128
 
117
129
  def visit_struct(node, opts = {})
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Swagger
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.1"
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.0
4
+ version: 0.5.1
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