Skip to content


Repository files navigation


Swagger is a Ruby library for parsing, building, and traversing Swagger API definitions.


- WARNING: Swagger is currently locked to an old verison of Hashie.
- If you use this gem you may run into version compatiblity issues. Help is needed to resolve the issue (#19).


NOTE: The gem is named swagger-core, because swagger was taken by an unrelated project.

Add this line to your application's Gemfile:

gem 'swagger-core'

And then execute:

$ bundle

Or install it yourself as:

$ gem install swagger-core


  • Structural and semantic validation of Swagger objects
  • Convenient traversal APIs: use hierarchical or flat traversals
  • Handles derived or combined properties, like joining root, path, and operation level property definitions
  • A Swagger::Builder to help create valid Swagger documents from other data



If you're loading a Swagger document from a file, you can use #load. The Swagger version will be detected from the file content.

api = Swagger.load('swagger.yaml')

If you already have the Swagger content loaded as a Hash you can call build, or you can call build and tell Swagger the content is a JSON or YAML string it needs to parse.

api =
# or
api =, format: :json)
# or
api =, format: :yaml)


The parsing methods above all return an API object. The object has a hierarchical object that mirrors the Swagger specification. You can traverse it hierarchically, for example:

# {"tags"=>["Pet Operations"],
# "summary"=>"finds pets in the system",
# "responses"=>
  {"200"=>{"description"=>"pet response", "schema"=>{"type"=>"array", "items"=>{"$ref"=>"#/definitions/Pet"}}, "headers"=>[{"x-expires"=>{"type"=>"string"}}]},
   "default"=>{"description"=>"unexpected error", "schema"=>{"$ref"=>"#/definitions/Error"}}}}

There are also methods available to provide flatter APIs or convenient derived properties. For example:

api.operations.each do | operation |
  puts operation.signature
# GET{id}
# DELETE{id}

See the RDoc documentation for more details.


If you want to build a Swagger document from another structure, you can use the builder. It will validate the structure and data types as you build the Swagger document, but it won't enforce constraints about required Swagger fields until you call Swagger::Builder#build.

builder = Swagger.builder
# or builder = Swagger.builder(version: '2.0')

builder.swagger = 2.0 do |info|
  info.title = 'Sample Swagger API'
  info.version = '1.0'
builder.paths = {
  '/foo' => {}
builder.paths['/foo'].get do |get|
  get.description = 'Testing...'
  get.tags = %w(foo bar)

api =


  • Support Swagger 1.2 - right now only Swagger 2.0 is supported
  • Better handling of $ref
  • Handle combined parameters, respones, etc


  1. Fork it ([my-github-username]/swagger/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request