Skip to content

Commit

Permalink
Merge pull request #868 from dpcat237/502_hide-requirement-when-empty
Browse files Browse the repository at this point in the history
#502 hide requirement when empty
  • Loading branch information
willdurand committed Jun 13, 2016
2 parents a009e97 + 1256275 commit e6ea851
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Extractor/Handler/FosRestHandler.php
Expand Up @@ -56,11 +56,15 @@ public function handle(ApiDoc $annotation, array $annotations, Route $route, \Re
'description' => $annot->description,
'default' => $annot->default,
));
} else {
} elseif ($annot->requirements !== null) {
$annotation->addFilter($annot->name, array(
'requirement' => $this->handleRequirements($annot->requirements).((property_exists($annot, 'map') ? $annot->map : $annot->array) ? '[]' : ''),
'description' => $annot->description,
));
} else {
$annotation->addFilter($annot->name, array(
'description' => $annot->description,
));
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/Extractor/Handler/FosRestHandlerTest.php
Expand Up @@ -203,4 +203,22 @@ public function testWithRequestParamPlainArrayRequirements()
$this->assertArrayHasKey('requirement', $filters['param1']);
$this->assertEquals('NotNull, NotBlank', $filters['param1']['requirement']);
}

public function testWithRequirementParamNotSet()
{
$container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zActionWithRequirementParamNotSet', 'test_route_31');

$this->assertNotNull($annotation);

$filters = $annotation->getFilters();
$this->assertCount(1, $filters);
$this->assertArrayHasKey('param1', $filters);
$filter = $filters['param1'];

$this->assertArrayNotHasKey('requirement', $filter);
$this->assertArrayHasKey('description', $filter);
$this->assertEquals($filter['description'], 'Param1 description.');
}
}
8 changes: 8 additions & 0 deletions Tests/Fixtures/Controller/TestController.php
Expand Up @@ -414,4 +414,12 @@ public function routeWithQueryParamArrayRequirementsAction()
public function routeWithQueryParamPlainArrayRequirementsAction()
{
}

/**
* @ApiDoc()
* @QueryParam(name="param1", description="Param1 description.")
*/
public function zActionWithRequirementParamNotSet()
{
}
}
5 changes: 5 additions & 0 deletions Tests/Fixtures/app/config/routing.yml
Expand Up @@ -248,3 +248,8 @@ test_route_30:
path: /z-query-param-plain-array-requirements
methods: [GET]
defaults: { _controller: NelmioApiDocTestBundle:Test:routeWithQueryParamPlainArrayRequirementsAction }

test_route_31:
path: /z-query-requirement-param-not-set
methods: [GET]
defaults: { _controller: NelmioApiDocTestBundle:Test:zActionWithRequirementParamNotSet }

0 comments on commit e6ea851

Please sign in to comment.