Skip to content

Commit

Permalink
Fix exposure of private/protected methods
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhemN committed Dec 16, 2020
1 parent 76c6f05 commit 3895e17
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ModelDescriber/ObjectModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ public function describe(Model $model, Schema $schema)
$annotationsReader->updateDefinition($reflClass, $schema);

$propertyInfoProperties = $this->propertyInfo->getProperties($class, $context);

if (null === $propertyInfoProperties) {
return;
}

// Fix for https://github.com/nelmio/NelmioApiDocBundle/issues/1756
// The SerializerExtractor does expose private/protected properties for some reason, so we eliminate them here
$propertyInfoProperties = array_intersect($propertyInfoProperties, $this->propertyInfo->getProperties($class, []) ?? []);

foreach ($propertyInfoProperties as $propertyName) {
$serializedName = null !== $this->nameConverter ? $this->nameConverter->normalize($propertyName, $class, null, null !== $model->getGroups() ? ['groups' => $model->getGroups()] : []) : $propertyName;

Expand Down
30 changes: 30 additions & 0 deletions Tests/Functional/Entity/PrivateProtectedExposure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;

/**
* @author Guilhem N. <[email protected]>
*/
class PrivateProtectedExposure
{
private $privateField;
protected $protectedField;

/**
* @var string
*/
public $publicField;

protected function setProtected(string $thing)
{
}
}
15 changes: 15 additions & 0 deletions Tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,19 @@ public function testInvokableController()
$operation = $this->getOperation('/api/invoke', 'get');
$this->assertSame('Invokable!', $operation->getResponses()->get(200)->getDescription());
}

/**
* Related to https://github.com/nelmio/NelmioApiDocBundle/issues/1756
* Ensures private/protected properties are not exposed, just like the symfony serializer does.
*/
public function testPrivateProtectedExposure()
{
// Ensure that groups are supported
$modelProperties = $this->getModel('PrivateProtectedExposure')->getProperties();
$this->assertCount(1, $modelProperties);
$this->assertTrue($modelProperties->has('publicField'));
$this->assertFalse($modelProperties->has('privateField'));
$this->assertFalse($modelProperties->has('protectedField'));
$this->assertFalse($modelProperties->has('protected'));
}
}
1 change: 1 addition & 0 deletions Tests/Functional/SwaggerUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function testApiPlatformSwaggerUi()
'Test' => ['type' => 'string'],
'JMSPicture_mini' => ['type' => 'object'],
'BazingaUser_grouped' => ['type' => 'object'],
'PrivateProtectedExposure' => $expected['definitions']['PrivateProtectedExposure'],
];

$this->assertEquals($expected, json_decode($crawler->filterXPath('//script[@id="swagger-data"]')->text(), true)['spec']);
Expand Down
5 changes: 5 additions & 0 deletions Tests/Functional/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Nelmio\ApiDocBundle\NelmioApiDocBundle;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\BazingaUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSPicture;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\PrivateProtectedExposure;
use Nelmio\ApiDocBundle\Tests\Functional\ModelDescriber\VirtualTypeClassDoesNotExistsHandlerDefinedDescriber;
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
Expand Down Expand Up @@ -213,6 +214,10 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
],
'models' => [
'names' => [
[
'alias' => 'PrivateProtectedExposure',
'type' => PrivateProtectedExposure::class,
],
[
'alias' => 'JMSPicture_mini',
'type' => JMSPicture::class,
Expand Down

0 comments on commit 3895e17

Please sign in to comment.