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.
Not directly related to the main changes of this pull request, but checking for reference equality here is more correct.
eamonnmcmanus
left a comment
There was a problem hiding this comment.
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 |
| */ | ||
| 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
There was a problem hiding this comment.
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 theinstanceCreatorsmap. 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
instanceCreatorsto theConstructorConstructorconstructor 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-argsConstructorConstructorconstructor 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:jarpasses without errors