diff --git a/Annotation/Model.php b/Annotation/Model.php index d4aeff5..3130928 100644 --- a/Annotation/Model.php +++ b/Annotation/Model.php @@ -22,6 +22,7 @@ final class Model extends AbstractAnnotation public static $_types = [ 'type' => 'string', 'groups' => '[string]', + 'options' => '[mixed]', ]; public static $_required = ['type']; @@ -40,4 +41,9 @@ final class Model extends AbstractAnnotation * @var string[] */ public $groups; + + /** + * @var mixed[] + */ + public $options; } diff --git a/Model/Model.php b/Model/Model.php index d24ed01..a575bfe 100644 --- a/Model/Model.php +++ b/Model/Model.php @@ -19,13 +19,16 @@ final class Model private $groups; + private $options; + /** * @param string[]|null $groups */ - public function __construct(Type $type, array $groups = null) + public function __construct(Type $type, array $groups = null, array $options = null) { $this->type = $type; $this->groups = $groups; + $this->options = $options; } /** @@ -48,4 +51,12 @@ final class Model { return md5(serialize([$this->type, $this->groups])); } + + /** + * @return mixed[]|null + */ + public function getOptions() + { + return $this->options; + } } diff --git a/ModelDescriber/FormModelDescriber.php b/ModelDescriber/FormModelDescriber.php index 5c8dafc..0255df2 100644 --- a/ModelDescriber/FormModelDescriber.php +++ b/ModelDescriber/FormModelDescriber.php @@ -51,7 +51,7 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry $class = $model->getType()->getClassName(); - $form = $this->formFactory->create($class, null, []); + $form = $this->formFactory->create($class, null, $model->getOptions() ?? []); $this->parseForm($schema, $form); } @@ -101,7 +101,11 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry if (!$builtinFormType = $this->getBuiltinFormType($type)) { // if form type is not builtin in Form component. - $model = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, get_class($type->getInnerType()))); + $model = new Model( + new Type(Type::BUILTIN_TYPE_OBJECT, false, get_class($type->getInnerType())), + null, + $config->getOptions() + ); $property->setRef($this->modelRegistry->register($model)); return; diff --git a/SwaggerPhp/ModelRegister.php b/SwaggerPhp/ModelRegister.php index 5150a86..c34a59b 100644 --- a/SwaggerPhp/ModelRegister.php +++ b/SwaggerPhp/ModelRegister.php @@ -44,7 +44,7 @@ final class ModelRegister if ($annotation instanceof Schema && $annotation->ref instanceof ModelAnnotation) { $model = $annotation->ref; - $annotation->ref = $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model, $parentGroups))); + $annotation->ref = $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model, $parentGroups), $model->options)); // It is no longer an unmerged annotation $this->detach($model, $annotation, $analysis); @@ -90,7 +90,7 @@ final class ModelRegister } $annotation->merge([new $annotationClass([ - 'ref' => $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model, $parentGroups))), + 'ref' => $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model, $parentGroups), $model->options)), ])]); // It is no longer an unmerged annotation diff --git a/Tests/Functional/Controller/ApiController.php b/Tests/Functional/Controller/ApiController.php index c359c0f..d54c808 100644 --- a/Tests/Functional/Controller/ApiController.php +++ b/Tests/Functional/Controller/ApiController.php @@ -91,7 +91,7 @@ class ApiController * name="foo", * in="body", * description="This is a parameter", - * @SWG\Schema(ref=@Model(type=UserType::class)) + * @SWG\Schema(ref=@Model(type=UserType::class, options={"bar": "baz"})) * ) */ public function submitUserTypeAction() diff --git a/Tests/Functional/Form/UserType.php b/Tests/Functional/Form/UserType.php index a60bf56..a05591c 100644 --- a/Tests/Functional/Form/UserType.php +++ b/Tests/Functional/Form/UserType.php @@ -50,5 +50,7 @@ class UserType extends AbstractType $resolver->setDefaults([ 'data_class' => User::class, ]); + + $resolver->setRequired('bar'); } }