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

Use array items type from validator #904

Merged
merged 2 commits into from
Dec 19, 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
6 changes: 3 additions & 3 deletions lib/apipie/dsl_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,15 @@ 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
end
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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/extractor/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/extractor/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/extractor/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/generator/swagger/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Config
HEREDOC
)

send("#{attribute}=", value)
send(:"#{attribute}=", value)
end

old_setter_method = "swagger_#{attribute}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 15 additions & 2 deletions lib/apipie/generator/swagger/param_description/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -67,7 +67,7 @@ def for_array_type
'$ref' => reference_name
}
else
items = { type: 'string' }
items = { type: array_items_type(items_type).to_s }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

end

enum = @param_description.options[:in]
Expand All @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/apipie.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/apipie/extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' }) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

end

describe 'enum' do
subject { items[:enum] }

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/swagger/rake_swagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down