Skip to content

Commit 7cc859e

Browse files
authored
Fix param: Consider default_value: nil as valid config (#894)
* param: Consider default_value: nil as valid config When using `param` as: ```ruby param(:name, String, required: false, default_value: nil) ``` there would be a warning that `default_value` is missing. This fix changes from checking `options[key].blank?` to `options.key?(key)`. * Fixes rubocop warning * Fixes review comments
1 parent ad2bc8d commit 7cc859e

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

lib/apipie/generator/swagger/param_description/builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def for_required
7171
end
7272

7373
def for_default
74-
return {} if @param_description.options[:default_value].blank?
74+
return {} unless @param_description.options.key?(:default_value)
7575

7676
{
7777
default: @param_description.options[:default_value],

spec/lib/apipie/apipies_controller_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
end
5858

5959
it "succeeds on method details with the default language" do
60-
allow(Apipie.configuration).to receive(:default_locale).and_return("en")
61-
allow(Apipie.configuration).to receive(:languages).and_return([])
60+
allow(Apipie.configuration).to receive_messages(default_locale: "en", languages: [])
6261

6362
get :index, :params => { :version => "2.0", :resource => "architectures", :method => "index.en" }
6463

spec/lib/apipie/generator/swagger/param_description/builder_spec.rb

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,49 @@
102102

103103
subject { generated_block }
104104

105-
context 'when is not required' do
106-
let(:base_param_description_options) { { required: false } }
105+
context 'when required is true' do
106+
let(:base_param_description_options) { { required: true } }
107+
108+
it 'does not output an option without default warning' do
109+
expect { subject }.not_to output(
110+
/is optional but default value is not specified/
111+
).to_stderr
112+
end
113+
end
107114

108-
context 'and no default is given' do
109-
before { param_description_options.delete(:default) }
115+
context 'when required is false' do
116+
context 'when default_value is nil' do
117+
let(:base_param_description_options) do
118+
{ required: false, default_value: nil }
119+
end
110120

111-
it 'outputs an option without default warning' do
112-
expect { subject }.to output(/is optional but default value is not specified/).to_stderr
121+
it 'will not warn' do
122+
expect { subject }.not_to output(
123+
/is optional but default value is not specified/
124+
).to_stderr
113125
end
114126
end
115-
end
116127

117-
context 'when is required' do
118-
let(:base_param_description_options) { { required: true } }
128+
context 'when default_value is 123' do
129+
let(:base_param_description_options) do
130+
{ required: false, default_value: 123 }
131+
end
119132

120-
it 'does not output an option without default warning' do
121-
expect { subject }.not_to output(/is optional but default value is not specified/).to_stderr
133+
it 'will not warn' do
134+
expect { subject }.not_to output(
135+
/is optional but default value is not specified/
136+
).to_stderr
137+
end
138+
end
139+
140+
context 'default_value not given' do
141+
let(:base_param_description_options) { { required: false } }
142+
143+
it 'warns' do
144+
expect { subject }.to output(
145+
/is optional but default value is not specified/
146+
).to_stderr
147+
end
122148
end
123149
end
124150
end

0 commit comments

Comments
 (0)