From ae754456badd77f0008d1b3850b1b9b36ef5b5be Mon Sep 17 00:00:00 2001 From: Panos Dalitsouris <22100396+PanosCodes@users.noreply.github.com> Date: Tue, 19 Dec 2023 09:38:01 +0200 Subject: [PATCH] Use array items type from validator (#904) * Fix Rubocop offenses * Use array items type from validator --- lib/apipie/dsl_definition.rb | 6 +++--- lib/apipie/errors.rb | 2 +- lib/apipie/extractor/collector.rb | 2 +- lib/apipie/extractor/recorder.rb | 2 +- lib/apipie/extractor/writer.rb | 2 +- lib/apipie/generator/swagger/config.rb | 2 +- .../method_description/parameters_service.rb | 2 +- .../generator/swagger/param_description/type.rb | 17 +++++++++++++++-- lib/tasks/apipie.rake | 6 +++--- spec/lib/apipie/extractor_spec.rb | 2 +- .../swagger/param_description/type_spec.rb | 7 ++++++- spec/lib/swagger/rake_swagger_spec.rb | 2 +- 12 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lib/apipie/dsl_definition.rb b/lib/apipie/dsl_definition.rb index b8f2e436..03cfe496 100644 --- a/lib/apipie/dsl_definition.rb +++ b/lib/apipie/dsl_definition.rb @@ -242,7 +242,7 @@ def _apipie_define_validators(description) if Apipie.configuration.validate_presence? Validator::BaseValidator.raise_if_missing_params do |missing| - method_params.each do |_, param| + method_params.each_value do |param| # check if required parameters are present missing << param if param.required && !params.key?(param.name) end @@ -250,7 +250,7 @@ def _apipie_define_validators(description) end if Apipie.configuration.validate_value? - method_params.each do |_, param| + method_params.each_value do |param| # params validations param.validate(params[:"#{param.name}"]) if params.key?(param.name) end @@ -269,7 +269,7 @@ def _apipie_define_validators(description) return unless Apipie.configuration.process_value? @api_params ||= {} - method_params.each do |_, param| + method_params.each_value do |param| # params processing @api_params[param.as] = param.process_value(params[:"#{param.name}"]) if params.key?(param.name) end diff --git a/lib/apipie/errors.rb b/lib/apipie/errors.rb index df585ed7..ac287c20 100644 --- a/lib/apipie/errors.rb +++ b/lib/apipie/errors.rb @@ -62,7 +62,7 @@ class ParamInvalid < DefinedParamError attr_accessor :value, :error def initialize(param, value, error) - super param + super(param) @value = value @error = error end diff --git a/lib/apipie/extractor/collector.rb b/lib/apipie/extractor/collector.rb index ed841d99..599cdbb2 100644 --- a/lib/apipie/extractor/collector.rb +++ b/lib/apipie/extractor/collector.rb @@ -89,7 +89,7 @@ def refine_params_description(params_desc, recorded_params) end def finalize_descriptions - @descriptions.each do |method, desc| + @descriptions.each_value do |desc| add_routes_info(desc) end return @descriptions diff --git a/lib/apipie/extractor/recorder.rb b/lib/apipie/extractor/recorder.rb index 500182d5..24816343 100644 --- a/lib/apipie/extractor/recorder.rb +++ b/lib/apipie/extractor/recorder.rb @@ -90,7 +90,7 @@ def reformat_hash(boundary, attrs, lines) lines << '' << %{... contents of "#{attrs[:name]}" ...} else # Look for subelements that contain a part. - attrs.each { |k,v| v.is_a?(Hash) and reformat_hash(boundary, v, lines) } + attrs.each_value { |v| v.is_a?(Hash) and reformat_hash(boundary, v, lines) } end end diff --git a/lib/apipie/extractor/writer.rb b/lib/apipie/extractor/writer.rb index 5b0b4245..4ede1ca8 100644 --- a/lib/apipie/extractor/writer.rb +++ b/lib/apipie/extractor/writer.rb @@ -80,7 +80,7 @@ def write_examples def write_docs descriptions = @collector.finalize_descriptions - descriptions.each do |_, desc| + descriptions.each_value do |desc| if desc[:api].empty? logger.warn("REST_API: Couldn't find any path for #{desc_to_s(desc)}") next diff --git a/lib/apipie/generator/swagger/config.rb b/lib/apipie/generator/swagger/config.rb index 73062f06..d77df5be 100644 --- a/lib/apipie/generator/swagger/config.rb +++ b/lib/apipie/generator/swagger/config.rb @@ -24,7 +24,7 @@ class Config HEREDOC ) - send("#{attribute}=", value) + send(:"#{attribute}=", value) end old_setter_method = "swagger_#{attribute}" diff --git a/lib/apipie/generator/swagger/method_description/parameters_service.rb b/lib/apipie/generator/swagger/method_description/parameters_service.rb index 9524e430..0b7a35dd 100644 --- a/lib/apipie/generator/swagger/method_description/parameters_service.rb +++ b/lib/apipie/generator/swagger/method_description/parameters_service.rb @@ -96,7 +96,7 @@ def body_param_descriptions def path_param_descriptions @path_param_descriptions ||= all_params .select { |k, _| @path.param?(k) } - .each { |_, desc| desc.required = true } + .each_value { |desc| desc.required = true } .values end diff --git a/lib/apipie/generator/swagger/param_description/type.rb b/lib/apipie/generator/swagger/param_description/type.rb index cbf9bde1..f35c49b7 100644 --- a/lib/apipie/generator/swagger/param_description/type.rb +++ b/lib/apipie/generator/swagger/param_description/type.rb @@ -56,7 +56,7 @@ def type def for_array_type validator_opts = validator.param_description.options - items_type = validator_opts[:of].to_s || validator_opts[:array_of].to_s + items_type = (validator_opts[:of] || validator_opts[:array_of]).to_s if items_type == 'Hash' && params_in_body_use_reference? reference_name = Apipie::Generator::Swagger::OperationId. @@ -67,7 +67,7 @@ def for_array_type '$ref' => reference_name } else - items = { type: 'string' } + items = { type: array_items_type(items_type).to_s } end enum = @param_description.options[:in] @@ -80,6 +80,19 @@ def for_array_type } end + # @param [String] items_type + # + # @return [Apipie::Generator::Swagger::Type] + def array_items_type(items_type) + type = Apipie::Generator::Swagger::TypeExtractor::TYPES[items_type.downcase.to_sym] + + if type == 'object' || type.blank? + Apipie::Generator::Swagger::TypeExtractor::TYPES[:string] + else + type + end + end + def for_enum_type { type: 'string', diff --git a/lib/tasks/apipie.rake b/lib/tasks/apipie.rake index 5003f214..be7e3a2e 100644 --- a/lib/tasks/apipie.rake +++ b/lib/tasks/apipie.rake @@ -186,11 +186,11 @@ namespace :apipie do av = renderer File.open(file_name, "w") do |f| variables.each do |var, val| - av.instance_variable_set("@#{var}", val) + av.instance_variable_set(:"@#{var}", val) end f.write av.render( :template => "#{template}", - :layout => (layout && "apipie/#{layout}")) + :layout => layout && "apipie/#{layout}") end end @@ -259,7 +259,7 @@ namespace :apipie do end def generate_resource_pages(version, file_base, doc, include_json = false, lang = nil) - doc[:docs][:resources].each do |resource_id, _| + doc[:docs][:resources].each_key do |resource_id| resource_file_base = File.join(file_base, resource_id.to_s) FileUtils.mkdir_p(File.dirname(resource_file_base)) unless File.exist?(File.dirname(resource_file_base)) diff --git a/spec/lib/apipie/extractor_spec.rb b/spec/lib/apipie/extractor_spec.rb index b29d0452..038b6dbc 100644 --- a/spec/lib/apipie/extractor_spec.rb +++ b/spec/lib/apipie/extractor_spec.rb @@ -2,7 +2,7 @@ describe Apipie::Extractor do it 'handles routes without (.:format)' do - Apipie::Extractor.apis_from_routes.each do |(controller, action), apis| + Apipie::Extractor.apis_from_routes.each_value do |apis| apis.each { |api| expect(api[:path]).not_to be_nil } end 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 fd190edf..dbbe34f0 100644 --- a/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +++ b/spec/lib/apipie/generator/swagger/param_description/type_spec.rb @@ -115,10 +115,15 @@ it 'returns the reference' do expect(subject).to eq({ '$ref' => reference }) end - end end + context 'of a Swagger type' do + let(:validator_options) { { of: Integer } } + + it { is_expected.to eq({ type: 'integer' }) } + end + describe 'enum' do subject { items[:enum] } diff --git a/spec/lib/swagger/rake_swagger_spec.rb b/spec/lib/swagger/rake_swagger_spec.rb index 42606544..6817956c 100644 --- a/spec/lib/swagger/rake_swagger_spec.rb +++ b/spec/lib/swagger/rake_swagger_spec.rb @@ -151,7 +151,7 @@ def expect_response_params_def(http_method, path, response_code, param_name, fie describe 'apipie:did_swagger_change[development,form_data,_tmp]' do it "keeps a reference file" do - expect(Pathname(ref_output).children.count).to eq(2) # one file for each language + expect(Pathname(ref_output).children.count).to eq(2) # one file for each language end end end