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

Params extension does not work when param_type is body #901

Closed
lenon opened this issue May 26, 2023 · 2 comments
Closed

Params extension does not work when param_type is body #901

lenon opened this issue May 26, 2023 · 2 comments

Comments

@lenon
Copy link

lenon commented May 26, 2023

Hi!

When I try to document an extension for a parameter like this:

requires :foo, type: String, documentation: { x: { some: 'stuff' }, param_type: 'body' }

It does not work. The documentation is generated without the x-some extension.

But when param_type is omitted:

requires :foo, type: String, documentation: { x: { some: 'stuff' } }

The generated documentation includes the x-some extension.

I wrote the following script to help reproduce this problem:

require 'grape'
require 'grape-swagger'

class API < Grape::API
  format :json

  params do
    requires :foo, type: String, documentation: { x: { some: 'stuff' } }
  end
  post :test1 do
  end

  params do
    requires :foo, type: String, documentation: { x: { some: 'stuff' }, param_type: 'body' }
  end
  post :test2 do
  end

  add_swagger_documentation
end

This is the generated documentation:

(I redacted it to keep only the relevant content)

{
    ...
    "paths": {
        "/test1": {
            "post": {
                ...
                "parameters": [
                    {
                        "in": "formData",
                        "name": "foo",
                        "type": "string",
                        "required": true,
                        "x-some": "stuff"
                    }
                ],
                ...
            }
        },
        "/test2": {
            "post": {
                ...
                "parameters": [
                    {
                        "name": "postTest2",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/postTest2"
                        }
                    }
                ],
                ...
            }
        }
    },
    "definitions": {
        "postTest2": {
            "type": "object",
            "properties": {
                "foo": {
                    "type": "string"
                }
            },
            "required": [
                "foo"
            ]
        }
    }
}

As you can see in the JSON, when in is formData, "x-some": "stuff" is present. When in is body, it isn't.

I think this happens because of this method in the MoveParams class, which appears to be used to document body params:

def document_as_property(param)
property_keys.each_with_object({}) do |x, memo|
next unless param.key?(x)
value = param[x]
if x == :type && @definitions[value].present?
memo['$ref'] = "#/definitions/#{value}"
else
memo[x] = value
end
end
end

It uses a fixed list of properties from property_keys and documents them if they are present. Anything else that is not in this list gets discarded.

Maybe it would be possible to change this method to check for keys that start with x- and document them.

Does that make sense? I can try to make a PR if it does.

Thanks!

@LeFnord
Copy link
Member

LeFnord commented Jun 6, 2023

Hi @lenon … thanks for the detailed bug report, and yes I PR would be very welcome … 👍

@lenon
Copy link
Author

lenon commented Feb 5, 2024

Fixed by #918. Thanks @numbata 😃

@lenon lenon closed this as completed Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants