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:
Guilhem N 2018-01-11 12:26:59 +01:00 committed by GitHub
parent 667569be7d
commit 73a5f104d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 30 deletions

View File

@ -65,17 +65,28 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn
$schema->setType('object');
$properties = $schema->getProperties();
foreach ($metadata->propertyMetadata as $item) {
if (null === $item->type) {
continue;
}
// filter groups
if (null !== $groupsExclusion && $groupsExclusion->shouldSkipProperty($item, SerializationContext::create())) {
continue;
}
$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)) {
$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))
);
}
// 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);
}
}
}

View File

@ -51,6 +51,21 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
}
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);
if (0 === count($types)) {
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];
$realProp = $property = $properties->get($propertyName);
if (Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) {
$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 = $property->getItems();
}
@ -89,14 +106,6 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
} else {
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
);
}
}
}

View File

@ -61,6 +61,12 @@ class JMSUser
*/
private $password;
/**
* @SWG\Property(type="date")
* @Serializer\Expose
*/
private $updatedAt;
/**
* Ignored as the JMS serializer can't detect its type.
*

View File

@ -26,9 +26,7 @@ class User
private $id;
/**
* @var string
*
* @SWG\Property(readOnly = false)
* @SWG\Property(type="string", readOnly = false)
*/
private $email;
@ -91,10 +89,7 @@ class User
$this->id = $id;
}
/**
* @param string $email
*/
public function setEmail(string $email)
public function setEmail($email)
{
$this->email = $email;
}

View File

@ -59,6 +59,9 @@ class JMSFunctionalTest extends WebTestCase
'description' => 'Only enabled users may be used in actions.',
'enum' => ['disabled', 'enabled'],
],
'updated_at' => [
'type' => 'date',
],
],
], $this->getModel('JMSUser')->toArray());
}