Skip to content

fix(constructor): fixed circular references with promoted properties #272

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions src/Generator/MapMethodStatementsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ public function getStatements(GeneratorMetadata $metadata): array
}

if (\count($duplicatedStatements) > 0 && \count($metadata->getPropertiesInConstructor())) {
/**
* We know that the last statement is an `if` statement (otherwise we can't add an `else` statement).
* Without this logic, the addedDependencies would only be called when the target was set. If the target is
* `null` instead, the code looked like:
*
* ```php
* if (null !== result) {
* $result = // Extracted with constructor arguments
* $context = \AutoMapper\MapperContext::withReference($context, $sourceHash, $result);
* $context = \AutoMapper\MapperContext::withIncrementedDepth($context);
* } else {
* $context = \AutoMapper\MapperContext::withReference($context, $sourceHash, $result);
* $context = \AutoMapper\MapperContext::withIncrementedDepth($context);
*
* $source->propertyName = $this->extractCallbacks['propertyName']($source);
* }
*/
$lastStatement = $statements[array_key_last($statements)];
assert($lastStatement instanceof Stmt\If_);
$lastStatement->stmts = [
...$lastStatement->stmts,
...$addedDependenciesStatements,
];
/*
* Generate else statements when the result is already an object, which means it has already been created,
* so we need to execute the statements that need to be executed before the constructor since the constructor has already been called
Expand Down
16 changes: 16 additions & 0 deletions tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use AutoMapper\Tests\Fixtures\AddressDTOWithReadonlyPromotedProperty;
use AutoMapper\Tests\Fixtures\AddressType;
use AutoMapper\Tests\Fixtures\AddressWithEnum;
use AutoMapper\Tests\Fixtures\Category;
use AutoMapper\Tests\Fixtures\CategoryDTO;
use AutoMapper\Tests\Fixtures\ClassWithMapToContextAttribute;
use AutoMapper\Tests\Fixtures\ClassWithNullablePropertyInConstructor;
use AutoMapper\Tests\Fixtures\ClassWithPrivateProperty;
Expand All @@ -40,6 +42,7 @@
use AutoMapper\Tests\Fixtures\Order;
use AutoMapper\Tests\Fixtures\PetOwner;
use AutoMapper\Tests\Fixtures\PetOwnerWithConstructorArguments;
use AutoMapper\Tests\Fixtures\Post;
use AutoMapper\Tests\Fixtures\SourceForConstructorWithDefaultValues;
use AutoMapper\Tests\Fixtures\Transformer\MoneyTransformerFactory;
use AutoMapper\Tests\Fixtures\Uninitialized;
Expand Down Expand Up @@ -1376,6 +1379,19 @@ public function testAutoMapperFixtures(string $mapFile, string $directory): void
}
}

public function testCircularReferenceForPromotedProperties(): void
{
$category = new Category('Category');
$category->posts[] = new Post('Example', $category);

$categoryDTO = $this->autoMapper->map($category, CategoryDTO::class);

self::assertEquals('Category', $categoryDTO->name);
self::assertCount(1, $categoryDTO->posts);
self::assertEquals('Example', $categoryDTO->posts[0]->name);
self::assertEquals($categoryDTO, $categoryDTO->posts[0]->category);
}

public static function provideAutoMapperFixturesTests(): iterable
{
$directories = (new Finder())
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures;

class Category
{
/** @var Post[] */
public array $posts = [];

public function __construct(public string $name)
{
}
}
19 changes: 19 additions & 0 deletions tests/Fixtures/CategoryDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures;

class CategoryDTO
{
/**
* @var PostDTO[]
*/
public array $posts = [];

public function __construct(
public string $name,
)
{
}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures;

class Post
{
public function __construct(
public string $name,
public Category $category
) {
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/PostDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures;

class PostDTO
{
public function __construct(
public string $name,
public CategoryDTO $category,
)
{
}
}