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

Add support for new JsonTypeInfo.Id.SIMPLE_NAME polymorphic type id option #4065

Merged

Conversation

JooHyukKim
Copy link
Member

@JooHyukKim JooHyukKim commented Aug 2, 2023

(resolves #4061)

Description

  • As discussed in the original issue, implementation of TypeNameIdResolver fits right in, so implementation is heavily referenced.
  • Note that we do not actually invoke Class.getSimpleName(), goal is handling type value equivalent to what Class::getSimpleName returns.

// no name? Need to figure out default; for now, let's just
// use non-qualified class name
Class<?> cls = t.getType();
String id = t.hasName() ? t.getName() : cls.getSimpleName(); // not {@code _defaultTypeId(cls);}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we use cls.getSimpleName(); not _defaultTypeId(cls); like in TypeNameIdResolver

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it still makes sense to extract _defaultTypeId() method here, too, same as in TypeNameIdResolver.java.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, will put back _defaultTypeId() and instead modify change its implementation a little bit. Here is the link 👉🏼 https://github.com/FasterXML/jackson-databind/pull/4065/files#r1283295309

* Since lazily constructed will require synchronization (either internal
* by type, or external)
*/
protected final ConcurrentHashMap<String, String> _typeToId;
Copy link
Member

@cowtowncoder cowtowncoder Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to use concurrent data structure when it instances are immutable.

EDIT: I missed the part where we do actually modify the Map. So this is actually needed, just like TypeNameIdResolver.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not immutable. It has a mutable Map.

I think what is meant is whether a single SimpleClassNameIdResolver instance can be used by 2 threads at the same time. If it can be used by multiple threads, then this should be a concurrent map.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pjfanning Yes you are correct, I first only looked at construction but then noticed mutation later on. So my comment was based on only reading part of the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻👍🏻

*
* @since 2.16
*/
public class JsonTypeInfoSimpleClassName4061Test extends BaseMapTest
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to get some coverage, at least as much as other TypeIdResolver classes

@JooHyukKim JooHyukKim changed the title Add JsonTypeInfo.Id.SIMPLE_NAME that uses Class::getSimpleName Add new JsonTypeInfo.Id.SIMPLE_NAME Aug 3, 2023
@cowtowncoder
Copy link
Member

I merged the annotation change so this can proceed I think. Not sure why JDK 11 tests fail...

@JooHyukKim
Copy link
Member Author

I merged the annotation change so this can proceed I think. Not sure why JDK 11 tests fail...

After applying your suggestion in #4065 (comment), now JDK 17 tests fail. It seems that the ordering mechanism does not work as I thought it would.🤔 Will look into this.

@JooHyukKim
Copy link
Member Author

JooHyukKim commented Aug 19, 2023

I merged the annotation change so this can proceed I think. Not sure why JDK 11 tests fail...

Seems like type resolution is done in unpredictable order, specifically in StdSubtypeResolver.collectAndResolveSubtypesByTypeId(). So I did two things.

First I filed PR #4086 to resolve this issue wrt unpredictable ordering in type resolution.

Then created reproducible "resolved" demonstration in my personal fork which shows that PR #4086 can actually fix this issue.

/cc @cowtowncoder

@cowtowncoder
Copy link
Member

Merged #4086, tests now seem to pass.

@JooHyukKim JooHyukKim marked this pull request as ready for review August 20, 2023 02:45
@JooHyukKim JooHyukKim marked this pull request as draft August 22, 2023 13:00
@JooHyukKim JooHyukKim marked this pull request as ready for review August 22, 2023 13:00
@cowtowncoder cowtowncoder changed the title Add new JsonTypeInfo.Id.SIMPLE_NAME Add support for new JsonTypeInfo.Id.SIMPLE_NAME polymorphic type id option Aug 26, 2023
@cowtowncoder cowtowncoder merged commit 3579be0 into FasterXML:2.16 Aug 26, 2023
3 checks passed
@JooHyukKim JooHyukKim deleted the 4061-JsonTypeInfo.Id.SIMPLE_NAME- branch August 26, 2023 01:03
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.

Add JsonTypeInfo.Id.SIMPLE_NAME which defaults type id to Class.getSimpleName()
3 participants