mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
Merge pull request #1396 from scaytrase/bugfix/respect-property-name-for-constraints
[Bug] Respect property name for constraints
This commit is contained in:
commit
c805beb3f5
@ -49,8 +49,13 @@ class SymfonyConstraintAnnotationReader
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$propertyName = $this->getSchemaPropertyName($property);
|
||||||
|
if (null === $propertyName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$existingRequiredFields = $this->schema->getRequired() ?? [];
|
$existingRequiredFields = $this->schema->getRequired() ?? [];
|
||||||
$existingRequiredFields[] = $reflectionProperty->getName();
|
$existingRequiredFields[] = $propertyName;
|
||||||
|
|
||||||
$this->schema->setRequired(array_values(array_unique($existingRequiredFields)));
|
$this->schema->setRequired(array_values(array_unique($existingRequiredFields)));
|
||||||
} elseif ($annotation instanceof Assert\Length) {
|
} elseif ($annotation instanceof Assert\Length) {
|
||||||
@ -76,6 +81,24 @@ class SymfonyConstraintAnnotationReader
|
|||||||
$this->schema = $schema;
|
$this->schema = $schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get assigned property name for property schema.
|
||||||
|
*/
|
||||||
|
private function getSchemaPropertyName(Schema $property)
|
||||||
|
{
|
||||||
|
if (null === $this->schema) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->schema->getProperties() as $name => $schemaProperty) {
|
||||||
|
if ($schemaProperty === $property) {
|
||||||
|
return $name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append the pattern from the constraint to the existing pattern.
|
* Append the pattern from the constraint to the existing pattern.
|
||||||
*/
|
*/
|
||||||
|
@ -14,6 +14,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Controller;
|
|||||||
use Nelmio\ApiDocBundle\Annotation\Model;
|
use Nelmio\ApiDocBundle\Annotation\Model;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex;
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSDualComplex;
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSDualComplex;
|
||||||
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSNamingStrategyConstraints;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser;
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChat;
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChat;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatUser;
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatUser;
|
||||||
@ -75,6 +76,18 @@ class JMSController
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route("/api/jms_naming_strategy", methods={"GET"})
|
||||||
|
* @SWG\Response(
|
||||||
|
* response=200,
|
||||||
|
* description="Success",
|
||||||
|
* @Model(type=JMSNamingStrategyConstraints::class, groups={"Default"})
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
public function namingStrategyConstraintsAction()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Route("/api/jms_chat", methods={"GET"})
|
* @Route("/api/jms_chat", methods={"GET"})
|
||||||
* @SWG\Response(
|
* @SWG\Response(
|
||||||
|
31
Tests/Functional/Entity/JMSNamingStrategyConstraints.php
Normal file
31
Tests/Functional/Entity/JMSNamingStrategyConstraints.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
|
||||||
|
|
||||||
|
use JMS\Serializer\Annotation as Serializer;
|
||||||
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
|
class JMSNamingStrategyConstraints
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*
|
||||||
|
* @Serializer\Type("string")
|
||||||
|
* @Serializer\SerializedName("beautifulName")
|
||||||
|
*
|
||||||
|
* @Assert\NotBlank()
|
||||||
|
* @Assert\Regex(pattern="\w+")
|
||||||
|
* @Assert\Length(min="3", max="10")
|
||||||
|
*/
|
||||||
|
private $some_weird_named_property = 'default';
|
||||||
|
|
||||||
|
public function getSomeWeirdNamedProperty(): string
|
||||||
|
{
|
||||||
|
return $this->some_weird_named_property;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSomeWeirdNamedProperty(string $some_weird_named_property)
|
||||||
|
{
|
||||||
|
$this->some_weird_named_property = $some_weird_named_property;
|
||||||
|
}
|
||||||
|
}
|
@ -237,6 +237,21 @@ class JMSFunctionalTest extends WebTestCase
|
|||||||
], $this->getModel('VirtualProperty')->toArray());
|
], $this->getModel('VirtualProperty')->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNamingStrategyWithConstraints()
|
||||||
|
{
|
||||||
|
$this->assertEquals([
|
||||||
|
'type' => 'object',
|
||||||
|
'properties' => [
|
||||||
|
'beautifulName' => [
|
||||||
|
'type' => 'string',
|
||||||
|
'maxLength' => '10',
|
||||||
|
'minLength' => '3',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'required' => ['beautifulName'],
|
||||||
|
], $this->getModel('JMSNamingStrategyConstraints')->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
protected static function createKernel(array $options = [])
|
protected static function createKernel(array $options = [])
|
||||||
{
|
{
|
||||||
return new TestKernel(true);
|
return new TestKernel(true);
|
||||||
|
@ -32,15 +32,16 @@ class SymfonyConstraintAnnotationReaderTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
private $property2;
|
private $property2;
|
||||||
};
|
};
|
||||||
$reflectionProperties = (new \ReflectionClass($entity))->getProperties();
|
|
||||||
$property = new Schema();
|
|
||||||
$schema = new Schema();
|
$schema = new Schema();
|
||||||
|
$schema->getProperties()->set('property1', new Schema());
|
||||||
|
$schema->getProperties()->set('property2', new Schema());
|
||||||
|
|
||||||
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
||||||
$symfonyConstraintAnnotationReader->setSchema($schema);
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
||||||
foreach ($reflectionProperties as $reflectionProperty) {
|
|
||||||
$symfonyConstraintAnnotationReader->updateProperty($reflectionProperty, $property);
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->getProperties()->get('property1'));
|
||||||
}
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property2'), $schema->getProperties()->get('property2'));
|
||||||
|
|
||||||
// expect required to be numeric array with sequential keys (not [0 => ..., 2 => ...])
|
// expect required to be numeric array with sequential keys (not [0 => ..., 2 => ...])
|
||||||
$this->assertEquals($schema->getRequired(), ['property1', 'property2']);
|
$this->assertEquals($schema->getRequired(), ['property1', 'property2']);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user