Skip to content

Support BsonCreator with BsonProperty renaming#413

Closed
visualage wants to merge 6 commits intomongodb:masterfrom
visualage:master
Closed

Support BsonCreator with BsonProperty renaming#413
visualage wants to merge 6 commits intomongodb:masterfrom
visualage:master

Conversation

@visualage
Copy link
Copy Markdown
Contributor

The existing POJO codec does not work if a property is renamed using @BsonProperty("newName") on the property getter and a @BsonCreator with property @BsonProperty annotations to the parameter.

Also added support of @BsonId annotation to the @BsonCreator's parameter.

To my opinion, the value of @BsonProperty should be mapped to the property name in BSON, regardless where it is annotated. Right now, @BsonProperty value on a @BsonCreator maps to a property getter name. For example, if there is a getter @BsonId getPersonId(), the corresponding constructor parameter annotation should be @BsonId or @BsonProperty("_id) (since '_id' is the property name in BSON), not @BsonProperty("personId") ('personId' does not exist in BSON data). This patch corrects the behavior, but falls back to the old behavior if the new behavior cannot find the right property.

@rozza
Copy link
Copy Markdown
Member

rozza commented Sep 15, 2017

Hi @visualage,

Thanks for the ticket. I've added JAVA-2599 to track this issue.

A property has 3 names:

  • The original name of the property
    (either the field name or the method name (minus any bean conventions get/set).
  • The write name
  • The read name

This allows for a high level of flexibility over how data is controlled. Currently, in the context of @bsonCreator the @bsonproperty value relates to the actual property name and not the write name.

On reflection using the write name would have been a better choice and I like your backwards compatible approach, so thanks again for the patch. We'll look to incorporate into the next release.

In the meantime, you can provide your own Conventions to the PojoCodecProvider to configure these annotations.

Ross

@rozza
Copy link
Copy Markdown
Member

rozza commented Dec 4, 2017

Thanks @visualage after rebasing and some minor changes - your PR was commited to master: b376aa4

Thank you for the excellent PR and your work on improving the PojoCodec.

@rozza rozza closed this Dec 4, 2017
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