notonthehighstreet / svelte

Dynamic Ruby API Client from Swagger JSON Spec
MIT License
48 stars 17 forks source link

Error when calling end point with no parameters #26

Open eflukx opened 4 years ago

eflukx commented 4 years ago

Seems that PR #20 breaks calling end points that have no parameters.

When doing so I get a NoMethodError: undefined method 'reduce' for nil:NilClass originating from operation_builder.rb:61:in strip_headers!

It seems that in that case operation_parameters == nil is not handled. Made a quick fix for myself to get things working again, setting operation_parameters to [] when nil

Although this patch makes thins work for me now, probably not the best method? After having a quick look at the code, I presume that strip_headers! may just return nil when operation_parameters == nil.

@telekid As the author of the PR, maybe you can have a look at this? Very much appreciated!

def strip_headers!(operation_parameters:, request_parameters:)
  operation_parameters ||= [] # <--- 'hack' handling nil values
  header_names = operation_parameters.reduce([]) do |memo, param|
    memo.push(param["name"].downcase) if param["in"] == 'header'
    memo
  end

  headers = request_parameters.select { |key, val| header_names.include?(key)}
  request_parameters.reject! { |key, val| header_names.include?(key) }
  headers.empty? ? nil : headers
end
slithernix commented 4 years ago

Came here to post this