Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 25 additions & 9 deletions src/Metadata/MetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@ public function buildMetadata(string $className): ClassMetadata
throw new SerializerException('Cannot create reflection for ' . $className, 0, $exception);
}

$propertyMetadata = $this->buildPropertyMetadata($reflection);

return new ClassMetadata(
array_merge(
$this->buildPropertyMetadata($reflection),
$propertyMetadata,
$this->buildMethodMetadata($reflection)
),
$this->buildConstructorMetadata($reflection),
$this->buildConstructorMetadata($propertyMetadata, $reflection),
);
}

/**
* @throws SerializerException
*/
private function buildConstructorMetadata(ReflectionClass $reflection): array
private function buildConstructorMetadata(array $propertyMetadata, ReflectionClass $reflection): array
{
$constructorMethod = $reflection->getConstructor();
if (null === $constructorMethod) {
Expand All @@ -64,19 +66,26 @@ private function buildConstructorMetadata(ReflectionClass $reflection): array

$metadata = [];
foreach ($constructorMethod->getParameters() as $parameter) {
if ($parameter->isDefaultValueAvailable()) {
// we will use only the required parameters
continue;
}

$attribute = $this->findRelatedClassPropertyAttribute($reflection, $parameter->getName());

if (null === $attribute) {
// accept if the constructor has a property that should not be serialized
// because the object may only be used for serialization and not deserialization
$attribute = new Serialize();
}

$dataName = $attribute->serializedName ?? $parameter->getName();
$propertyMeta = $propertyMetadata[$dataName] ?? null;
if ($parameter->isPromoted() && $propertyMeta) {
$metadata[$dataName] = $propertyMeta;

continue;
}

if ($parameter->isDefaultValueAvailable()) {
// we will use only the required parameters
continue;
}

$metadata[$dataName] = new Metadata(
(string) $parameter->getType(),
Expand All @@ -89,7 +98,7 @@ private function buildConstructorMetadata(ReflectionClass $reflection): array
$attribute->strategy,
$attribute->persistedName,
$attribute->discriminatorMap,
orderBy: $attribute->orderBy
orderBy: $attribute->orderBy,
);
}

Expand Down Expand Up @@ -215,6 +224,13 @@ private function getPropertyMetadata(ReflectionProperty $property, Serialize $at
$setter = $property->getName();
}
}
if ($property->isPublic()) {
$getterSetterStrategy = false;
$getter = $setter = $property->getName();
if (version_compare(PHP_VERSION, '8.4.0', '>=') && $property->isPrivateSet()) {
$setter = null;
}
}
if ($getterSetterStrategy) {
$getter = $getterPrefix . ucfirst($property->getName());
$declaringClass = $property->getDeclaringClass();
Expand Down
5 changes: 4 additions & 1 deletion src/OpenApi/SerializerModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public function describe(Model $model, Schema $schema): void
// Method docBlock description.
if (null === $metadata->setter && null !== $model->getType()->getClassName()) {
/** @psalm-suppress ArgumentTypeCoercion */
$methodReflection = new ReflectionMethod($model->getType()->getClassName(), $metadata->getter);
$methodReflection = $metadata->getterSetterStrategy
? new ReflectionMethod($model->getType()->getClassName(), $metadata->getter)
: new ReflectionProperty($model->getType()->getClassName(), $metadata->getter)
;
$this->addDocBlockDescription($methodReflection, $property);
}

Expand Down