diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index a418779..d7d13e7 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -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); - } } } diff --git a/ModelDescriber/ObjectModelDescriber.php b/ModelDescriber/ObjectModelDescriber.php index bb6fad4..ee58483 100644 --- a/ModelDescriber/ObjectModelDescriber.php +++ b/ModelDescriber/ObjectModelDescriber.php @@ -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 - ); - } } } diff --git a/Tests/Functional/Entity/JMSUser.php b/Tests/Functional/Entity/JMSUser.php index db0e760..07eb4fd 100644 --- a/Tests/Functional/Entity/JMSUser.php +++ b/Tests/Functional/Entity/JMSUser.php @@ -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. * diff --git a/Tests/Functional/Entity/User.php b/Tests/Functional/Entity/User.php index 68cc737..cf14a3e 100644 --- a/Tests/Functional/Entity/User.php +++ b/Tests/Functional/Entity/User.php @@ -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; } diff --git a/Tests/Functional/JMSFunctionalTest.php b/Tests/Functional/JMSFunctionalTest.php index 4229833..e77a78f 100644 --- a/Tests/Functional/JMSFunctionalTest.php +++ b/Tests/Functional/JMSFunctionalTest.php @@ -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()); }