Skip to content

Commit

Permalink
Fix param: Consider default_value: nil as valid config (#894)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
davidwessman authored Aug 9, 2023
1 parent ad2bc8d commit 7cc859e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/apipie/generator/swagger/param_description/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def for_required
end

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

{
default: @param_description.options[:default_value],
Expand Down
3 changes: 1 addition & 2 deletions spec/lib/apipie/apipies_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
end

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

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

Expand Down
48 changes: 37 additions & 11 deletions spec/lib/apipie/generator/swagger/param_description/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,49 @@

subject { generated_block }

context 'when is not required' do
let(:base_param_description_options) { { required: false } }
context 'when required is true' do
let(:base_param_description_options) { { required: true } }

it 'does not output an option without default warning' do
expect { subject }.not_to output(
/is optional but default value is not specified/
).to_stderr
end
end

context 'and no default is given' do
before { param_description_options.delete(:default) }
context 'when required is false' do
context 'when default_value is nil' do
let(:base_param_description_options) do
{ required: false, default_value: nil }
end

it 'outputs an option without default warning' do
expect { subject }.to output(/is optional but default value is not specified/).to_stderr
it 'will not warn' do
expect { subject }.not_to output(
/is optional but default value is not specified/
).to_stderr
end
end
end

context 'when is required' do
let(:base_param_description_options) { { required: true } }
context 'when default_value is 123' do
let(:base_param_description_options) do
{ required: false, default_value: 123 }
end

it 'does not output an option without default warning' do
expect { subject }.not_to output(/is optional but default value is not specified/).to_stderr
it 'will not warn' do
expect { subject }.not_to output(
/is optional but default value is not specified/
).to_stderr
end
end

context 'default_value not given' do
let(:base_param_description_options) { { required: false } }

it 'warns' do
expect { subject }.to output(
/is optional but default value is not specified/
).to_stderr
end
end
end
end
Expand Down

0 comments on commit 7cc859e

Please sign in to comment.