-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[FEATURE] Jacksonized would use NonNull to set JsonProperty(required = true) #2675
Comments
This one is a ton more straightforward. Lombok already has a system to recognize nonnull annotations specifically, so there's no real question as to how to identify them (there are like 10 different libraries with nonnulls that mean this). One slight issue is the confusing ones. For example, JPA has So that's one small source of annoyance: What if you annotate a field with a JPA But, that problem is rampant through all of lombok and is unsolvable, it's not our fault (example: If we generate a setter for a Is 'required = true' an ancient addition to |
However,
So this would look better with a |
... sigh. Nothing is ever easy with persistence and mapping, is it? I'm pulling the accepted tag for now. The problem with |
Just a s remark: Since Jackson 2.6, |
@janrieke What it does lines up with what I've outlined, which is that |
And with that, this feature is now rather related to #2669 I think. And that one is going nowhere about as fast as this is :( |
Alternate, even better, proposal: Mark This doesn't add new annotations, doesn't add semantically conflicting checks like |
But it is backwards incompatible. Code you write today without builder.defaults and with I think it is okay to do that, but will wait for some more input. Our experimental support of checkerframework's |
I'm having trouble finding more info about Lombok+CheckerFramework (eg the |
@almson said:
Yeah, I'm waiting for the docs too. The basics were communicated to is in private email from Michael Ernst (one of the checkerframework.org leads), we were under the impression the annotations we talked about would be added soon, but they aren't really there. I'll try to explain the basics of it, but first, I need to explain this extremely exotic java feature. This is legal java:
This java, if compiled, would compile to the exact same byte code as:
in all ways; e.g. trying to use reflection to get a handle on Seems pointless, but, there is a point to it all: It lets you annotate the receiver. That's a fancypants trick that so far no library I ever heard of actually uses, except this checkerframework idea. The idea being that you could write, say, this:
This is a typetag concept: It is saying that the
is semantically speaking as illegal as You can ask lombok to generate this stuff to tackle builders and mandatory params. This way, if you write, say: |
But, unfortunately, not in accordance with the Jackson semantics. Jackson does not do any validation by default, only if you request it explicitly. And no validation is what most people expect. Changing that will definitely be surprising and will cause too much harm. |
@janrieke Jackson doesn't do any validation on setters marked required. |
@rzwitserloot That's very interesting. I've heard of Checker Framework. It's intriguing. Essentially huge chunks of Java syntax were added just to support it. (Like... explicit receivers!) Yet I've never seen it used and it seemed just a research project. Glad to see it slowly going somewhere. In the distant future when I write my Python-killing Java ML library, I plan to use it to annotate tensor shapes. In the meantime, I've wanted to annotate https://github.com/almson/almson-regex which is a Regex builder library. The library ignores OOP for this task and passes around Strings (which makes it so much lighter and easier to use). At the same time, it could benefit from checks. There are 4 types in a regex: charclasses, expressions, quantifier expressions, and replacement expressions, and it would make the library easier to use if the compiler prevented them from getting mixed up. Do you know if CheckerFramework makes it easy to create such a basic type hierarchy? I think there's many problems where a full-blown OOP approach is too heavy-handed, but some sort of light, auxiliary, easy-to-ignore type tagging is just right. As for required Builder methods: Yeah, I found some epic threads about that:
Really, I don't see a huge problem in doing the check at runtime (maybe I've been doing too much Python). As they say--better late than never. But compile-time would be great too. I'm a bit surprised at the
|
That's true for the moment. However, |
I asked the Jackson team if/when/how they plan to handle this breaking change. FasterXML/jackson-databind#2618 (comment) |
Jackson team answered, saying that if implemented there would be an opt-in mechanism. Also, nobody is working to implement it. |
Perhaps a good behavior is to assume NonNull and only drop the constraint if a field is annotated as Nullable. |
Describe the feature
Related to #2673, Jackson uses
@JsonProperty(required=true)
to document required properties.@Builder
uses@NonNull
(according to #1043, although it's not documented). It would be nice if the Jackson metadata was generated from the corresponding Builder metadata. (Actually, a@Builder.Required
annotation that reuses@Builder.Default
machinery might look nicer and be more appropriate.)Describe the target audience
This feature benefits those who produce documentation and schemas of their Jackson classes. For example, I use https://github.com/mbknor/mbknor-jackson-jsonSchema It is really awesome because it works with https://github.com/json-editor/json-editor to automatically generate web forms.
Example
Here is the desired syntax:
Here is code that I currently write:
Granted, it's not a big savings, but it seems natural.
The text was updated successfully, but these errors were encountered: