grape-swagger icon indicating copy to clipboard operation
grape-swagger copied to clipboard

require and optional with block

Open ChloePlanet opened this issue 8 years ago • 4 comments

First a part of my definition code,

      params do
        requires :id, type: Integer
        optional :text, type: String, regexp: /\A[a-z]+\z/
        group :group_params, type: Hash do
          requires :require_param
          optional :optional_param
        end
        requires :requires_params, type: Hash do
          optional :optional_param1
          optional :optional_param2
        end
        optional :optional_params, type: Hash do
          requires :require_param
          optional :optional_param
        end
      end

From my perspective,

  • group_params[require_param] is required,
  • group_params[optional_param], require_params[optional_param1], require_params[optional_param2], optional_params[require_param] and optional_params[optional_param] is not required.
  • If I pass optional_params[optional_param], then optional_params[require_param] is required.


Part of the instance variable @options of API

   :forward_match => nil,
           :suffix => "(.:format)",
           :params => {
                                      "id" => {
            :required => true,
                :type => "Integer"
        },
                                    "text" => {
            :required => false,
                :type => "String"
        },
                            "group_params" => {
            :required => true,
                :type => "Hash"
        },
             "group_params[require_param]" => {
            :required => true
        },
            "group_params[optional_param]" => {
            :required => false
        },
                         "requires_params" => {
            :required => true,
                :type => "Hash"
        },
        "requires_params[optional_param1]" => {
            :required => false
        },
        "requires_params[optional_param2]" => {
            :required => false
        },
                         "optional_params" => {
            :required => false,
                :type => "Hash"
        },
          "optional_params[require_param]" => {
            :required => true
        },
         "optional_params[optional_param]" => {
            :required => false
        }
    }


But what I got from the swagger documentation was as below:

parameters": [

    {
        "in": "query",
        "name": "id",
        "type": "integer",
        "format": "int32",
        "required": true
    },
    {
        "in": "query",
        "name": "text",
        "type": "string",
        "required": false
    },
    {
        "in": "query",
        "name": "group_params[require_param]",
        "type": "string",
        "required": true
    },
    {
        "in": "query",
        "name": "group_params[optional_param]",
        "type": "string",
        "required": false
    },
    {
        "in": "query",
        "name": "requires_params[optional_param1]",
        "type": "string",
        "required": false
    },
    {
        "in": "query",
        "name": "requires_params[optional_param2]",
        "type": "string",
        "required": false
    },
    {
        "in": "query",
        "name": "optional_params[require_param]",
        "type": "string",
        "required": true
    },
    {
        "in": "query",
        "name": "optional_params[optional_param]",
        "type": "string",
        "required": false
    }

],

ChloePlanet avatar May 24 '17 06:05 ChloePlanet

… and what is the issue?

LeFnord avatar May 24 '17 08:05 LeFnord

Sorry...Didn't describe clearly...

I want to test this API and don't want to pass the optional_params[require_param] parameter, but can't do that. default

ChloePlanet avatar May 24 '17 09:05 ChloePlanet

it seems like correct behavior, but to ensure that, how did behave grape itself, gives it an validation error, if don't submit optional_params[require_param], if yes, then it is correct

LeFnord avatar May 31 '17 18:05 LeFnord

Hello :wave: I'm having the same issue and it seems to me that this isn't the correct behavior. If the hash key itself is optional (like :optional_params here), all the params under it should also be marked as optional in Swagger. Grape will only check their presence when the hash key is provided :wink: See docs

jerryskye avatar Jun 03 '20 14:06 jerryskye