-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Add type declaration to setter methods of generated ExtensionAttribute classes #2451
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
Add type declaration to setter methods of generated ExtensionAttribute classes #2451
Conversation
👍 Hopefully authentication for travis builds will be fixed soon, so the PR can be processed further. |
002ba2b
to
2d5ea54
Compare
Please merge latest from develop and rerun builds. |
bf9b759
to
29b57bd
Compare
@vancoz It looks like the tests are failing because there is core code that is setting null values on methods that are now expecting objects of a certain type: (From https://travis-ci.org/magento/magento2/jobs/102137070#L587) Can you have one of your architects review this pull request and confirm that they would like to accept this pull request, assuming the build is green? If so, then I can spend the time to refactor any core Magento code that tries to use an extension attribute method to set a value that isn't an instanceof the proper interface. But I only want to spend this time if this PR is going to be merged. My refactoring will look for any places where a null value could be passed to an extension attribute method that expects an object that implements the defined interface, and if so, it won't try to set a value. |
Change makes sense, thanks for contribution! Could you please fix Travis builds so that we will be able accept this PR. |
@okorshenko Are you confirming that you would accept this PR if I make the changes I mentioned on January 14th? |
@erikhansen , yes, I do |
PR is Accepted, but tests should be fixed before merge |
Hi @erikhansen. Should we wait for tests fixes for this Pull Request or this PR can be closed? |
- It is important to have type declarations added to the setter methods of ExtensionAttribute classes as it ensures that the getter methods will always return objects of the proper interface/class - Added isValidTypeDeclaration method to \Magento\Framework\Reflection\TypeProcessor to centralize the logic for determine whether a string is valid as a type declaration. When and if Magento 2 supports PHP 7, the logic of this method should be changed to support primitive types. - Tests updated/added to reflect changes
29b57bd
to
dd90e3a
Compare
@okorshenko I just fixed the issue with the test that failed back in January. I'll see what additional tests fail and resolve those issue, assuming the fixes are not too intensive. |
0d293e3
to
a56b768
Compare
@okorshenko Travis tests are now green, so this PR should be ready to merge into develop. |
@okorshenko Bump. |
Hi @erikhansen . We are running internal builds against your branch. |
@erikhansen we successfully executed all builds against your branch. There are couple issues that we need to fix before merging this PR. |
@okorshenko Ok, good. Do I need to fix the issues or are you going to take care of them? If the former, can you let me know what the issues are? |
} | ||
|
||
/** | ||
* @param \Magento\Bundle\Api\Data\BundleOptionInterface |
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.
param annotation should looks like:
- @param \Magento\Bundle\Api\Data\BundleOptionInterface $complexObjectAttributeWithTypeDeclaration
but this is not issue of this PR
@erikhansen I added some comments to show you what issue we found in this PR. We will fix this and and will run all tests again. Thank you for your contribution. |
@okorshenko - Interesting approach to keeping BC, I must say. But it works. Slightly offtopic here, but do M2 devs agree with the whole |
@erikhansen your pull request merged to develop branch. Thank you for your contribution. |
[PERFORMANCE] Use price index to calculate price for configurable products
The code that generates the classes that implement the
\Magento\Framework\Api\ExtensionAttributesInterface
interface were doing so in a way that did not add type declarations to the setter methods. This pull request adds functionality to add type declarations, but only for types that are valid type declarations. It is important to have type declarations added to the setter methods of ExtensionAttribute classes as it ensures that the getter methods will always return objects of the proper interface/class.Summary of changes:
\Magento\Framework\Api\Code\Generator\ExtensionAttributesGenerator::_getClassMethods
method to add type declaration to method, as long as type is a valid (e.g. not a primitive type or an array of classes/interfaces).\Magento\Framework\Reflection\Test\Unit\TypeProcessorTest::testIsValidTypeDeclaration
test. I wrote what I believe to be a very comprehensive set of true and false assertions.Here is an example of how this auto-generated file changes:
var/generation/Magento/Catalog/Api/Data/ProductAttributeMediaGalleryEntryExtension.php
.Before pull request
After pull request
You'll notice that the
setVideoContent
method has a\Magento\Framework\Api\Data\VideoContentInterface
type declaration.