-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix GraphAdapterBuilder#addType(Type)
delegating to previous creator
#2811
Fix GraphAdapterBuilder#addType(Type)
delegating to previous creator
#2811
Conversation
When `addType(Type, InstanceCreator)` is used, it adds the creator to the `instanceCreators` map. However, previously that map was also used by the `constructorConstructor`. So when `addType(Type)` was used afterwards, instead of actually obtaining the default creator, it obtained the previously registered creator.
assertThat(jesse.company).isEqualTo(company); | ||
assertThat(jesse.company).isSameInstanceAs(company); | ||
Employee joel = company.employees.get(1); | ||
assertThat(joel.name).isEqualTo("Joel"); | ||
assertThat(joel.company).isEqualTo(company); | ||
assertThat(joel.company).isSameInstanceAs(company); |
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.
Not directly related to the main changes of this pull request, but checking for reference equality here is more correct.
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.
Seems reasonable. I was going to run this against Google's internal tests (since Google internal code has access to the extras
package), but it turns out that nothing uses this.
Have fixed the merge conflict, and included a small fix for when |
@@ -121,6 +121,8 @@ | |||
* @param gsonBuilder the {@code GsonBuilder} on which to register the graph adapter | |||
*/ | |||
public void registerOn(GsonBuilder gsonBuilder) { | |||
// Create copy to allow reusing GraphAdapterBuilder without affecting adapter factory | |||
Map<Type, InstanceCreator<?>> instanceCreators = new HashMap<>(this.instanceCreators); |
Check notice
Code scanning / CodeQL
Possible confusion of local and field Note
registerOn
instanceCreators
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.
Intentionally shadowing this.instanceCreators
, to avoid accidentally referring to it instead of the copy again.
Purpose
Fix
GraphAdapterBuilder#addType(Type)
delegating to previous creatorDescription
When
addType(Type, InstanceCreator)
is used, it adds the creator to theinstanceCreators
map. However, previously that map was also used by theconstructorConstructor
.So when
addType(Type)
was used afterwards, instead of actually obtaining the default creator fromconstructorConstructor
, it obtained the previously registered creator. That does not seem intended to me.Side note: Passing
instanceCreators
to theConstructorConstructor
constructor was done in 1d9e86e. But I am not sure if this was done intentionally, or just to solve a compilation error due to the removal of the no-argsConstructorConstructor
constructor in 040bae3 (?).Checklist
This is automatically checked by
mvn verify
, but can also be checked on its own usingmvn spotless:check
.Style violations can be fixed using
mvn spotless:apply
; this can be done in a separate commit to verify that it did not cause undesired changes.null
@since $next-version$
(
$next-version$
is a special placeholder which is automatically replaced during release)TestCase
)mvn clean verify javadoc:jar
passes without errors