Skip to content
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

Merged

Conversation

Marcono1234
Copy link
Collaborator

@Marcono1234 Marcono1234 commented Feb 20, 2025

Purpose

Fix GraphAdapterBuilder#addType(Type) delegating to previous creator

Description

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 from constructorConstructor, it obtained the previously registered creator. That does not seem intended to me.

Side note: Passing instanceCreators to the ConstructorConstructor 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-args ConstructorConstructor constructor in 040bae3 (?).

Checklist

  • New code follows the Google Java Style Guide
    This is automatically checked by mvn verify, but can also be checked on its own using mvn 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.
  • If necessary, new public API validates arguments, for example rejects null
  • New public API has Javadoc
    • Javadoc uses @since $next-version$
      ($next-version$ is a special placeholder which is automatically replaced during release)
  • If necessary, new unit tests have been added
    • Assertions in unit tests use Truth, see existing tests
    • No JUnit 3 features are used (such as extending class TestCase)
    • If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
  • mvn clean verify javadoc:jar passes without errors

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.
Comment on lines -159 to +207
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);
Copy link
Collaborator Author

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.

Copy link
Member

@eamonnmcmanus eamonnmcmanus left a 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.

@Marcono1234
Copy link
Collaborator Author

Have fixed the merge conflict, and included a small fix for when GraphAdapterBuilder is reused.

@@ -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

Potentially confusing name: method
registerOn
also refers to field
instanceCreators
(as this.instanceCreators).
Copy link
Collaborator Author

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.

@eamonnmcmanus eamonnmcmanus merged commit c395dd1 into google:main Feb 22, 2025
11 checks passed
@Marcono1234 Marcono1234 deleted the marcono1234/graph-adapter-addType branch February 22, 2025 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants