[JAVA-3208] Allow @BsonProperty to set the write name on a field when no setter is available Created: 20/Feb/19 Updated: 23/Feb/21 Resolved: 07/Jan/20 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | POJO |
| Affects Version/s: | 3.10.0 |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Minor - P4 |
| Reporter: | Eric Tray | Assignee: | Ross Lawley |
| Resolution: | Won't Fix | Votes: | 2 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
The PropertyModel for the field in the class above looks like:
Preferably I would expect propertyName="ssn". If that isn't an option than I would like to set the writeName="ssn" without the use of a setter. If I add a setter with the @BsonProperty it works as expected,
This can be done by manually configuring the PropertyModel writeName via the ClassBuilder etc., but ideally this should be able to be configured via annotations. Could this be achieved by allowing a @BsonProperty annotation on a field to set the writeName?
|
| Comments |
| Comment by Andrew G10i [ 23/Feb/21 ] | |||||||||||||
|
Thank you, @Ross, for suggesting the alternative solution for this! I have created a custom Convention which works good so far. Posting my solution here for those like me who spent hours figuring out what is the issue first and then looking for solution in the web but could not find one. Scenario below: WriteName property should be replaced from "files" to "_files" on objects which implement BasicFileAwareModel Interface.
+1 to those who is looking for native Mongo Java Driver capabilities for defining the WriteName when Conventions.USE_GETTERS_FOR_SETTERS is in use. P.S.S. Just had to re-post my reply from '19. | |||||||||||||
| Comment by Ross Lawley [ 07/Jan/20 ] | |||||||||||||
|
Marking as Won't Fix. The property name of a property model is defaulted to camelCaps of the property name itself. This can be changed by a custom convention without causing any backwards compatibility issues with users who expect the existing convention to be adhered to. | |||||||||||||
| Comment by Eric Tray [ 12/Aug/19 ] | |||||||||||||
|
Thanks, I'll give the custom Convention a shot when I get a chance. | |||||||||||||
| Comment by Ross Lawley [ 09/Aug/19 ] | |||||||||||||
|
This would require more logic in the ConventionAnnotationImpl so that this example overrides the default naming convention for propertyBuilders. In the meantime an alternative solution could be to create a custom Convention and annotation that renames all the property names. |