Skip to content

Commit

Permalink
Merge branch 'master' into feature/map-uploaded-file
Browse files Browse the repository at this point in the history
  • Loading branch information
jankal authored Jan 7, 2025
2 parents 8efceca + 8e56941 commit 5cc5688
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ jobs:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Upload code coverage to Codecov.io
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: true
env:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## 4.34.0
* Added support for the `#[MapUploadedFile]` symfony controller argument attribute

## 4.33.6
* Fixed Symfony 7.2 deprecation of tagged arguments

## 4.33.5
* Added new optional parameter `$context` to` PropertyDescriberInterface::supports()`

Expand Down
2 changes: 1 addition & 1 deletion config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@

<!-- Property Describers -->
<service id="nelmio_api_doc.object_model.property_describer" class="Nelmio\ApiDocBundle\PropertyDescriber\PropertyDescriber" public="false">
<argument type="tagged" tag="nelmio_api_doc.object_model.property_describer" />
<argument type="tagged_iterator" tag="nelmio_api_doc.object_model.property_describer" />

<tag name="nelmio_api_doc.object_model.property_describer" priority="100" />
</service>
Expand Down
71 changes: 60 additions & 11 deletions src/ModelDescriber/Annotations/ReflectionReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,26 @@ public function updateProperty(
if ($reflection instanceof \ReflectionMethod) {
$methodDefault = $this->getDefaultFromMethodReflection($reflection);
if (Generator::UNDEFINED !== $methodDefault) {
if (null === $methodDefault) {
$property->nullable = true;

return;
}
$property->default = $methodDefault;

return;
}
}

if ($reflection instanceof \ReflectionProperty) {
$methodDefault = $this->getDefaultFromPropertyReflection($reflection);
if (Generator::UNDEFINED !== $methodDefault) {
$property->default = $methodDefault;
$propertyDefault = $this->getDefaultFromPropertyReflection($reflection);
if (Generator::UNDEFINED !== $propertyDefault) {
if (Generator::UNDEFINED === $property->nullable && null === $propertyDefault) {
$property->nullable = true;

return;
}
$property->default = $propertyDefault;

return;
}
Expand All @@ -77,6 +87,7 @@ public function updateProperty(
if ($parameter->name !== $serializedName) {
continue;
}

if (!$parameter->isDefaultValueAvailable()) {
continue;
}
Expand All @@ -89,7 +100,12 @@ public function updateProperty(
continue;
}

$property->default = $parameter->getDefaultValue();
$default = $parameter->getDefaultValue();
if (Generator::UNDEFINED === $property->nullable && null === $default) {
$property->nullable = true;
}

$property->default = $default;
}
}

Expand Down Expand Up @@ -117,10 +133,6 @@ private function getDefaultFromMethodReflection(\ReflectionMethod $reflection)
return Generator::UNDEFINED;
}

if (null === $param->getDefaultValue()) {
return Generator::UNDEFINED;
}

return $param->getDefaultValue();
}

Expand All @@ -134,12 +146,49 @@ public function getDefaultFromPropertyReflection(\ReflectionProperty $reflection
return Generator::UNDEFINED;
}

$defaultValue = $reflection->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? null;
if (PHP_VERSION_ID < 80000) {
return $reflection->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? Generator::UNDEFINED;
}

if (!$reflection->hasDefaultValue()) {
return Generator::UNDEFINED;
}

if (null === $defaultValue) {
if ($this->hasImplicitNullDefaultValue($reflection)) {
return Generator::UNDEFINED;
}

return $defaultValue;
return $reflection->getDefaultValue();
}

/**
* Check whether the default value is an implicit null.
*
* An implicit null would be any null value that is not explicitly set and
* contradicts a set DocBlock @ var annotation
*
* So a property without an explicit value but an `@var int` docblock
* would be considered as not having an implicit null default value as
* that contradicts the annotation.
*
* A property without a default value and no docblock would be considered
* to have an explicit NULL default value to be set though.
*/
private function hasImplicitNullDefaultValue(\ReflectionProperty $reflection): bool
{
if (null !== $reflection->getDefaultValue()) {
return false;
}

if (false === $reflection->getDocComment()) {
return true;
}

$docComment = $reflection->getDocComment();
if (!preg_match('/@var\s+([^\s]+)/', $docComment, $matches)) {
return true;
}

return false === strpos(strtolower($matches[1]), 'null');
}
}

0 comments on commit 5cc5688

Please sign in to comment.