Skip to content

Enable WebAPI interface to handle parameters through constructor #14801

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

Conversation

phoenix128
Copy link
Contributor

@phoenix128 phoenix128 commented Apr 22, 2018

Add construct parameters injection for WebAPI interfaces.

Description

WebAPI interfaces were relying on setter/getter methods violating the immutable stateless pattern.
This Pull Request enables the ServiceInputProcessor to get arguments directly from constructor. Setters methods are not required anymore.

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

DTO setter methods are no more required, parameters can be handled through constructor for immutable state approach
A setter fallback is supported
phoenix128 and others added 4 commits April 22, 2018 12:49
_createFromArray will not be fixed due to its eventual deprecation, since a new immutable approach is provided
…m:phoenix128/magento2 into webapi-support-immutable-construct-params
@@ -82,6 +89,7 @@ public function __construct(
AttributeValueFactory $attributeValueFactory,
CustomAttributeTypeLocatorInterface $customAttributeTypeLocator,
MethodsMap $methodsMap,
\Magento\Framework\ObjectManager\ConfigInterface $config,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @phoenix128 , due to Magento backward-compatible guide we can't add required parament to the constructor.
Please add the optional parameter and take instance from ObjectManager, as example:
$this->storeManager = $storeManager ?: \Magento\Framework\App\ObjectManager::getInstance()>get(\Magento\Store\Model\StoreManagerInterface::class);

@VladimirZaets
Copy link
Contributor

Hi @phoenix128 , any updates?

@phoenix128
Copy link
Contributor Author

Hi @VladimirZaets , sorry if I did not answer you before. I've been terribly busy. You are definitely right about that, I must have forgotten it while coding quickly. Fixing in the next commit.

phoenix128 and others added 3 commits May 13, 2018 19:39
Enable WebAPI interface to handle parameters through constructor magento#14801
- New optional param should be last in the params list.
@VladimirZaets
Copy link
Contributor

Hi @phoenix128. Thanks.
Also, optional param should be added to end of the params list. I fixed it.

@magento-engcom-team
Copy link
Contributor

Hi @phoenix128. Thank you for your contribution.
Changes from your Pull Request will be available with the upcoming 2.3.0 release.

@devamitbera
Copy link

@phoenix128

Can you please give an example how we can add Add construct parameters injection for WebAPI interface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants