Generates documentation for your REST API from annotations
Find a file
David Buchmann ccf7a75ec5
Merge pull request #1159 from nelmio/analysis-zG01nA
Apply fixes from StyleCI
2017-12-22 18:44:38 +01:00
Annotation Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
Controller Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
DependencyInjection Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
Describer Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
Model Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
ModelDescriber Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
Resources set up DI and add type support 2017-12-19 08:56:35 +01:00
RouteDescriber Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
Routing Change the behavior of path_patterns filter to use OR instead of AND 2017-06-07 13:55:32 +02:00
SwaggerPhp Fix the invalid reference notice 2017-08-27 17:41:42 +02:00
Tests Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
Util Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
.gitignore Fix the tests 2017-03-15 13:38:23 +01:00
.php_cs.dist Allow implicit operations with SwaggerPhp annotations 2017-01-08 12:12:43 +01:00
.styleci.yml Fix CS 2016-07-30 20:04:03 +02:00
.travis.yml Update Travis config 2017-12-17 10:46:20 +01:00
ApiDocGenerator.php Apply fixes from StyleCI 2017-12-22 17:42:18 +00:00
composer.json Fix CS 2017-12-17 10:44:07 +01:00
CONTRIBUTING.md Document the bundle 2017-05-13 17:21:28 +02:00
LICENSE Change the vendor to "Nelmio" 2016-12-29 12:09:26 +01:00
NelmioApiDocBundle.php Enable the FormModelDescriber only if forms are enabled (#1154) 2017-12-19 20:35:10 +01:00
phpunit Use phpunit 6 2017-05-31 19:36:17 +02:00
phpunit.xml.dist Update travis config (#1117) 2017-11-27 14:40:22 +01:00
README.md adjust to feedback 2017-12-19 23:14:57 +01:00
update-js.sh Include map files 2017-06-22 21:22:10 +02:00
UPGRADE-3.0.md make upgrade instructions depend on stable version 2017-12-18 08:41:18 +01:00

NelmioApiDocBundle

Build
Status Total Downloads Latest Stable
Version

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

Migrate from 2.x to 3.0

To migrate from 2.x to 3.0, just follow our guide.

Installation

First, open a command console, enter your project directory and execute the following command to download the latest version of this bundle:

composer require nelmio/api-doc-bundle dev-master

Then add the bundle to your kernel:

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...

            new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
        ];

        // ...
    }
}

To browse your documentation with Swagger UI, register the following route:

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

If you also want to expose it in JSON, register this route:

# app/config/routing.yml
app.swagger:
    path: /api/doc.json
    methods: GET
    defaults: { _controller: nelmio_api_doc.controller.swagger }

What does this bundle?

It generates you a swagger documentation from your symfony app thanks to Describers. Each of these Describers extract infos from various sources. For instance, one extract data from SwaggerPHP annotations, one from your routes, etc.

If you configured the app.swagger_ui route above, you can browse your documentation at http://example.org/api/doc.

Configure the bundle

As you just installed the bundle, you'll likely see routes you don't want in your documentation such as /_profiler/. To fix this, you can filter the routes that are documented by configuring the bundle:

# app/config/config.yml
nelmio_api_doc:
    routes:
        path_patterns: # an array of regexps
            - ^/api

Use the bundle

You can configure globally your documentation in the config (take a look at the Swagger specification to know the fields available):

nelmio_api_doc:
    documentation:
        info:
            title: My App
            description: This is an awesome app!
            version: 1.0.0

To document your routes, you can use annotations in your controllers:

namespace AppBundle\Controller;

use AppBundle\Entity\User;
use AppBundle\Entity\Reward;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use Symfony\Component\Routing\Annotation\Route;

class UserController
{
    /*
     * @Route("/api/{user}/rewards", methods={"GET"})
     * @SWG\Response(
     *     response=200,
     *     description="Returns the rewards of an user",
     *     @SWG\Schema(
     *         type="array",
     *         @Model(type=Reward::class, groups={"full"})
     *     )
     * )
     * @SWG\Parameter(
     *     name="order",
     *     in="query",
     *     type="string",
     *     description="The field used to order rewards"
     * )
     * @SWG\Tag(name="rewards")
     */
    public function fetchUserRewardsAction(User $user)
    {
        // ...
    }
}

Use models

As shown in the example above, the bundle provides the @Model annotation. When you use it, the bundle will deduce your model properties.

If you're not using the JMS Serializer

The Symfony PropertyInfo component is used to describe your models. It supports doctrine annotations, type hints, and even PHP doc blocks as long as you required the phpdocumentor/reflection-docblock library. It does also support serialization groups when using the Symfony serializer.

If you're using the JMS Serializer

The metadata of the JMS serializer are used by default to describe your models. Additional information is extracted from the PHP doc block comment, but the property types must be specified in the JMS annotations.

In case you prefer using the Symfony PropertyInfo component (you won't be able to use JMS serialization groups), you can disable JMS serializer support in your config:

nelmio_api_doc:
    models: { use_jms: false }

What's supported?

This bundle supports Symfony route requirements, PHP annotations, Swagger-Php annotations, FOSRestBundle annotations and apps using Api-Platform.

For models, it supports the Symfony serializer and the JMS serializer.

Contributing

See CONTRIBUTING file.

Running the Tests

Install the Composer dependencies:

git clone https://github.com/nelmio/NelmioApiDocBundle.git
cd NelmioApiDocBundle
composer update

Then run the test suite:

./phpunit

License

This bundle is released under the MIT license.