Generates documentation for your REST API from annotations
Find a file
Jordi Boggiano 6aac4a001e Merge pull request #83 from lsmith77/patch-2
also support symfony 2.2
2012-10-15 05:04:47 -07:00
Annotation finished up, tests passing, fixed cs 2012-08-27 13:25:03 -04:00
Command Add new option to the command line: --no-sandbox 2012-07-18 16:51:53 +02:00
Controller Fixed rendering issue when used with FOSRestBundle and configured to not 2012-08-23 15:09:56 +02:00
DependencyInjection Merge pull request #64 from asm89/sandbox-api-key 2012-08-23 08:24:33 -07:00
EventListener Refactoring 2012-07-20 01:32:16 +02:00
Extractor Merge pull request #75 from evillemez/param_descriptions 2012-09-10 04:08:11 -07:00
Form/Extension Fixed BC Break from master (commit d072f35ea0 ) 2012-07-23 13:01:23 +03:00
Formatter implemented nested parameter handling in AbstractFormatter, updated MarkdownFormatter and HtmlFormatter to use it 2012-09-10 09:46:02 -04:00
Parser fix cs 2012-10-05 09:36:43 +02:00
Resources do not support form types with required options 2012-10-04 10:37:12 +02:00
Tests implemented nested parameter handling in AbstractFormatter, updated MarkdownFormatter and HtmlFormatter to use it 2012-09-10 09:46:02 -04:00
Twig/Extension Fix Twig filter name 2012-07-20 17:38:39 +02:00
Util abstracted docblock comment extraction, implemented in JmsMetadataParser to get parameter descriptions 2012-08-31 14:57:42 -04:00
.gitignore Added phpunit.xml to the ignore file 2012-05-23 00:43:18 +02:00
.travis.yml Fix travis-ci config 2012-08-06 12:04:42 +02:00
composer.json still allow 2.1 2012-10-14 01:06:56 +03:00
LICENSE Refactoring 2012-07-20 01:32:16 +02:00
NelmioApiDocBundle.php tests passing with JmsMetadataParser, but work still to do on required and description properties 2012-08-07 21:47:33 -04:00
phpunit.xml.dist Ignore vendor in code coverage 2012-04-13 15:21:45 +02:00
README.md updated documentation to reflect return property, updated markdown formatter tests to reflect parameter readonly property 2012-08-28 16:43:47 -04:00

NelmioApiDocBundle

Build Status

The NelmioApiDocBundle bundle allows you to generate a decent documentation for your APIs.

Important: This bundle is developed in sync with symfony's repository. For Symfony 2.0.x, you need to use the 1.* version of the bundle.

Installation

Add this bundle to your composer.json file:

{
    "require": {
        "nelmio/api-doc-bundle": "dev-master"
    }
}

Register the bundle in app/AppKernel.php:

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
    );
}

Import the routing definition in routing.yml:

# app/config/routing.yml
NelmioApiDocBundle:
    resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
    prefix:   /api/doc

Enable the bundle's configuration in app/config/config.yml:

# app/config/config.yml
nelmio_api_doc: ~

Usage

The main problem with documentation is to keep it up to date. That's why the NelmioApiDocBundle uses introspection a lot. Thanks to an annotation, it's really easy to document an API method.

The ApiDoc() annotation

The bundle provides an ApiDoc() annotation for your controllers:

<?php

namespace Your\Namespace;

use Nelmio\ApiDocBundle\Annotation\ApiDoc;

class YourController extends Controller
{
    /**
     * This the documentation description of your method, it will appear
     * on a specific pane. It will read all the text until the first
     * annotation.
     *
     * @ApiDoc(
     *  resource=true,
     *  description="This is a description of your API method",
     *  filters={
     *      {"name"="a-filter", "dataType"="integer"},
     *      {"name"="another-filter", "dataType"="string", "pattern"="(foo|bar) ASC|DESC"}
     *  }
     * )
     */
    public function getAction()
    {
    }

    /**
     * @ApiDoc(
     *  description="Create a new Object",
     *  input="Your\Namespace\Form\Type\YourType"
     *  return='Your\Namespace\Class'
     * )
     */
    public function postAction()
    {
    }
}

The following properties are available:

  • resource: whether the method describes a main resource or not (default: false);

  • description: a description of the API method;

  • filters: an array of filters;

  • input: the input type associated to the method, currently this supports Form Types, and classes with JMS Serializer metadata, useful for POST|PUT methods, either as FQCN or as form type (if it is registered in the form factory in the container)

  • return: the return type associated with the response. Specified and parsed the same way as input.

Each filter has to define a name parameter, but other parameters are free. Filters are often optional parameters, and you can document them as you want, but keep in mind to be consistent for the whole documentation.

If you set input, then the bundle automatically extracts parameters based on the given type, and determines for each parameter its data type, and if it's required or not.

For Form Types, you can add an extra option named description on each field:

For classes parsed with JMS metadata, description will be taken from the properties doc comment, if available.

<?php

class YourType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('note', null, array(
            'description' => 'this is a note',
        ));

        // ...
    }
}

The bundle will also get information from the routing definition (requirements, pattern, etc), so to get the best out of it you should define strict _method requirements etc.

Documentation on-the-fly

By calling an URL with the parameter ?_doc=1, you will get the corresponding documentation if available.

Web Interface

You can browse the whole documentation at: http://example.org/api/doc.

Command

A command is provided in order to dump the documentation in json, markdown, or html.

php app/console api:doc:dump [--format="..."]

The --format option allows to choose the format (default is: markdown).

For example to generate a static version of your documentation you can use:

php app/console api:doc:dump --format=html > api.html

By default, the generated HTML will add the sandbox feature if you didn't disable it in the configuration. If you want to generate a static version of your documentation without sandbox, use the --no-sandbox option.

Configuration

You can specify your own API name:

# app/config/config.yml
nelmio_api_doc:
    name:   My API

This bundle provides a sandbox mode in order to test API methods. You can configure this sandbox using the following parameters:

# app/config/config.yml
nelmio_api_doc:
    sandbox:
        authentication: # default null, if set, the value of the api key is read from the query string and appended to every sandbox api call
            name: access_token
            delivery: query # only query delivery is supported for now
        enabled:  true # default: true, you can set this parameter to `false` to disable the sandbox
        endpoint: http://sandbox.example.com/ # default: /app_dev.php, use this parameter to define which URL to call through the sandbox

The bundle provides a way to register multiple input parsers. The first parser that can handle the specified input is used, so you can configure their priorities via container tags. Here's an example parser service registration:

#app/config/config.yml
services:
    mybundle.api_doc.extractor.custom_parser:
        class: MyBundle\Parser\CustomDocParser;
        tags:
            - {name: nelmio_api_doc.extractor.parser, priority: 2}

Credits

The design is heavily inspired by the swagger-ui project.