mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
Respect property name for constraints
This commit is contained in:
parent
99833189fd
commit
995ade1c8d
@ -49,8 +49,13 @@ class SymfonyConstraintAnnotationReader
|
||||
continue;
|
||||
}
|
||||
|
||||
$propertyName = $this->getSchemaPropertyName($property);
|
||||
if (null === $propertyName) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$existingRequiredFields = $this->schema->getRequired() ?? [];
|
||||
$existingRequiredFields[] = $reflectionProperty->getName();
|
||||
$existingRequiredFields[] = $propertyName;
|
||||
|
||||
$this->schema->setRequired(array_values(array_unique($existingRequiredFields)));
|
||||
} elseif ($annotation instanceof Assert\Length) {
|
||||
@ -76,6 +81,24 @@ class SymfonyConstraintAnnotationReader
|
||||
$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.
|
||||
*/
|
||||
|
@ -14,6 +14,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Controller;
|
||||
use Nelmio\ApiDocBundle\Annotation\Model;
|
||||
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex;
|
||||
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\NestedGroup\JMSChat;
|
||||
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"})
|
||||
* @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());
|
||||
}
|
||||
|
||||
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 = [])
|
||||
{
|
||||
return new TestKernel(true);
|
||||
|
Loading…
x
Reference in New Issue
Block a user