Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc broken with Validator Constraints in FOSRestBundle requirements #357

Closed
pyrech opened this issue Mar 26, 2014 · 4 comments · Fixed by #365
Closed

Doc broken with Validator Constraints in FOSRestBundle requirements #357

pyrech opened this issue Mar 26, 2014 · 4 comments · Fixed by #365

Comments

@pyrech
Copy link
Contributor

pyrech commented Mar 26, 2014

I just used a new feature of the FOSRestBundle (FriendsOfSymfony/FOSRestBundle#700). This PR allow to use Constraints from the sf validator as (Query|Request)Param's requirement, not just a string containing the regex. We can now do:

use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\Validator\Constraints as Assert;
...
@Rest\QueryParam(name="foo", requirements=@Assert\Regex("#bar#u"), description="toto")

Howewer, there is an issue in the rendering of the doc. A Twig_Error_Runtime exception is thrown in "NelmioApiDocBundle::method.html.twig" at line 71:

{{ infos.requirement is defined ? infos.requirement : ''}}

I suppose this is due to the fact that requirement is not printable as this is a Constraint. Is this confirmed?

@pyrech
Copy link
Contributor Author

pyrech commented Apr 4, 2014

I tried to a add a method handleRequirements in Nelmio\ApiDocBundle\Extractor\Handle\FosRestHandler which displayed the pattern if it's a Regex constraint, or the name of the class if it's a Constraint, or the requirements if not. It's not really nice but I don't see another simple way to display useful information.

private function handleRequirements($requirements)
{
    if (is_object($requirements) && $requirements instanceof Constraint) {
        if ($requirements instanceof Regex) {
            return $requirements->getHtmlPattern();
        }
        $class = get_class($requirements);
        return substr($class, strrpos($class, '\\')+1);
    }
    return $requirements;
}

Example of use in the handle method:

$annotation->addRequirement($annot->name, array(
    'requirement'   => $this->handleRequirements($annot->requirements),
    'dataType'      => '',
    'description'   => $annot->description,
));

@pyrech pyrech changed the title Doc broken with Symfony Constraints as FOSRestBundle requirements Doc broken with Validator Constraints in FOSRestBundle requirements Apr 4, 2014
@willdurand
Copy link
Collaborator

Your solution is interesting. Would you mind create a PR?

@pyrech
Copy link
Contributor Author

pyrech commented Apr 17, 2014

Of course. I'm doing it right now! :)

@pyrech
Copy link
Contributor Author

pyrech commented Apr 18, 2014

Oups, I thought the PR would be referenced, not the commit. So here is the PR #365

willdurand added a commit that referenced this issue May 16, 2014
Fix #357 - doc broken with Validator Constraints in FOSRestBundle requirements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants