Skip to content

Adding swagger documentation for multiple Grape APIs in single Rails app #28

Closed
@jhecking

Description

@jhecking

In our Rails app we are mounting two separate Grape APIs, e.g. /api/partner/v1 and /api/clients/v1. Now when I call add_swagger_documentation for each of these APIs, two separate swagger_doc entry points are mounted correctly. However querying these two entry points returns the same API documentation. E.g. querying /api/partner/v1/swagger_doc.json returns the documentation for API::Partner but querying /api/clients/v1/swagger_doc.json also returns the documentation for API::Partner.

I've been doing some debugging on this issue and it seems that the @@target_class class variable points to the same API class in the get @@mount_path definition:

get @@mount_path do
  Rails.logger.debug("XXXX self class: #{self.class}, #{self.class.object_id}")
  header['Access-Control-Allow-Origin'] = '*'
  header['Access-Control-Request-Method'] = '*'
  routes = @@target_class::combined_routes
  Rails.logger.debug("XXXX routes for #{@@target_class}: #{routes}")

This is despite the fact that two separate documentation classes get created with separate class variable. Somehow these class variables get merged into a single new instance of the Grape::Endpoint class. Here is what I see in the logs:

Started GET "/api/client/v1/client_api_docs.json" for 127.0.0.1 at 2013-03-06 01:38:24 +0800
XXXX self class: Grape::Endpoint, 70097079507800
XXXX routes for API::Client: {"search"=>[version=v1, method=GET, path=/:version/search(.:format)]}

Started GET "/api/partner/v1/partner_api_docs.json" for 127.0.0.1 at 2013-03-06 01:19:14 +0800
XXXX self class: Grape::Endpoint, 70097079507800
XXXX routes for API::Client: {"client_api_docs"=>[version=v1, method=GET, path=/:version/client_api_docs(.:format), version=v1, method=GET, path=/:version/client_api_docs/:name(.:format)], "search"=>[version=v1, method=GET, path=/:version/search(.:format)]}

Maybe this data shouldn't be stored in class variables?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions