Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow boolean array [true, false] to be a valid argument for BooleanValidator #848

Merged
merged 6 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions lib/apipie/generator/swagger/type_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,10 @@ def initialize(validator)
@validator = validator
end

# @param [Hash<Symbol, Apipie::Generator::Swagger::Warning>] warnings
def extract_with_warnings(warnings = {})
if boolean? && warnings[:boolean].present?
Apipie::Generator::Swagger::WarningWriter.instance.warn(warnings[:boolean])
end

extract
end

def extract
expected_type =
if string?
:string
elsif boolean?
:boolean
elsif enum?
:enum
else
Expand All @@ -59,12 +48,4 @@ def enum?
@validator.is_a?(Apipie::Validator::EnumValidator) ||
(@validator.respond_to?(:is_enum?) && @validator.is_enum?)
end

def boolean?
@_boolean ||= enum? && boolean_values?
end

def boolean_values?
@validator.values.to_set == Set.new([true, false])
end
end
7 changes: 2 additions & 5 deletions lib/apipie/generator/swagger/warning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Apipie::Generator::Swagger::Warning
OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE = 105
PARAM_IGNORED_IN_FORM_DATA_CODE = 106
PATH_PARAM_NOT_DESCRIBED_CODE = 107
INFERRING_BOOLEAN_CODE = 108

CODES = {
missing_method_summary: MISSING_METHOD_SUMMARY_CODE,
Expand All @@ -17,8 +16,7 @@ class Apipie::Generator::Swagger::Warning
optional_param_in_path: OPTIONAL_PARAM_IN_PATH_CODE,
optional_without_default_value: OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE,
param_ignored_in_form_data: PARAM_IGNORED_IN_FORM_DATA_CODE,
path_param_not_described_code: PATH_PARAM_NOT_DESCRIBED_CODE,
inferring_boolean: INFERRING_BOOLEAN_CODE
path_param_not_described_code: PATH_PARAM_NOT_DESCRIBED_CODE
}

MESSAGES = {
Expand All @@ -29,8 +27,7 @@ class Apipie::Generator::Swagger::Warning
OPTIONAL_PARAM_IN_PATH_CODE => "The parameter :%{parameter} is 'in-path'. Ignoring 'not required' in DSL",
OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE => "The parameter :%{parameter} is optional but default value is not specified (use :default_value => ...)",
PARAM_IGNORED_IN_FORM_DATA_CODE => "Ignoring param :%{parameter} -- cannot include Hash without fields in a formData specification",
PATH_PARAM_NOT_DESCRIBED_CODE => "The parameter :%{name} appears in the path %{path} but is not described",
INFERRING_BOOLEAN_CODE => "The parameter [%{parameter}] is Enum with [true, false] values. Inferring 'boolean'"
PATH_PARAM_NOT_DESCRIBED_CODE => "The parameter :%{name} appears in the path %{path} but is not described"
}

attr_reader :code
Expand Down
10 changes: 7 additions & 3 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,13 @@ def validate(value)
end

def self.build(param_description, argument, options, block)
if argument == :bool || argument == :boolean
if argument == :bool || argument == :boolean || boolean_array?(argument)
self.new(param_description)
end
end

def expected_type
'boolean'
private_class_method def self.boolean_array?(argument)
argument.is_a?(Array) && (argument - [true, false]) == []
end

def description
Expand All @@ -489,6 +489,10 @@ def description
def ignore_allow_blank?
true
end

def expected_type
'boolean'
end
end

class NestedValidator < BaseValidator
Expand Down
43 changes: 0 additions & 43 deletions spec/lib/apipie/generator/swagger/type_extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@
it 'returns an enum type' do
expect(subject).to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:enum])
end

context 'and has `true`, `false` as values' do
let(:param_description_name) { :visible }
let(:enum_values) { [true, false] }

it 'returns a boolean type' do
expect(subject).to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:boolean])
end
end
end
end
end
Expand All @@ -44,38 +35,4 @@

it_behaves_like 'extractable method'
end

describe '#extarct_with_warnings' do
before { Apipie.configuration.generator.swagger.suppress_warnings = false }

subject { extractor.extract_with_warnings(warnings) }

it_behaves_like 'extractable method'

context "when enum validator is used" do
context "and has `true`, `false` as values" do
let(:enum_values) { [true, false] }

let(:validator) do
Apipie::ResponseDescriptionAdapter::PropDesc::Validator.new('some-type', enum_values)
end

context 'and a boolean warning is passed' do
let(:boolean_warning) do
Apipie::Generator::Swagger::Warning.for_code(
Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE,
'SampleController#action',
{ parameter: 'some-param' }
)
end

let(:warnings) { { boolean: boolean_warning } }

it 'outputs the warning' do
expect { subject }.to output(boolean_warning.warning_message).to_stderr
end
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/apipie/generator/swagger/warning_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

let(:warning) do
Apipie::Generator::Swagger::Warning.for_code(
Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE,
Apipie::Generator::Swagger::Warning::PARAM_IGNORED_IN_FORM_DATA_CODE,
'SampleController#action',
{ parameter: 'some-param' }
)
Expand All @@ -32,7 +32,7 @@
context 'when Apipie.configuration.generator.swagger.suppress_warnings includes warning code' do
before do
Apipie.configuration.generator.swagger.suppress_warnings =
Array(Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE)
Array(Apipie::Generator::Swagger::Warning::PARAM_IGNORED_IN_FORM_DATA_CODE)
end

it { is_expected.to be_falsey }
Expand Down
Loading