haveapi 0.27.2 → 0.27.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33a06b199d36331bf03d273d528e62fad7a0fa96e78667afa3d8411734e8d966
4
- data.tar.gz: 94c99822c1b06670ea2623b9faa8c9d22bc7fdfd1be9e3a7d4fb09928f812395
3
+ metadata.gz: ec45b62af7fa8653e5f1a304b57746a9a2858db8b75c6a1d8a3deb5536cc7946
4
+ data.tar.gz: 0a991e394ccf4a4629f8d755fcfdfa9fd76af6fe37c696d361c1d9340ea8763c
5
5
  SHA512:
6
- metadata.gz: e473cd23de9122f5e817bafbaf24147a2c67b5d311a37c690d7b474d09647e7b4b0ea6f7104f99377ca8534e352b274dcaa7403a67b3dce2dfd510a7ab75fe7a
7
- data.tar.gz: c7f8b6f1ffbcf9b8f299e8ce487b8959cc631b98ac3c7b53e508ada38a3191d687b5772c8fd84eac0a14359b70ac182c98859976d81e0cacebfb2a5c436e6b5e
6
+ metadata.gz: 693eb912e8215aa93c1c7b009475d91a752bfee1a2d4faeb33d59728168def03128dc7738c264e52f6cef3a87b7e7e11362a4da2f227deb057a9a89e50813705
7
+ data.tar.gz: e11fd922568a2ed631a702ec71e8e4631759bee27ecd122029e50af3fb389cde4f7167a0c26876b79b1db220d434b4c3117cb338e1a1fb3f529d85675fa91f1d
data/haveapi.gemspec CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.required_ruby_version = ">= #{File.read('../../.ruby-version').strip}"
16
16
 
17
17
  s.add_dependency 'activesupport', '>= 7.1'
18
- s.add_dependency 'haveapi-client', '~> 0.27.2'
18
+ s.add_dependency 'haveapi-client', '~> 0.27.3'
19
19
  s.add_dependency 'json'
20
20
  s.add_dependency 'mail'
21
21
  s.add_dependency 'nesty', '~> 1.0'
@@ -142,15 +142,22 @@ module HaveAPI::ModelAdapters
142
142
 
143
143
  class Input < ::HaveAPI::ModelAdapter::Input
144
144
  def self.clean(model, raw, extra)
145
- return nil if raw.nil?
146
-
147
145
  original = raw
146
+ allow_null = if extra
147
+ extra.has_key?(:nullable) ? extra[:nullable] : extra[:optional]
148
+ end
149
+
150
+ if raw.nil?
151
+ return nil if allow_null
152
+
153
+ raise HaveAPI::ValidationError, "not a valid id #{original.inspect}"
154
+ end
148
155
 
149
156
  if raw.is_a?(String)
150
157
  stripped = raw.strip
151
158
 
152
159
  if stripped.empty?
153
- return nil if extra && extra[:optional]
160
+ return nil if allow_null
154
161
 
155
162
  raise HaveAPI::ValidationError, "not a valid id #{original.inspect}"
156
163
  end
@@ -5,7 +5,7 @@ module HaveAPI::Parameters
5
5
 
6
6
  def initialize(resource, name: nil, label: nil, desc: nil,
7
7
  choices: nil, value_id: :id, value_label: :label, required: nil,
8
- db_name: nil, fetch: nil)
8
+ db_name: nil, fetch: nil, nullable: nil)
9
9
  @resource = resource
10
10
  @resource_path = build_resource_path(resource)
11
11
  @name = name || resource.resource_name.underscore.to_sym
@@ -15,6 +15,7 @@ module HaveAPI::Parameters
15
15
  @value_id = value_id
16
16
  @value_label = value_label
17
17
  @required = required
18
+ @nullable = nullable
18
19
  @db_name = db_name
19
20
  @extra = {
20
21
  fetch:
@@ -33,6 +34,10 @@ module HaveAPI::Parameters
33
34
  !@required
34
35
  end
35
36
 
37
+ def nullable?
38
+ @nullable == true && optional?
39
+ end
40
+
36
41
  def show_action
37
42
  @resource::Show
38
43
  end
@@ -56,6 +61,7 @@ module HaveAPI::Parameters
56
61
 
57
62
  {
58
63
  required: required?,
64
+ nullable: nullable?,
59
65
  label: @label,
60
66
  description: @desc,
61
67
  type: 'Resource',
@@ -92,12 +98,18 @@ module HaveAPI::Parameters
92
98
  end
93
99
 
94
100
  def clean(raw)
101
+ if raw.nil?
102
+ return nil if nullable?
103
+
104
+ raise HaveAPI::ValidationError, 'cannot be null'
105
+ end
106
+
95
107
  if raw.is_a?(String)
96
108
  stripped = raw.strip
97
- return nil if stripped.empty? && optional?
109
+ return nil if stripped.empty? && nullable?
98
110
  end
99
111
 
100
- extra = @extra.merge(optional: optional?)
112
+ extra = @extra.merge(optional: optional?, nullable: nullable?)
101
113
 
102
114
  ::HaveAPI::ModelAdapter.for(
103
115
  show_action.input.layout, @resource.model
@@ -2,7 +2,7 @@ require 'date'
2
2
 
3
3
  module HaveAPI::Parameters
4
4
  class Typed
5
- ATTRIBUTES = %i[label desc type db_name default fill clean protected load_validators].freeze
5
+ ATTRIBUTES = %i[label desc type db_name default fill clean protected load_validators nullable].freeze
6
6
 
7
7
  attr_reader :name, :label, :desc, :type, :default
8
8
 
@@ -36,6 +36,10 @@ module HaveAPI::Parameters
36
36
  !required?
37
37
  end
38
38
 
39
+ def nullable?
40
+ @nullable == true && optional?
41
+ end
42
+
39
43
  def fill?
40
44
  @fill
41
45
  end
@@ -47,6 +51,7 @@ module HaveAPI::Parameters
47
51
  def describe(context)
48
52
  {
49
53
  required: required?,
54
+ nullable: nullable?,
50
55
  label: @label,
51
56
  description: @desc,
52
57
  type: @type ? @type.to_s : String.to_s,
@@ -75,15 +80,18 @@ module HaveAPI::Parameters
75
80
  def clean(raw)
76
81
  return instance_exec(raw, &@clean) if @clean
77
82
 
83
+ if raw.nil?
84
+ return nil if nullable?
85
+
86
+ raise HaveAPI::ValidationError, 'cannot be null'
87
+ end
88
+
78
89
  if raw.is_a?(String)
79
90
  stripped = raw.strip
80
- return nil if stripped.empty? && optional?
91
+ return nil if stripped.empty? && nullable?
81
92
  end
82
93
 
83
- if raw.nil?
84
- @default
85
-
86
- elsif @type.nil?
94
+ if @type.nil?
87
95
  nil
88
96
 
89
97
  elsif @type == Integer
@@ -85,10 +85,11 @@ module HaveAPI::Resources
85
85
  float :timeout, label: 'Timeout', desc: 'in seconds', default: 15, fill: true
86
86
  float :update_in, label: 'Progress',
87
87
  desc: 'number of seconds after which the state is returned if the progress ' \
88
- 'has changed'
89
- bool :status, desc: 'status to check with if update_in is set'
90
- integer :current, desc: 'progress to check with if update_in is set'
91
- integer :total, desc: 'progress to check with if update_in is set'
88
+ 'has changed',
89
+ nullable: true
90
+ bool :status, desc: 'status to check with if update_in is set', nullable: true
91
+ integer :current, desc: 'progress to check with if update_in is set', nullable: true
92
+ integer :total, desc: 'progress to check with if update_in is set', nullable: true
92
93
  end
93
94
 
94
95
  output(:hash) do
@@ -1,4 +1,4 @@
1
1
  module HaveAPI
2
2
  PROTOCOL_VERSION = '2.0'.freeze
3
- VERSION = '0.27.2'.freeze
3
+ VERSION = '0.27.3'.freeze
4
4
  end
@@ -343,8 +343,8 @@ describe HaveAPI::ModelAdapters::ActiveRecord do
343
343
  expect(described_class::Input.clean(ARAdapterSpec::Environment, 1, {})).to eq(environment)
344
344
  expect(described_class::Input.clean(ARAdapterSpec::Environment, '1', {})).to eq(environment)
345
345
  expect(described_class::Input.clean(ARAdapterSpec::Environment, 1.0, {})).to eq(environment)
346
- expect(described_class::Input.clean(ARAdapterSpec::Environment, '', { optional: true })).to be_nil
347
- expect(described_class::Input.clean(ARAdapterSpec::Environment, ' ', { optional: true })).to be_nil
346
+ expect(described_class::Input.clean(ARAdapterSpec::Environment, '', { nullable: true })).to be_nil
347
+ expect(described_class::Input.clean(ARAdapterSpec::Environment, ' ', { nullable: true })).to be_nil
348
348
 
349
349
  expect do
350
350
  described_class::Input.clean(ARAdapterSpec::Environment, 'abc', {})
@@ -78,7 +78,8 @@ describe 'Parameters::Typed' do
78
78
  expect(p.clean(12.0)).to eq(12)
79
79
  expect { p.clean('abc') }.to raise_error(HaveAPI::ValidationError)
80
80
  expect { p.clean('12abc') }.to raise_error(HaveAPI::ValidationError)
81
- expect(p.clean('')).to be_nil
81
+ expect { p.clean('') }.to raise_error(HaveAPI::ValidationError)
82
+ expect { p.clean(nil) }.to raise_error(HaveAPI::ValidationError)
82
83
  expect { p.clean('12.0') }.to raise_error(HaveAPI::ValidationError)
83
84
  expect { p.clean(12.3) }.to raise_error(HaveAPI::ValidationError)
84
85
  expect { p.clean(true) }.to raise_error(HaveAPI::ValidationError)
@@ -92,7 +93,8 @@ describe 'Parameters::Typed' do
92
93
  expect(p.clean('1e3')).to eq(1000.0)
93
94
  expect(p.clean(3)).to eq(3.0)
94
95
  expect { p.clean('abc') }.to raise_error(HaveAPI::ValidationError)
95
- expect(p.clean('')).to be_nil
96
+ expect { p.clean('') }.to raise_error(HaveAPI::ValidationError)
97
+ expect { p.clean(nil) }.to raise_error(HaveAPI::ValidationError)
96
98
  expect { p.clean('NaN') }.to raise_error(HaveAPI::ValidationError)
97
99
  expect { p.clean(Float::NAN) }.to raise_error(HaveAPI::ValidationError)
98
100
  expect { p.clean(Float::INFINITY) }.to raise_error(HaveAPI::ValidationError)
@@ -115,7 +117,8 @@ describe 'Parameters::Typed' do
115
117
  expect(p.clean(1)).to be true
116
118
  expect(p.clean(' YES ')).to be true
117
119
  expect { p.clean('maybe') }.to raise_error(HaveAPI::ValidationError)
118
- expect(p.clean('')).to be_nil
120
+ expect { p.clean('') }.to raise_error(HaveAPI::ValidationError)
121
+ expect { p.clean(nil) }.to raise_error(HaveAPI::ValidationError)
119
122
  expect { p.clean(2) }.to raise_error(HaveAPI::ValidationError)
120
123
 
121
124
  p = p_arg(type: Boolean, required: true)
@@ -128,7 +131,8 @@ describe 'Parameters::Typed' do
128
131
 
129
132
  expect(p.clean(t.iso8601)).to eq(t2)
130
133
  expect { p.clean('bzz') }.to raise_error(HaveAPI::ValidationError)
131
- expect(p.clean('')).to be_nil
134
+ expect { p.clean('') }.to raise_error(HaveAPI::ValidationError)
135
+ expect { p.clean(nil) }.to raise_error(HaveAPI::ValidationError)
132
136
 
133
137
  p = p_arg(type: Datetime, required: true)
134
138
  expect { p.clean('') }.to raise_error(HaveAPI::ValidationError)
@@ -136,6 +140,7 @@ describe 'Parameters::Typed' do
136
140
  # String, Text
137
141
  p = p_type(String)
138
142
  expect(p.clean('bzz')).to eq('bzz')
143
+ expect(p.clean('')).to eq('')
139
144
  expect(p.clean(123)).to eq('123')
140
145
  expect(p.clean(true)).to eq('true')
141
146
  expect { p.clean([]) }.to raise_error(HaveAPI::ValidationError)
@@ -143,17 +148,38 @@ describe 'Parameters::Typed' do
143
148
 
144
149
  p = p_type(Text)
145
150
  expect(p.clean('bzz')).to eq('bzz')
151
+ expect(p.clean('')).to eq('')
146
152
  expect(p.clean(123)).to eq('123')
147
153
  expect(p.clean(true)).to eq('true')
148
154
  expect { p.clean([]) }.to raise_error(HaveAPI::ValidationError)
149
155
  expect { p.clean({}) }.to raise_error(HaveAPI::ValidationError)
150
156
 
151
- # Defaults
152
- p = p_type(String)
157
+ # Nullable
158
+ p = p_arg(type: Integer, nullable: true)
159
+ expect(p.clean('')).to be_nil
160
+ expect(p.clean(nil)).to be_nil
161
+
162
+ p = p_arg(type: Float, nullable: true)
163
+ expect(p.clean('')).to be_nil
153
164
  expect(p.clean(nil)).to be_nil
154
165
 
166
+ p = p_arg(type: Boolean, nullable: true)
167
+ expect(p.clean('')).to be_nil
168
+ expect(p.clean(nil)).to be_nil
169
+
170
+ p = p_arg(type: Datetime, nullable: true)
171
+ expect(p.clean('')).to be_nil
172
+ expect(p.clean(nil)).to be_nil
173
+
174
+ p = p_arg(type: String, nullable: true)
175
+ expect(p.clean('')).to be_nil
176
+ expect(p.clean(nil)).to be_nil
155
177
  p.patch(default: 'bazinga')
156
- expect(p.clean(nil)).to eq('bazinga')
178
+ expect(p.clean(nil)).to be_nil
179
+
180
+ p = p_arg(type: Text, nullable: true)
181
+ expect(p.clean('')).to be_nil
182
+ expect(p.clean(nil)).to be_nil
157
183
  end
158
184
 
159
185
  it 'can be protected' do
@@ -462,7 +462,7 @@ module HaveAPI
462
462
  route 'echo_optional'
463
463
  http_method :post
464
464
  input(:hash) do
465
- datetime :dt, required: false
465
+ datetime :dt, required: false, nullable: true
466
466
  end
467
467
  output(:hash) do
468
468
  bool :dt_provided, required: true
@@ -486,7 +486,7 @@ module HaveAPI
486
486
  route 'echo_optional_get'
487
487
  http_method :get
488
488
  input(:hash) do
489
- datetime :dt, required: false
489
+ datetime :dt, required: false, nullable: true
490
490
  end
491
491
  output(:hash) do
492
492
  bool :dt_provided, required: true
@@ -527,7 +527,7 @@ module HaveAPI
527
527
  route 'echo_resource_optional'
528
528
  http_method :get
529
529
  input(:hash) do
530
- resource HaveAPI::ClientTestAPI::Resources::Project, required: false
530
+ resource HaveAPI::ClientTestAPI::Resources::Project, required: false, nullable: true
531
531
  end
532
532
  output(:hash) do
533
533
  bool :project_provided, required: true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haveapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.27.2
4
+ version: 0.27.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Skokan
@@ -29,14 +29,14 @@ dependencies:
29
29
  requirements:
30
30
  - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: 0.27.2
32
+ version: 0.27.3
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 0.27.2
39
+ version: 0.27.3
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: json
42
42
  requirement: !ruby/object:Gem::Requirement