Skip to content

Commit

Permalink
add allowedValues and defaultValues + remove format
Browse files Browse the repository at this point in the history
  • Loading branch information
ManjunathaN committed Nov 9, 2012
1 parent 706a215 commit 2aeaae0
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/grape-swagger.rb
Expand Up @@ -146,13 +146,17 @@ def parse_header_params(params)
dataType = 'String'
description = value.is_a?(Hash) ? value[:description] : ''
required = value.is_a?(Hash) ? !!value[:required] : false
allowableValues = value[:allowableValues] if value.is_a?(Hash) && value[:allowableValues]
defaultValue = value[:defaultValue] if value.is_a?(Hash) && value[:defaultValue]
paramType = "header"
{
paramType: paramType,
name: param,
description: description,
dataType: dataType,
required: required
required: required,
allowableValues: allowableValues,
defaultValue: defaultValue
}
end
else
Expand All @@ -162,7 +166,8 @@ def parse_header_params(params)

def parse_path(path, version)
# adapt format to swagger format
parsed_path = path.gsub('(.:format)', '.{format}')
# parsed_path = path.gsub('(.:format)', '.{format}')
parsed_path = path.gsub('(.:format)', '')
# This is attempting to emulate the behavior of
# Rack::Mount::Strexp. We cannot use Strexp directly because
# all it does is generate regular expressions for parsing URLs.
Expand Down

3 comments on commit 2aeaae0

@spier
Copy link

@spier spier commented on 2aeaae0 Apr 25, 2013

Choose a reason for hiding this comment

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

I was looking for the allowableValues option as well, which is how I found your work here. How do I use it in a grape API?

I tried this but it did not work:

      params do
        optional :selector, :type => String, :allowableValues => [1,2,3], :desc => "selector."
      end

@ManjunathaN
Copy link
Owner Author

Choose a reason for hiding this comment

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

Sample that works for me.

:params =>
{
"gender" => {
:desc => "gender; 0-Unspecified, 1-Male, 2-Female",
:type => "Integer" ,
:allowableValues => {
:valueType => "LIST",
:values => [ 0, 1, 2 ]
} ,
:defaultValue => 0
}
}

@spier
Copy link

@spier spier commented on 2aeaae0 Apr 26, 2013

Choose a reason for hiding this comment

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

I think I am still missing something here. What you posted looks like the JSON that grape-swagger will generate based on the annotations in the ruby code. Isn't it?

I tried the following:

  desc "do something"
  params do
    optional :gender, :type => Integer, :allowableValues => {:valueType => "LIST", :values => [1,2,3]}, :desc => "gender; 0-Unspecified, 1-Male, 2-Female."
  end
  post "do_something" do
    #do something
  end

Then I get an error saying:

 .../.rvm/gems/ruby-1.9.3-p194/gems/grape-0.2.6/lib/grape/validations.rb:170:in `validate': unknown validator: allowableValues (RuntimeError)

So that is an exception from grape, not from grape-swagger.

So far I thought that grape-swagger just uses the normal grape params and descriptions to generate the JSON file that is needed by swagger. Or not?

Please sign in to comment.