mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-10 11:39:25 +03:00
Use @SWG\Parameter type when set instead of throwing (#1174)
* Add an exception * Just use the manually defined type when set * Update the jms model describer
This commit is contained in:
parent
667569be7d
commit
73a5f104d6
@ -65,17 +65,28 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
|
|||||||
$schema->setType('object');
|
$schema->setType('object');
|
||||||
$properties = $schema->getProperties();
|
$properties = $schema->getProperties();
|
||||||
foreach ($metadata->propertyMetadata as $item) {
|
foreach ($metadata->propertyMetadata as $item) {
|
||||||
if (null === $item->type) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// filter groups
|
// filter groups
|
||||||
if (null !== $groupsExclusion && $groupsExclusion->shouldSkipProperty($item, SerializationContext::create())) {
|
if (null !== $groupsExclusion && $groupsExclusion->shouldSkipProperty($item, SerializationContext::create())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$name = $this->namingStrategy->translateName($item);
|
$name = $this->namingStrategy->translateName($item);
|
||||||
$realProp = $property = $properties->get($name);
|
$property = $properties->get($name);
|
||||||
|
|
||||||
|
// read property options from Swagger Property annotation if it exists
|
||||||
|
if (null !== $item->reflection) {
|
||||||
|
if ($this->phpdocPropertyAnnotationsReader) {
|
||||||
|
$this->phpdocPropertyAnnotationsReader->updateWithPhpdoc($item->reflection, $property);
|
||||||
|
}
|
||||||
|
$this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation($item->reflection, $property);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $property->getType()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (null === $item->type) {
|
||||||
|
$properties->remove($name);
|
||||||
|
}
|
||||||
|
|
||||||
if ($type = $this->getNestedTypeInArray($item)) {
|
if ($type = $this->getNestedTypeInArray($item)) {
|
||||||
$property->setType('array');
|
$property->setType('array');
|
||||||
@ -110,14 +121,6 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
|
|||||||
$this->modelRegistry->register(new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type), $groups))
|
$this->modelRegistry->register(new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type), $groups))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// read property options from Swagger Property annotation if it exists
|
|
||||||
if (null !== $item->reflection) {
|
|
||||||
if ($this->phpdocPropertyAnnotationsReader) {
|
|
||||||
$this->phpdocPropertyAnnotationsReader->updateWithPhpdoc($item->reflection, $realProp);
|
|
||||||
}
|
|
||||||
$this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation($item->reflection, $realProp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,6 +51,21 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($propertyInfoProperties as $propertyName) {
|
foreach ($propertyInfoProperties as $propertyName) {
|
||||||
|
$property = $properties->get($propertyName);
|
||||||
|
|
||||||
|
// read property options from Swagger Property annotation if it exists
|
||||||
|
if (property_exists($class, $propertyName)) {
|
||||||
|
$this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation(
|
||||||
|
new \ReflectionProperty($class, $propertyName),
|
||||||
|
$property
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If type manually defined
|
||||||
|
if (null !== $property->getType()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$types = $this->propertyInfo->getTypes($class, $propertyName);
|
$types = $this->propertyInfo->getTypes($class, $propertyName);
|
||||||
if (0 === count($types)) {
|
if (0 === count($types)) {
|
||||||
throw new \LogicException(sprintf('The PropertyInfo component was not able to guess the type of %s::$%s', $class, $propertyName));
|
throw new \LogicException(sprintf('The PropertyInfo component was not able to guess the type of %s::$%s', $class, $propertyName));
|
||||||
@ -60,10 +75,12 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
|
|||||||
}
|
}
|
||||||
|
|
||||||
$type = $types[0];
|
$type = $types[0];
|
||||||
$realProp = $property = $properties->get($propertyName);
|
|
||||||
|
|
||||||
if (Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) {
|
if (Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) {
|
||||||
$type = $type->getCollectionValueType();
|
$type = $type->getCollectionValueType();
|
||||||
|
if (null === $type) {
|
||||||
|
throw new \LogicException(sprintf('Property "%s:%s" is an array, but no indication of the array elements are made. Use e.g. string[] for an array of string.', $class, $propertyName));
|
||||||
|
}
|
||||||
|
|
||||||
$property->setType('array');
|
$property->setType('array');
|
||||||
$property = $property->getItems();
|
$property = $property->getItems();
|
||||||
}
|
}
|
||||||
@ -89,14 +106,6 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
|
|||||||
} else {
|
} else {
|
||||||
throw new \Exception(sprintf('Unknown type: %s', $type->getBuiltinType()));
|
throw new \Exception(sprintf('Unknown type: %s', $type->getBuiltinType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// read property options from Swagger Property annotation if it exists
|
|
||||||
if (property_exists($class, $propertyName)) {
|
|
||||||
$this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation(
|
|
||||||
new \ReflectionProperty($class, $propertyName),
|
|
||||||
$realProp
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,6 +61,12 @@ class JMSUser
|
|||||||
*/
|
*/
|
||||||
private $password;
|
private $password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @SWG\Property(type="date")
|
||||||
|
* @Serializer\Expose
|
||||||
|
*/
|
||||||
|
private $updatedAt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ignored as the JMS serializer can't detect its type.
|
* Ignored as the JMS serializer can't detect its type.
|
||||||
*
|
*
|
||||||
|
@ -26,9 +26,7 @@ class User
|
|||||||
private $id;
|
private $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @SWG\Property(type="string", readOnly = false)
|
||||||
*
|
|
||||||
* @SWG\Property(readOnly = false)
|
|
||||||
*/
|
*/
|
||||||
private $email;
|
private $email;
|
||||||
|
|
||||||
@ -91,10 +89,7 @@ class User
|
|||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function setEmail($email)
|
||||||
* @param string $email
|
|
||||||
*/
|
|
||||||
public function setEmail(string $email)
|
|
||||||
{
|
{
|
||||||
$this->email = $email;
|
$this->email = $email;
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,9 @@ class JMSFunctionalTest extends WebTestCase
|
|||||||
'description' => 'Only enabled users may be used in actions.',
|
'description' => 'Only enabled users may be used in actions.',
|
||||||
'enum' => ['disabled', 'enabled'],
|
'enum' => ['disabled', 'enabled'],
|
||||||
],
|
],
|
||||||
|
'updated_at' => [
|
||||||
|
'type' => 'date',
|
||||||
|
],
|
||||||
],
|
],
|
||||||
], $this->getModel('JMSUser')->toArray());
|
], $this->getModel('JMSUser')->toArray());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user