1

I hope someone could help me to use Api-platorm with Nelmio.

I use Api-plaform and Nelmio. I need to hide the Api-platform docs from Nelmio.

I need to have 3 routes:

/internal -> API-Platform Docs
/external -> NELMIO-Docs
/admin -> NELMIO-Docs

My config of Nelmio:

# config/packages/nelmio_api_doc.yaml
nelmio_api_doc:
    documentation:
        info:
            title: ...
            description: ...
            version: 0.2.0
    areas: # to filter documented areas
        default:
            path_patterns: [ ^/external ]
        external:
            path_patterns: [ ^/external ]
        admin:
            path_patterns: [ ^/admin ]

My config of Nelmio (routes):

# config/routes/nelmio_api_doc.yaml
app.swagger:
    path: /{area}/json
    methods: GET
    defaults: { _controller: nelmio_api_doc.controller.swagger, area: default }

app.swagger_ui:
    path: /{area}
    methods: GET
    defaults: { _controller: nelmio_api_doc.controller.swagger_ui, area: default }

My config of API-Platform:

# config/routes/api_platform.yaml
api_platform:
    resource: .
    type: api_platform
    prefix: /internal/

But if I go to http://localhost/external or http://localhost/admin I see always not only needed routes, but also the routes from API-Platform:

enter image description here

krut1
  • 21
  • 4

2 Answers2

1

I know this question is old by now, but I am facing the same situation and I found a workaround that may help some one, so I am posting it.

API Platform lets you decorate Swagger so you can customize the final documentation output. I took advantage of this to get rid of the entire api platform documentation when not asking for it.

<?php

namespace App\Swagger;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class SwaggerDecorator implements NormalizerInterface
{
    private $decorated;
    private $requestStack;

    public function __construct(NormalizerInterface $decorated, RequestStack $requestStack)
    {
        $this->decorated = $decorated;
        $this->requestStack = $requestStack;
    }

    public function normalize($object, $format = null, array $context = [])
    {
        if ('/internal/docs' !== $this->requestStack->getCurrentRequest()->getPathInfo()) {
            // request is not for internal docs (maybe it is for external or admin one) so get rid of api platform docs
            return null;
        }

        $docs = $this->decorated->normalize($object, $format, $context);

        // here you can customize documentation

        return $docs;
    }

    public function supportsNormalization($data, $format = null)
    {
        return $this->decorated->supportsNormalization($data, $format);
    }
}

I hope this helps someone, happy coding!


UPDATE

In order to enable that decorator, you must declare it as so in your services file:

App\Swagger\SwaggerDecorator:
        decorates: 'api_platform.swagger.normalizer.api_gateway'
        arguments: [ '@App\Swagger\SwaggerDecorator.inner' ]
        autoconfigure: false

Then, in the class itself, replace '/internal/docs' with the actual URL you are using for your API Platform documentation.

Hope this helps.

0

On your nelmio configuration yaml file, use a regex to exclude the docs. For instance, for excluding the /external/doc you should:

 external:
            path_patterns: [ ^/external(?!/doc$) ]
javier_domenech
  • 5,995
  • 6
  • 37
  • 59
  • This works when you want to exclude something that is in "second position" in the path. Your example will remove only the /external/doc, but if you want to remove the whole /external it doesn't seem to work. – No_or_yes Jun 01 '21 at 14:01
  • path_patterns: [ ^/external ] – javier_domenech Jun 01 '21 at 17:50
  • this should only include everything that starts with `/external`, unfortunately it doesn't work for me. – No_or_yes Jun 03 '21 at 10:23
  • if you want only include everything that starts with /external you dont need path_patherns, just use prefix: /external/ – javier_domenech Jun 03 '21 at 12:16
  • Apparently there is a bug or something as this doesn't work. I use the API platform for part of our endpoints, which are all all prefixed with `/v1` and I'm unable to exclude them from the Nelmio documentation. So far the only solution I've found is to disable documentation on API platform completely. – No_or_yes Jun 07 '21 at 07:58