-
-
Notifications
You must be signed in to change notification settings - Fork 921
Make resource constructor parameters writables #1749
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
Merged
dunglas
merged 1 commit into
api-platform:master
from
Nek-:feature/make-constructor-parameters-writable
Jun 8, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
features/serializer/deserialize_objects_using_constructor.feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Feature: Resource with constructor deserializable | ||
In order to build non anemic resource object | ||
As a developer | ||
I should be able to deserialize data into objects with constructors | ||
|
||
@createSchema | ||
Scenario: post a resource built with constructor | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/dummy_entity_with_constructors" with body: | ||
""" | ||
{ | ||
"foo": "hello", | ||
"bar": "world" | ||
} | ||
""" | ||
Then the response status code should be 201 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" | ||
And the JSON should be equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/DummyEntityWithConstructor", | ||
"@id": "/dummy_entity_with_constructors/1", | ||
"@type": "DummyEntityWithConstructor", | ||
"id": 1, | ||
"foo": "hello", | ||
"bar": "world", | ||
"baz": null | ||
} | ||
""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures; | ||
|
||
/** | ||
* @author Maxime Veber <[email protected]> | ||
*/ | ||
class DummyObjectWithConstructor | ||
{ | ||
private $foo; | ||
private $bar; | ||
|
||
public function __construct(string $foo, \stdClass $bar) | ||
{ | ||
$this->foo = $foo; | ||
$this->bar = $bar; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures; | ||
|
||
/** | ||
* @author Maxime Veber <[email protected]> | ||
*/ | ||
class DummyObjectWithoutConstructor | ||
{ | ||
private $foo; | ||
|
||
public function getFoo() | ||
{ | ||
return $this->foo; | ||
} | ||
|
||
public function setFoo($foo) | ||
{ | ||
$this->foo = $foo; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
tests/Fixtures/TestBundle/Entity/DummyEntityWithConstructor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
|
||
/** | ||
* Dummy entity built with constructor. | ||
* https://github.com/api-platform/core/issues/1747. | ||
* | ||
* @author Maxime Veber <[email protected]> | ||
* | ||
* @ApiResource( | ||
* itemOperations={ | ||
* "get", | ||
* "put"={"denormalization_context"={"groups"={"put"}}} | ||
* } | ||
* ) | ||
* @ORM\Entity | ||
*/ | ||
class DummyEntityWithConstructor | ||
{ | ||
/** | ||
* @var int The id | ||
* | ||
* @ORM\Column(type="integer") | ||
* @ORM\Id | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @ORM\Column | ||
*/ | ||
private $foo; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @ORM\Column | ||
*/ | ||
private $bar; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @ORM\Column(nullable=true) | ||
* @Groups({"put"}) | ||
*/ | ||
private $baz; | ||
|
||
public function __construct(string $foo, string $bar) | ||
{ | ||
$this->foo = $foo; | ||
$this->bar = $bar; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFoo(): string | ||
{ | ||
return $this->foo; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getBar(): string | ||
{ | ||
return $this->bar; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getBaz() | ||
{ | ||
return $this->baz; | ||
} | ||
|
||
/** | ||
* @param string $baz | ||
*/ | ||
public function setBaz(string $baz) | ||
{ | ||
$this->baz = $baz; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Long lines should be avoided, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split across more lines != easier to read 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think many nested levels of brackets, especially with uneven indentation levels, is much worse than long lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅