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

Use static PropertyNamingStrategy instances in POJOPropertiesCollector._findNamingStrategy() #4109

Conversation

takezoe
Copy link
Contributor

@takezoe takezoe commented Sep 11, 2023

Returns static PropertyNamingStrategy instances defined in PropertyNamingStrategies from POJOPropertiesCollector._findNamingStrategy() instead of creating new instance every time.

This also eliminates risk of dead-lock mentioned in #2715 when subclasses of PropertyNamingStrategy are used with @JsonNaiming though they have been already deprecated.

@@ -1450,6 +1450,50 @@ private PropertyNamingStrategy _findNamingStrategy()
return pns;
}
}

Copy link
Member

@JooHyukKim JooHyukKim Sep 11, 2023

Choose a reason for hiding this comment

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

// Plz check Line 1448
PropertyNamingStrategy pns = hi.namingStrategyInstance(_config, _classDef, namingClass);

Possibly, at line 1448 (above) HandlerInstantiator is also doing what we are trying to prevent to happen here. namingStrategyInstance() JavaDoc says....

/**
  * Method called to construct a NamingStrategy instance used for specified
  * class.
  *
  * @since 2.1
  */

Copy link
Contributor Author

@takezoe takezoe Oct 7, 2023

Choose a reason for hiding this comment

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

HandlerInstantiator is given through configuration and it can return any instance of PropertyNamingStrategy. I think we can't rescue if HandlerInstantiator is used.

@@ -1450,6 +1450,50 @@ private PropertyNamingStrategy _findNamingStrategy()
return pns;
}
}

// PropertyNamingStrategy
if (namingClass == PropertyNamingStrategy.SnakeCaseStrategy.class) {
Copy link
Member

Choose a reason for hiding this comment

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

can this list of if stmts be made into a switch stmt with cases instead?

Copy link
Member

Choose a reason for hiding this comment

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

As far as I know the baseline version Java 8 doesn't support switch for Class 🧐

Copy link
Member

Choose a reason for hiding this comment

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

If we were to do this, I think switch logic should be moved into a static method of PropertyNamingStrategies, so here we would just call that method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved switching logic to PropertyNamingStrategies and added a test case.

@cowtowncoder
Copy link
Member

cowtowncoder commented Sep 12, 2023

@takezoe First of all, thank you for offering this contribution!

Now... I am not a big fan of this idea. I get that this could reduce memory usage slightly as well as tiny improvement in processing speed. But in most cases this occurs just once per class being (de)serialized.
If it occurs more often, usage may be wrong (not reusing ObjectMappers etc), symptom of something else.
But mostly I guess I don't like coupling between classes that are introduced here.

So let me ask you this: what is the motivation for suggesting the change?

@takezoe
Copy link
Contributor Author

takezoe commented Oct 1, 2023

@cowtowncoder Sorry for late response. My motivation is eliminating risk of dead-lock caused by pre-defined strategies with @JsonNaming on the library side.

In fact, we faced the dead-lock issue by mixed use of

  • @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) and
  • @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)

Of course, we can fix our codes to only use @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) if all codes that use Jackson are under our management, but even third-party libraries could still use PropertyNamingStrategy. This will eliminate such risk without breaking backward compatibility.

@JooHyukKim
Copy link
Member

JooHyukKim commented Oct 1, 2023

Of course, we can fix our codes to only use @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) if all codes that use Jackson are under our management, but even third-party libraries could still use PropertyNamingStrategy.

✅ True, sounds like keeping consistency @JsonNaming can be challenging when third-party libraries are each doing their own thing. Reasonable thing to ask, from a user's perspective.

❌ Considering this fix pertains to a class deprecated since Jackson 2.7 or 2.12 2~3 years back, encouraging third-party authors to update their Jackson dependencies or the usage might be beneficial in the long run for everyone. As per #2715, numerous projects have applied the official fix without modifying Jackson directly.

@takezoe
Copy link
Contributor Author

takezoe commented Oct 2, 2023

Then, I hope that deprecated static fields of PropertyNamingStrategy will be dropped shortly to prevent unlucky but critical accident 🙏

@cowtowncoder
Copy link
Member

Then, I hope that deprecated static fields of PropertyNamingStrategy will be dropped shortly to prevent unlucky but critical accident 🙏

The challenge is that deprecation only occurred in 2.12, first released under 3 years ago (and latest patch bit over 1 year ago), so despite the risk, there's very likely code out there that would get broken when someone upgrades from 2.15 or earlier to 2.16, should we drop deprecated classes now. This compared risk of possible race condition due to someone using old implementations (I don't believe mixing of new and old should be issue but simply use of old ones).

What I can do is file an issue for dropping these classes from Jackson 2.17, if agreed to by discussions within Jackson developer community.

@takezoe
Copy link
Contributor Author

takezoe commented Oct 4, 2023

What I can do is file an issue for dropping these classes from Jackson 2.17, if agreed to by discussions within Jackson developer community.

Thank you. That sounds good!

@takezoe
Copy link
Contributor Author

takezoe commented Oct 7, 2023

If we were to do this, I think switch logic should be moved into a static method of PropertyNamingStrategies, so here we would just call that method.

Not sure if this pull request has a chance to be accepted but I anyway updated to cover this comment.

@cowtowncoder
Copy link
Member

I think I'd rather go with the alternate approach on warning, deprecation, eventual removal.

@cowtowncoder
Copy link
Member

Closing in favor of #4144

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.

4 participants