Skip to content

Commit

Permalink
fix: openapi document errors.
Browse files Browse the repository at this point in the history
* fix: Openapi document errors. Set default parameter values, not render nil or empty hash/array. Status code 422 name.
  • Loading branch information
a-chacon authored Aug 15, 2024
1 parent 8d32054 commit df4fb94
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
9 changes: 6 additions & 3 deletions lib/oas_rails/spec/parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Parameter

STYLE_DEFAULTS = { query: 'form', path: 'simple', header: 'simple', cookie: 'form' }.freeze

attr_accessor :name, :in, :style, :description, :required, :schema
attr_accessor :name, :style, :description, :required, :schema
attr_reader :in

def initialize(specification)
@specification = specification
Expand All @@ -18,8 +19,10 @@ def initialize(specification)
@schema = { type: 'string' }
end

def default_from_in
STYLE_DEFAULTS[@in.to_sym]
def in=(value)
@in = value
@style = STYLE_DEFAULTS[@in.to_sym]
@required = true if value == "path"
end

def required?
Expand Down
9 changes: 5 additions & 4 deletions lib/oas_rails/spec/specable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def to_spec
elsif value.is_a?(Array) && value.all? { |elem| elem.respond_to?(:to_spec) }
value.map(&:to_spec)
# elsif value.is_a?(Hash)
# p "Here"
# p value
# hash = {}
# value.each do |key, object|
# hash[key] = object.to_spec
Expand All @@ -29,8 +27,7 @@ def to_spec
value
end

# hash[camel_case_key] = processed_value unless (processed_value.is_a?(Hash) || processed_value.is_a?(Array)) && processed_value.empty?
hash[camel_case_key] = processed_value unless processed_value.nil?
hash[camel_case_key] = processed_value unless valid_processed_value?(processed_value)
end
hash
end
Expand All @@ -43,6 +40,10 @@ def as_json(options = nil)

private

def valid_processed_value?(processed_value)
((processed_value.is_a?(Hash) || processed_value.is_a?(Array)) && processed_value.empty?) || processed_value.nil?
end

def snake_to_camel(snake_str)
words = snake_str.to_s.split('_')
words[1..].map!(&:capitalize)
Expand Down
2 changes: 1 addition & 1 deletion lib/oas_rails/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def status_to_integer(status)
if status.to_s =~ /^\d+$/
status.to_i
else
status = "unprocessable_content" if status == "unprocessable_entity"
# status = "unprocessable_content" if status == "unprocessable_entity"
Rack::Utils::SYMBOL_TO_STATUS_CODE[status.to_sym]
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def index
# @summary Get a user by id.
#
# This method show a User by ID. The id must exist of other way it will be returning a 404.
# @parameter id(path) [Integer] Used for identify the user.
# @parameter id(path) [Integer!] Used for identify the user.
# @response A nice user(200) [Hash] {user: {name: String, email: String, created_at: DateTime }}
# @response User not found by the provided Id(404) [Hash] {success: Boolean, message: String}
# @response You dont have the rigth persmissions for access to this reasource(403) [Hash] {success: Boolean, message: String}
Expand Down

0 comments on commit df4fb94

Please sign in to comment.