-
Notifications
You must be signed in to change notification settings - Fork 30
SLING-12359 - Extend records support #51
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
SLING-12359 - Extend records support #51
Conversation
18d39ca to
805f060
Compare
|
Thanks for the PR @ciechanowiec , happy to see further support added for records; in my own limited testing I did not run into this problem. My ask would be that you create a Jira for this enhancement as it's non-trivial (and impactful). I think someone with more models knowledge should review this ( maybe @kwin / @stefanseifert ? ). |
805f060 to
a4eefa9
Compare
|
@rombert , thank you for your comment. According to your suggestion, I've created a Jira ticket for this improvement and updated this pull request to reference that ticket: |
stefanseifert
left a comment
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.
thanks for the contribution - this looks very good!
i've created an experimental branch with a Java 17 based unit test actually using this extension and a Record class: https://github.com/apache/sling-org-apache-sling-models-impl/tree/experimental/SLING-12359-record-unit-test
it works fine for named injections - but it does not work without explicitly defining the attribute names - in this case it should default to the property name from code, it should not be required to put a name in the annotation if it's the same. is this possible without relying on Java 17?
we do not have to stick with Java 8, next parent update will raise it to Java 11 as minimum dependency.
|
@stefanseifert, thank you for your valuable insights. Below is my response regarding the topic of named constructor parameters. I'll address the rest of your suggestions separately to keep this comment focused. If I understand you correctly, you suggested that for unnamed parameters of record constructors (i.e., parameters without a
Therefore, if we introduce a rule that for unnamed parameters of record constructors (i.e., parameters without a a) would be applied inconsistently and only in cases where compilation was performed in a non-standard way, and b) would not comply with the current and expected Sling Models behavior for regular classes. Due to these reasons, I conclude that names for parameters of constructors that require a name for resolving the injection should be mandatory not only for regular classes (as currently implemented) but also for record classes. There should be no fallback to the record constructor parameter names themselves, just as there is no such fallback in case of regular classes. This pull request adheres to this conclusion and introduces the same behavior for record classes as is already implemented for regular classes. Please let me know if the pull request requires any changes regarding named constructor parameters, or if we can proceed as it is. I appreciate your guidance and look forward to your feedback. |
c1bdc05 to
9a3a622
Compare
you're right! thanks for the elaborate answer. that's not supported currently in sling models for any constructors currently. actually, there was a change in Java 8 to better get access to constructor parameters and their names and we have a SLING-11917 plus PR #45 pending for it - but it was not yet applied (and it only works with a special compiler option). so this is out of scope for this PR. |
stefanseifert
left a comment
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.
looks good!
cosmetic: can you try to get rid of the two new sonar issues (although they apply only to unit test code): https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl&pullRequest=51&resolved=false&sinceLeakPeriod=true
|



Java 14 introduced support for record classes, and Apache Sling 12 by default runs with Java 17, allowing the use of records.
Although Apache Sling 12 supports record classes, the current latest version of Apache Sling Models Implementation does not fully support injection in relation to records. Specifically, for records, the Apache Sling Models Implementation does support constructor injection similarly to a usual class if there is an explicit constructor annotated with
javax.inject.Inject:However, for records, Apache Sling Models Implementation does not support constructor injection if there is an implicit constructor. Such a constructor cannot be annotated with
javax.inject.Injectand will not be picked up during injection. Therefore, such a Sling Model will not be instantiated at all:This pull request addresses the above issue by introducing support for injection in records' implicit constructors.
Note:
java.lang.Recordclass.