Core: Custom action + ApiDoc

Created on 13 Jan 2016  路  4Comments  路  Source: api-platform/core

How is proper way to add custom action with custom api documentation?

Now I have custom POST action for confirm user account in users collection "users/confirmation", everything is working ok, but documentation is bad. I want to change "Parameters" and "Return" properties. How to do that? If I add @ApiDoc annotation for custom controller I have double action "users/confirmation", one with my annotation and one with default annotations from user resource.

Maybe something is wrong in DunglasApiProvider, I think there must be some statement for existing ApiDoc annotation in custom controller.

Most helpful comment

@dunglas how?

All 4 comments

I suggest something like that in DunglasApiProvider in NelmioApiDoc:

if (!$this->operationApiDocExists($operation, $annotationReader)) {
    $annotations[] = $this->getApiDoc(true, $resource, $operation, $resourceHydraDoc, $entrypointHydraDoc);
}   

And method definition:

/**
 * Check if ApiDoc annotation for operation exists
 * @param $operation
 * @param $annotationReader
 * @return bool
 */
private function operationApiDocExists($operation, $annotationReader)
{
    $defaults = $operation->getRoute()->getDefaults();

    list($controllerClass, $controllerMethod) = explode('::', $defaults['_controller']);

    $reflectedMethod = new \ReflectionMethod($controllerClass, $controllerMethod);
    $methodAnnotations = $annotationReader->getMethodAnnotations($reflectedMethod);

    foreach ($methodAnnotations as $annotation) {
        if ($annotation instanceof ApiDoc) {
            return true;
        }
    }

    return false;
}

Custom actions can be now documented using Swagger UI.

@dunglas how?

@dunglas could you provide an example for custom documentation with Action/Controller ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

WybrenKoelmans picture WybrenKoelmans  路  32Comments

teohhanhui picture teohhanhui  路  34Comments

Simperfit picture Simperfit  路  24Comments

dunglas picture dunglas  路  79Comments

jsamouh picture jsamouh  路  28Comments