Skip to content

Commit

Permalink
Override code based defaults with explicit ones
Browse files Browse the repository at this point in the history
This change allows that defaults that are set in attributes can override
the ones that are read from the code.

This fixes nelmio#2330
  • Loading branch information
heiglandreas committed Nov 6, 2024
1 parent 1023c9d commit 8f5eed3
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/ModelDescriber/ObjectModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,6 @@ public function describe(Model $model, OA\Schema $schema)

$property = Util::getProperty($schema, $serializedName);

// Fix for https://github.com/nelmio/NelmioApiDocBundle/issues/2222
// Property default value has to be set before SymfonyConstraintAnnotationReader::processPropertyAnnotations()
// is called to prevent wrongly detected required properties
if (Generator::UNDEFINED === $property->default && array_key_exists($propertyName, $defaultValues)) {
$property->default = $defaultValues[$propertyName];
}

// Interpret additional options
$groups = $model->getGroups();
if (isset($groups[$propertyName]) && is_array($groups[$propertyName])) {
Expand All @@ -175,6 +168,10 @@ public function describe(Model $model, OA\Schema $schema)
continue;
}

if (Generator::UNDEFINED === $property->default && array_key_exists($propertyName, $defaultValues)) {
$property->default = $defaultValues[$propertyName];
}

$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));
Expand Down

0 comments on commit 8f5eed3

Please sign in to comment.