Add @Required annotation with optional validator#1558
Add @Required annotation with optional validator#1558WilliamBergamin merged 12 commits intoslackapi:mainfrom
Conversation
|
Looks like this has been a requested feature for GSON for some time now |
Oh yeah nice! Really the only thing I added in this implementation was to allow folks to define their own predicate that should be applied against the field that the annotation was added on. This was mainly so that folks could handle primitive types (if they wanted) since these get boxed automatically by the JVM. We could make it "dumber" though and do something similar to the stack overflow link where primitives are checked against their JVM defaults, that kinda thing 🤷♂️ |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #1558 +/- ##
============================================
- Coverage 73.28% 73.27% -0.02%
- Complexity 4513 4524 +11
============================================
Files 477 479 +2
Lines 14274 14321 +47
Branches 1487 1494 +7
============================================
+ Hits 10461 10494 +33
- Misses 2922 2932 +10
- Partials 891 895 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
WilliamBergamin
left a comment
There was a problem hiding this comment.
Nice work!! 💯 I think the approach is good 🚀
I left mainly minor comments regarding adhering to existing patterns in the project, creating a new directory for the annotations and improving test coverage
Thanks for putting this together
slack-api-model/src/test/java/test_locally/unit/GsonFactory.java
Outdated
Show resolved
Hide resolved
slack-api-client/src/main/java/com/slack/api/util/json/GsonFactory.java
Outdated
Show resolved
Hide resolved
| @@ -0,0 +1,5 @@ | |||
| package com.slack.api.model.annotation; | |||
There was a problem hiding this comment.
I'm thinking we move this to a new package/directory
/**
* Provides JSON custom annotation utilities for the classes in this library.
*/
package com.slack.api.util.annotations;There was a problem hiding this comment.
Given that these predicates are meant to live on slack model objects I think it makes sense for it to live in the model package, but I'm open to a new sub-module under model; maybe predicates (or something?)
...api-model/src/main/java/com/slack/api/util/json/RequiredPropertyDetectionAdapterFactory.java
Show resolved
Hide resolved
| package com.slack.api.model.annotation; | ||
|
|
||
| public interface FieldPredicate { | ||
| boolean test(Object obj); |
There was a problem hiding this comment.
Nice job on the interface approach 🚀 but I think the test terminology might be confusing, what do you think of check or validate instead?
Maybe validate is better since it aligns with validator
There was a problem hiding this comment.
Yeah that's fair; I went with test because that's the abstract method on the Predicate functional interface https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html
slack-api-model/src/main/java/com/slack/api/util/json/RequiredAdapterFactory.java
Outdated
Show resolved
Hide resolved
| registerTypeAdapters(builder, failOnUnknownProps); | ||
| } | ||
|
|
||
| public static void registerTypeAdapters(GsonBuilder builder, boolean failOnUnknownProps) { |
There was a problem hiding this comment.
I don't know why this is public but I don't wanna break anybody, so leaving it as is 🤷♂️
WilliamBergamin
left a comment
There was a problem hiding this comment.
Looking good 💯 thanks for addressing my comments
Left 2 more comments! let mw know what you think
The one on the test GsonFactory is a nice to have and aims to maintain existing pattern in the project
slack-api-client/src/main/java/com/slack/api/util/json/GsonFactory.java
Outdated
Show resolved
Hide resolved
slack-api-model/src/test/java/test_locally/unit/GsonFactory.java
Outdated
Show resolved
Hide resolved
| registerTypeAdapters(gsonBuilder, failOnUnknownProps); | ||
|
|
||
| if (failOnUnknownProps || config.isLibraryMaintainerMode()) { | ||
| gsonBuilder = gsonBuilder.registerTypeAdapterFactory(new UnknownPropertyDetectionAdapterFactory()); |
There was a problem hiding this comment.
This reassignment is redundant, so removing it
| gsonBuilder.registerTypeAdapterFactory(new RequiredPropertyDetectionAdapterFactory()); | ||
| } | ||
| if (config.isPrettyResponseLoggingEnabled()) { | ||
| gsonBuilder = gsonBuilder.setPrettyPrinting(); |
There was a problem hiding this comment.
Same thing here - there's no need for the re-assignmen
WilliamBergamin
left a comment
There was a problem hiding this comment.
Nice thanks for working on this 🚀 And your patients with the feedback 🙏
Adds a new
@Requiredannotation to the model package for use in marking attributes in model classes as "required". This is so we can annotate attributes of our model classes that are expected to be present on every instance of the object, so developers can safely access without any explicit null checks. Meant to be a drop-in replacement for in-line code comments or javadocs that state the same (but apply no actual enforcement at runtime)Category (place an
xin each of the[ ])Requirements
Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you agree to those rules.