Skip to content

Instantly share code, notes, and snippets.

@shawnbot
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnbot/759ea4d735cc11aa6269 to your computer and use it in GitHub Desktop.
Save shawnbot/759ea4d735cc11aa6269 to your computer and use it in GitHub Desktop.

swagger-static

This document is largely superceded by the swagger-static repo, where I'm now planning a project. GitHub won't notify me of comments here, so if you want to get in touch please comment on or file an issue. Thanks!

Background

At 18F we're implementing Swagger-compliant APIs to make them more accessible and facilitate simpler documentation. Unfortunately, the only generally accepted way of exposing human-readable Swagger API documentation is SwaggerUI, a client-side JavaScript library with some serious issues:

  1. Because all SwaggerUI-based documentation is rendered at runtime, search engines are [theoretically] unable to crawl their contents.
  2. Tight coupling with jQuery and a long list of dependencies means that you've got to include everything they include, or it just won't run.
  3. Tight coupling between the distributed JavaScript source and HTML, e.g. lots of hard-coded CSS selectors means that it's a huge pain to customize the output.
  4. Unweildy and largely redundant stylesheets, which are tricky to override and impractical to rewrite.

The Plan

My plan is to make a suite of tools and templates that faciliate the generation of static, human-readable documentation to replace SwaggerUI. At this point, I've made two repos:

  1. swagger-template, just some HTML files that should theoretically be useful for generating static HTML documentation for a Swagger-compliant API.
  2. swagger-enhance, a little Node utility for grabbing a Swagger API's JSON and "enhancing" it with JSON data from each of its own endpoints (confusingly, "apis", in Swagger parlance).

The missing piece is a tool I'm referring to as swagger-static, which would take a swagger-enhanced JSON representation of an API's metadata and a Django-style template to produce static docs in whatever form you want: primarily HTML, but Markdown might also be useful for Jekyll sites.

Here's how I'm imagining that this could work, using the Discovery API as an example:

swagger-enhance --pretty \
    "https://discovery.18f.us/docs/api-docs/api/" > discovery.json
swagger-static \
    --template path/to/swagger-template/index.html \
    discovery.json > discovery.html

Ideally, swagger-static would bring the swagger-template templates with it (e.g., as an npm dependency), then allow you to selectively override one or all of them. Then it would work out of the box without the --template flag.

I haven't thought through styling the HTML yet, but here are some options:

  1. Just bundle a stylesheet with swagger-template and let people copy it as needed.

  2. Make a CDN-hosted default stylesheet and reference that in the swagger-template.

  3. Ignore styling altogether and let swagger-static users specify stylesheets in a custom template, e.g.:

    {% extend "swagger.html" %}
    {% block css %}
    <link rel="stylesheet" href="path/to/custom.css">
    {% endblock %}
@shawnbot
Copy link
Author

shawnbot commented Feb 2, 2015

Note: currently the swagger root template is named index.html, but I've referred to it less ambiguously in the custom stylesheet example as swagger.html.

@gbinal
Copy link

gbinal commented Feb 2, 2015

❤️

@kinlane
Copy link

kinlane commented Feb 2, 2015

This is huge you guys. Represents very much where we need to go with the machine readable API definition generated UI. Swagger UI is such a bitch to use, and even more of a bitch to customize.

When we have whole suite of simple, easy to deploy templates like you are proposing, driven from swagger, we will reach a tipping point in deployments. The value will be there for mainstream users.

@kinlane
Copy link

kinlane commented Feb 2, 2015

Here are some of my evolution thoughts on this, going beyond just documentation UI - http://apievangelist.com/2014/10/24/someone-please-build-my-open-interactive-portable-and-visual-api-documentation-toolkit/

@kinlane
Copy link

kinlane commented Feb 2, 2015

I also hope other API documentation UI providers like Slate - https://github.com/tripit/slate adopt a template approach like you guys propose. I will make time to play with as you evolve, and give feedback where I can.

@shawnbot
Copy link
Author

shawnbot commented Feb 2, 2015

Thanks so much for the feedback, @kinlane! I'm planning this out in a separate repo.

@nvembar
Copy link

nvembar commented Feb 2, 2015

This is great! There was definitely a struggle when we were trying to use Swagger - this will be great for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment