-
Notifications
You must be signed in to change notification settings - Fork 4.6k
airbyte-api: add jackson model annotations to remove null values from responses #13370
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
Merged
alafanechere
merged 9 commits into
master
from
augustin/airbyte-api/remove-null-from-json-responses
Jun 3, 2022
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e26ae21
add jackson model annotations to remove null values
alafanechere f161d44
remove nullable values from api spec
alafanechere 7e88690
Empty commit to run CI checks
alafanechere 7261c2b
Merge branch 'master' into augustin/airbyte-api/remove-null-from-json…
alafanechere 46ad3b7
Merge branch 'master' into augustin/airbyte-api/remove-null-from-json…
alafanechere e8c009f
Merge branch 'master' into augustin/airbyte-api/remove-null-from-json…
alafanechere fa56a4c
add comment
alafanechere 5a8e7a0
improve comment
alafanechere f7ca8f2
remove null values from connection test configs
alafanechere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,8 @@ task generateApiServer(type: GenerateTask) { | |
configOptions = [ | ||
dateLibrary : "java8", | ||
generatePom : "false", | ||
interfaceOnly: "true" | ||
interfaceOnly: "true", | ||
additionalModelTypeAnnotations: "\[email protected](com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)" | ||
] | ||
} | ||
compileJava.dependsOn tasks.generateApiServer | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
How did you decide between NON_NULL and NON_EMPTY? @jdpgrailsdev any opinion on which would make more sense here? My mental model is that we want the following:
I think we want this:
I think the change you are making does this:
The behavior before you change is this, right? Which we do not like.
Basically, my sense is fields that are not set should not be serialized, but fields that are set to null values should be serialized, but my intuition could be off on this one. I can see an argument for the way you have done it.
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.
I'm not sure if it is possible with these annotations, but it seems like the ideal behavior would be that fields with
nullable: true
are included in the serialized JSON if they are explicitly set to something (either null or a real value), and they are not included if they are not set. But fields that do not havenullable: true
should only ever be included in the serialized JSON if they are set to a non-null value, and they should not be included in the serialized JSON if they are set to null.This behavior would always keep the openAPI spec consistent with the behavior of the API, while still allowing us to explicitly set fields to null when needed (if they are nullable)
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.
derp. agreed with what lake said. if we need it to be nullable then we should set it as such. okay. i think the implementation in this PR make sense.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thank you @cgardens for the example! They are correct.
This is ideally what we want but I don't think it's achievable. When we create a model instance (from generated classes) the model attribute default to
null
in JAX-RS. It means that if we don't set the attribute on initialization the model instance will still have anull
value for non-set attributes. So the default behavior will be:The change I made with the annotation leads to the following:
Jackson cleans the object but without extra logic coming from the OpenAPI spec. It means that even if we set
nullable = true
on thelastName
attribute we won't end up with anull
value in the payload.It answers @lmossman's question:
This is not possible with this implementation.
@cgardens @lmossman @jdpgrailsdev I would like to know if you are ok with this and want you to be aware that with this change the
nullable = true
won't be of any effect in the Open API spec. I am under the impression it is an acceptable compromise at the moment as we are not using this property but we might want to find a more flexible solution in the future. I don't know what this solution could be but it might be changing the generator fromjaxrs-spec
to another likespring
🤔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.
Got it, it sounds like we can't get the ideal behavior we want with the current libraries we are using then. In that case, especially since it seems we aren't currently using nullable fields, I think your current implementation is acceptable.
@alafanechere I think it would be helpful to add a comment for this line that summarizes what we discussed here, to help explain why we are setting it to
NOT_NULL
and what that means for nullable fields.