nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.64k stars 334 forks source link

How to use rabl with swagger? #707

Open arparthasarathi opened 6 years ago

arparthasarathi commented 6 years ago

I am using grape-swagger and swagger-ui for documentation of REST APIs in my RAILS application. I would like to document the response examples for the APIs. I couldn't find a way to use Rabl along with Swagger to document my JSON responses.

EpiphanyMachine commented 5 years ago

Its possible, but it wasn't really simple:

We use a custom rake tasks that builds response examples using rabl + factory bot

We use factory girl to seed data based on a custom "for_docs" trait. Then loop through our rabl files and generate a response json from rabl for each of our views and save them as files. Those files are referenced by name in our api definitions swagger yaml file. Finally we combine all separate/referenced files into one and use that as our swagger file.

Here are some code snippets. We do this over a several different files so its not an easy copy paste of the full code.

      Rabl.render(
        object,
        @public_api_document.template_short_path,
        view_path: HintApi::Application.root.join('app', 'views'),
        format: :hash,
      )
built = JSONRef.new('index.yaml', input_format: :yaml, base_path: File.join(Rails.root, 'public', 'swagger')).expand
...
file.write(JSON.pretty_generate(built))
Amerr commented 5 years ago

@EpiphanyMachine is it possible to give a detailed explanation or separate repo illustrating this implementation .