-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
UnmatchedFieldTypeModule to deal with mismatched target types for fields #3574
UnmatchedFieldTypeModule to deal with mismatched target types for fields #3574
Conversation
Can one of the admins verify this patch? |
I would preserve the second commit |
d851ded
to
ab71e91
Compare
…of fields with values of different types
ab71e91
to
b68d207
Compare
- Reverted KubernetesDeserializer that fall-backed to a GenericKubernetesResource to deal with InvalidFormatException in Templates
b68d207
to
fd331b8
Compare
For serialization the only practical difference compared to existing behavior is that if you set the field and additional properties it gets serialized twice - which should be invalid yaml. It might be good to throw an exception in this case - that will make it clear there's an ambiguity.
That's a valid concern for deserialization. Seems like this behavior would be best enabled only for templates. |
The custom serializer prevents exactly that. The value in additionalProperties takes precedence over any other and it gets serialized only once. This circumstance could happen before (in fact we've seen it in some issue reports #3195). The new behavior will at least provide a valid YAML when serializing. Maybe we can log a warning or an info message if the circumstance is detected (when the field is ignored). |
The DeserializerModifier is applied per bean. There is no easy way to detect if the current bean (or its parents) is part of a list of Objects within a Template when updating the Deserializer builder. We can only do something like this at the SettableBeanPropertyDelegate deserializeAndSetMethod and check the JsonParser tree. But it might not look very nice. I'll give it a try. |
4f5faf0
to
799a0c4
Compare
799a0c4
to
f985686
Compare
Hi @shawkins |
For a warning by default should the additional properties be java-doc'd as having primacy over regular properties? - perhaps added to an interface like strimzi defines https://github.com/strimzi/strimzi-kafka-operator/blob/79e28149b6dd8dd7b61e0ed60aae6d26fb93b1b7/api/src/main/java/io/strimzi/api/kafka/model/UnknownPropertyPreserving.java Also this logic for isInTemplate won't work - it needs to preserve the state top down, if you have a template like:
Where you hit another object before the one with the parameters, then the flag will get flipped prematurely. |
3788438
to
c41a485
Compare
Well, the circumstance before was worst because the user ended up with an invalid JSON or YAML with duplicate entries. Now the user gets a warning in case the ambiguity is detected and ends up with a valid resource (but maybe unexpected). This default is only applicable if the user is actually using the ObjectMappers provided by the Serialization class, so I'm not sure if the Javadoc should include that entry. If it did we should add the conditional. In any case, I'm not sure how we could add this Javadoc easily to the project, since not all KubernetesResources might include the additional properties. Any ideas?
Should be fixed now ✔️ |
The two concerns are that it's a behavioral change and it's subtle. Historically there's a lot of warnings generated by fabric8, so some people will have that logging turned off entirely. Just in case it should be documented somehow and in the release notes.
It would be up to the generation logic right? The interface would have to be added into the javaInterfaces list when additionalProperties is true. But that does seem like a lot of work. |
This is the only way I can think of, besides something similar to #3570 (comment). But both approaches imply extra work, that I really don't think adds any value. I'll add an entry to the changelog. Maybe some notes on the marshal/unmarhsal methods of the Serializer make sense too. |
…lization defaults
Kudos, SonarCloud Quality Gate passed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
This is a partial implementation of what's described in #3460 (comment)
The new behavior is as follows (edited after #3574 (comment)):
additionalProperties
Map (if present)a. By default, this unmatched types deserialization is enabled only for Templates
b. Behavior can be enabled for all types by
Serialization.UNMATCHED_FIELD_TYPE_MODULE.setRestrictToTemplates(false);
additionalProperties
Map. In that case the field value is ignored.a. A warning will be logged if the situation is detected, indicating that a field is being ignored.
b. Log can be opted-out by:
Serialization.UNMATCHED_FIELD_TYPE_MODULE.setLogWarnings(false);
additionalProperties
Map are serialized.All of this logic is implemented in the
UnmatchedFieldTypeModule
Jackson module that sets a serializer and a deserializer modifier. The second commit enables this module by default.The only drawback when enabling the module is that Exceptions when deserializing input resources that have properties that don't match the target field type won't be thrown. Any user relying on this to validate an Input will lose this functionality (I'm not sure if this is an extended approach).
I would like to know if we should preserve the second commit (where the module is automatically enabled), or revert it and just provide the module as an opt-in feature. (@shawkins, @metacosm, @rohanKanojia, @oscerd, @iocanel)
The second commit also reverts the fallback implemented in #3526 which should no longer be necessary.
Relates to:
Type of change
test, version modification, documentation, etc.)
Checklist