Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: override code based defaults with explicit ones #2377

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/ModelDescriber/ObjectModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,6 @@

$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,22 @@
continue;
}

if (Generator::UNDEFINED === $property->default && array_key_exists($propertyName, $defaultValues)) {
$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) {
$schema->required = Generator::UNDEFINED;

Check failure on line 184 in src/ModelDescriber/ObjectModelDescriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property OpenApi\Annotations\Schema::$required (array<string>) does not accept string.
DjordyKoert marked this conversation as resolved.
Show resolved Hide resolved
}

$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
Loading