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

Ability to describe multiple success entities #2277

Open
Koilanetroc opened this issue Aug 11, 2022 · 6 comments
Open

Ability to describe multiple success entities #2277

Koilanetroc opened this issue Aug 11, 2022 · 6 comments

Comments

@Koilanetroc
Copy link

Sometimes it's necessary to return different entities depending on internal business logic. It would be nice to be able to document all possible success entities. Like with failure method but without status codes.
Something like this:

desc "List of users" do
  success [Entities::Customer, Entities::Client]
  is_array true
end
@dblock
Copy link
Member

dblock commented Aug 12, 2022

I dislike APIs that return two different types of things, but it's a matter of preference, so the proposal makes sense. Want to give it a try?

@Koilanetroc
Copy link
Author

Yeah, I'd like to try. Will send a pr when it's ready

@Koilanetroc
Copy link
Author

Well, after some research I realised that limitation was from OpenAPI 2.0. It does not support multiple response schemas per status code.
There is a branch in grape-swagger for oapi-3, but it's abandoned, so there is no way to add this functionality for now.

@dblock
Copy link
Member

dblock commented Aug 25, 2022

But is it a grape-swagger problem or a grape problem? Can we do one without the other?

@Koilanetroc
Copy link
Author

Koilanetroc commented Aug 27, 2022

Grape-swagger actually is able to accept an array in success section, but then in response_object hash is used for storing entities where response code is a key. So if you pass an array of multiple entities with same response codes like this:

  desc 'user' do
    success [{ code: 200, model: Entities::User }, { code: 200, model: Entities::Admin }]
  end

In swagger you'll see only Entities::Admin as a result for 200. If you pass entities with different codes they will appear in swagger correctly.
As I said before, there is a limitation from openapi 2.0. It also uses response code as a key:

  "paths": {
    "/user": {
      "get": {
        "description": "user",
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "user",
            "schema": {
              "$ref": "#/definitions/User"
            }
          }
        },
        "tags": [
          "user"
        ],
        "operationId": "getUser"
      }
    }
  }

And you can't pass an array to "200" key, this option is available only in openapi 3

@dblock
Copy link
Member

dblock commented Aug 28, 2022

Looks like a bug in grape-swagger, which should raise an error here, short of supporting openapi 3.

For Grape though I don't see a reason not to implement it. If someone ends up using it and swagger has a problem with it, 🤷

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