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

Example value feature is broken in swagger ui for body parameters #507

Closed
serggl opened this issue Sep 29, 2016 · 3 comments
Closed

Example value feature is broken in swagger ui for body parameters #507

serggl opened this issue Sep 29, 2016 · 3 comments

Comments

@serggl
Copy link
Member

serggl commented Sep 29, 2016

In particular it is broken for parameters which data type is Grape::Entity

Here is a maple API and entities definition:

class Rest::Sales::Root < Grape::API
  prefix :sales

  version 'v1', using: :path

  module Entities
    class A < Grape::Entity
      expose :a, documentation: {type: 'string'}
      expose :b, documentation: {type: 'number'}
    end
  end

  desc 'Create a new something' do
    success Entities::A
  end

  params do
    optional :data, type: Entities::A
  end

  post 'something' do
    {}
  end

  add_swagger_documentation hide_documentation_path: true, add_version: true, models: [Entities::A]
end

And here is how it looks like in swagger:

2016-09-29 16 24 10

2016-09-29 16 25 00

One the last screenshot you can also notice that data type names are quite different, but they should represent the same type.

Here is a swagger json generated for this API class:

{
    "info": {
        "title": "API title",
        "version": "0.0.1"
    },
    "swagger": "2.0",
    "produces": [
        "application/json"
    ],
    "host": "localhost:3000",
    "basePath": "/api",
    "tags": [
        {
            "name": "something",
            "description": "Operations about somethings"
        }
    ],
    "paths": {
        "/sales/v1/something": {
            "post": {
                "summary": "Create a new something",
                "description": "Create a new something",
                "produces": [
                    "application/json"
                ],
                "consumes": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "SalesV1Something",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/postSalesV1Something"
                        }
                    }
                ],
                "responses": {
                    "201": {
                        "description": "Create a new something",
                        "schema": {
                            "$ref": "#/definitions/A"
                        }
                    }
                },
                "tags": [
                    "something"
                ],
                "operationId": "postSalesV1Something"
            }
        }
    },
    "definitions": {
        "postSalesV1Something": {
            "type": "object",
            "properties": {
                "data": {
                    "type": "Rest::Sales::Root::A"
                }
            },
            "description": "Create a new something"
        },
        "A": {
            "type": "object",
            "properties": {
                "a": {
                    "type": "string"
                },
                "b": {
                    "type": "number"
                }
            },
            "description": "Create a new something"
        }
    }
}

Seems like a proper data type link should be "$ref": "#/definitions/A" instead of "type": "Rest::Sales::Root::A"

@LeFnord
Copy link
Member

LeFnord commented Oct 3, 2016

@serggl … mmh, think it has to do with the model_name stuff you commented
did it behaves same, if you remove the models parameter on add_swagger_documentation?

@serggl
Copy link
Member Author

serggl commented Oct 3, 2016

yes @LeFnord. Actually after looking into source code, I can suggest that it has nothing to do with the models option, which is a bit useless since it adds records to definitions section of the output json after everything (request/response/parameters objects) was parsed. PR is on the way..

@serggl
Copy link
Member Author

serggl commented Oct 3, 2016

here is the PR for this #511

@LeFnord LeFnord closed this as completed Oct 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants