From e1fd88ae5667e2f30fb7782aa852ac8ae20d5a86 Mon Sep 17 00:00:00 2001 From: Murray Steele Date: Mon, 24 Jun 2024 17:34:34 +0100 Subject: [PATCH] Use `method_name` instead of `method` when generating hash warnings In https://github.com/Apipie/apipie-rails/pull/865 we introduced a `method_name` method on `MethodDescription` to avoid this bug, but the commit didn't actually use that method. Sometimes the `@controller_method` object used is a `Apipie::Generator::Swagger::MethodDescription::Decorator` which is a `SimpleDelegate` onto a `Apipie::MethodDescription` and we'd expect to be able to call `method` on it, but unfortunately `method` is one of the things _not_ delegated by `SimpleDelegate` because it's a standard ruby method and so you get ArgumentError: wrong number of arguments (given 0, expected 1) when you try to call it. Using `method_name` instead avoids that so that's what we do - and now we can happily generate the swagger warnings when we have hash type objects without defined params. --- .../swagger/param_description/type.rb | 2 +- .../swagger/param_description/type_spec.rb | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/apipie/generator/swagger/param_description/type.rb b/lib/apipie/generator/swagger/param_description/type.rb index f35c49b7..50443b72 100644 --- a/lib/apipie/generator/swagger/param_description/type.rb +++ b/lib/apipie/generator/swagger/param_description/type.rb @@ -114,7 +114,7 @@ def validator def warn_hash_without_internal_typespec method_id = if @param_description.is_a?(Apipie::ResponseDescriptionAdapter::PropDesc) - @controller_method.method + @controller_method.method_name else Apipie::Generator::Swagger::MethodDescription::Decorator.new(@param_description.method_description).ruby_name end diff --git a/spec/lib/apipie/generator/swagger/param_description/type_spec.rb b/spec/lib/apipie/generator/swagger/param_description/type_spec.rb index dbbe34f0..93999b4d 100644 --- a/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +++ b/spec/lib/apipie/generator/swagger/param_description/type_spec.rb @@ -63,9 +63,11 @@ ) end + let(:controller_method) { 'index' } + let(:type_definition) do described_class. - new(param_description, with_null: with_null, controller_method: 'index'). + new(param_description, with_null: with_null, controller_method: controller_method). to_hash end @@ -178,6 +180,22 @@ 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 + + context 'and param is a prop desc with a delegated controller method' do + let(:param_description) do + Apipie.prop(param_description_name, 'object', {}, []) + end + + 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 end end end