From de11fd1b59d2b1074c45ebbfd9c82ca3f9d6048c Mon Sep 17 00:00:00 2001 From: Andreas Heigl Date: Wed, 6 Nov 2024 11:56:49 +0100 Subject: [PATCH] Remove fields with default from required entry --- src/ModelDescriber/ObjectModelDescriber.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ModelDescriber/ObjectModelDescriber.php b/src/ModelDescriber/ObjectModelDescriber.php index 7b767cbf1..678158c49 100644 --- a/src/ModelDescriber/ObjectModelDescriber.php +++ b/src/ModelDescriber/ObjectModelDescriber.php @@ -172,6 +172,18 @@ public function describe(Model $model, OA\Schema $schema) $property->default = $defaultValues[$propertyName]; } + if (Generator::UNDEFINED !== $property->default) { + // Fix for https://github.com/nelmio/NelmioApiDocBundle/issues/2222 + // When a default value has been set for the property, we can + // remove the field from the required fields. + if (is_array($schema->required) && ($key = array_search($propertyName, $schema->required, true)) !== false) { + unset($schema->required[$key]); + } + } + if ($schema->required === []) { + unset($schema->required); + } + $types = $this->propertyInfo->getTypes($class, $propertyName); if (null === $types || 0 === count($types)) { throw new \LogicException(sprintf('The PropertyInfo component was not able to guess the type of %s::$%s. You may need to add a `@var` annotation or use `@OA\Property(type="")` to make its type explicit.', $class, $propertyName));