mklemm / jaxb-rich-contract-plugin

Implements some JAXB plugins to generate advanced contract scenarios from XSD
MIT License
67 stars 33 forks source link

Optional flag to make builder return null upon calling build if all fields are null #65

Closed mastersang closed 6 months ago

mastersang commented 3 years ago

Suppose I have this class: public class ClassC { private ClassA a; private ClassB b; } Then I would like this line ClassC.builder().withA(null).withB(null).build() to give me null instead of an object of type ClassC with both fields a and b null. Do you think this is a feasible extension?

ryku123 commented 2 years ago

Hi, glad finding this request. I am facing with feeding data from my custom model to generated java classes.

Currently adding nested complex elements like below: withXXX(ClassC.builder().withA(null).withB(null).build())

results in an empty tag being marshalled: <someTag/>

withXXX(...) calls addXXX(...) and this adds list size 1 with empty element (all are null's)

Screenshot 2022-05-13 at 13 36 57

using with call without arguments withXXX() adds empty list with size=0 so now <someTag> is omitted once marshalling.

Screenshot 2022-05-13 at 13 37 23

So i need to do extra work each time in order to check manually if a model will contain some null fields and then decide which "withXXX" to apply.

Any idea on elegant solution to this issue?

mklemm commented 2 years ago

Hi,

you could try to add „xsi:nillable“ to the elements in question, so they will be added in any case.

Am 2022-05-13 um 13:39 schrieb ryku @.***>:

Hi, glad finding this request. I am facing with feeding data from my custom model to generated java classes.

Currently adding nested complex elements like below: withXXX(ClassC.builder().withA(null).withB(null).build())

results in an empty tag being marshalled:

withXXX(...) calls addXXX(...) and this adds list size 1 with empty element (all are null's)

https://user-images.githubusercontent.com/8982833/168275554-ca486f59-9654-4ce2-aab5-8cf090d1d863.png using with call without arguments withXXX() adds empty list with size=0 so now is omitted once marshalling.

https://user-images.githubusercontent.com/8982833/168275566-8cc52874-bfb0-4170-b463-0aa15020496e.png So i need to do extra work each time in order to check manually if a model will contain some null fields and then decide which "withXXX" to apply.

Any idea on elegant solution to this issue?

— Reply to this email directly, view it on GitHub https://github.com/mklemm/jaxb2-rich-contract-plugin/issues/65#issuecomment-1125967334, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMTIUMGK7HKFE3SS4MYGVLVJY5NJANCNFSM4W63CREQ. You are receiving this because you are subscribed to this thread.

mklemm commented 6 months ago

No planning on supporting this, even there might by some use for it in some situations. It's a very special requirement, though, and it won't work in all cases, since properties may be non-nullable and initialized with a default value, and you won't be able to tell inside the builder when properties are actually "not set" or set to their default value explicitly...