Skip to content

Commit

Permalink
Handle warnings for param descriptions without a controller method
Browse files Browse the repository at this point in the history
If we're generating swagger via
`SwaggerGenerator.json_schema_for_self_describing_class` we explicitly don't
have a `controller_method` being passed around so we can't use it for the
warnings. Introduce a test for type and builder to cover this scenario (first
spotted by a failing spec for `SwaggerGenerator`) and then change the
implementation to cope with it. Luckily,
`Apipie::Generator::Swagger::MethodDescription::Decorator` already had a
`ruby_name` implementation that handles being given `nil`, so we use that.
  • Loading branch information
h-lame committed Jul 12, 2024
1 parent ba599dc commit 6d84540
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/apipie/generator/swagger/param_description/builder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Apipie::Generator::Swagger::ParamDescription::Builder
# @param [Apipie::ParamDescription] param_description
# @param [TrueClass, FalseClass] in_schema
# @param [Apipie::MethodDescription] controller_method
# @param [Apipie::MethodDescription, nil] controller_method
def initialize(param_description, in_schema:, controller_method:)
@param_description = param_description
@in_schema = in_schema
Expand Down Expand Up @@ -98,7 +98,7 @@ def required_from_path?
def warn_optional_without_default_value(definition)
if !required? && !definition.key?(:default)
method_id =
if @param_description.is_a?(Apipie::ResponseDescriptionAdapter::PropDesc)
if @controller_method.present? && @param_description.is_a?(Apipie::ResponseDescriptionAdapter::PropDesc)
@controller_method.method_name
else
Apipie::Generator::Swagger::MethodDescription::Decorator.new(@controller_method).ruby_name
Expand Down
6 changes: 5 additions & 1 deletion lib/apipie/generator/swagger/param_description/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ def validator
def warn_hash_without_internal_typespec
method_id =
if @param_description.is_a?(Apipie::ResponseDescriptionAdapter::PropDesc)
@controller_method.method_name
if @controller_method.present?
@controller_method.method_name
else
Apipie::Generator::Swagger::MethodDescription::Decorator.new(nil).ruby_name
end
else
Apipie::Generator::Swagger::MethodDescription::Decorator.new(@param_description.method_description).ruby_name
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,33 @@
).to_stderr
end

context 'and param is a prop desc with a delegated controller method' do
context 'and param is a prop desc' do
let(:param_description) do
Apipie.prop(:param, 'object', param_description_options, [])
end

let(:method_desc) do
Apipie::Generator::Swagger::MethodDescription::Decorator.new(
Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
)
context 'with a delegated controller method' do
let(:method_desc) do
Apipie::Generator::Swagger::MethodDescription::Decorator.new(
Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
)
end

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

it 'warns' do
expect { subject }.to output(
/is optional but default value is not specified/
).to_stderr
context 'with a nil controller method' do
let(:method_desc) { nil }

it 'warns' do
expect { subject }.to output(
/is optional but default value is not specified/
).to_stderr
end
end
end
end
Expand Down
24 changes: 17 additions & 7 deletions spec/lib/apipie/generator/swagger/param_description/type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,29 @@
expect { subject }.to output(/is a generic Hash without an internal type specification/).to_stderr
end

context 'and param is a prop desc with a delegated controller method' do
context 'and param is a prop desc' do
let(:param_description) do
Apipie.prop(param_description_name, 'object', {}, [])
end

let(:controller_method) do
Apipie::Generator::Swagger::MethodDescription::Decorator.new(
method_desc
)
context 'with a delegated controller method' do
let(:controller_method) do
Apipie::Generator::Swagger::MethodDescription::Decorator.new(
method_desc
)
end

it 'outputs a hash without internal typespec warning' do
expect { subject }.to output(/is a generic Hash without an internal type specification/).to_stderr
end
end

it 'outputs a hash without internal typespec warning' do
expect { subject }.to output(/is a generic Hash without an internal type specification/).to_stderr
context 'and controller method is nil' do
let(:controller_method) { nil }

it 'outputs a hash without internal typespec warning' do
expect { subject }.to output(/is a generic Hash without an internal type specification/).to_stderr
end
end
end
end
Expand Down

0 comments on commit 6d84540

Please sign in to comment.