-
Notifications
You must be signed in to change notification settings - Fork 31
Validate JSON document during auto-rules creation #539
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
Validate JSON document during auto-rules creation #539
Conversation
The gson instance, and generally all other constructor-assigned fields that are provided by dependency injection, is final because the field reference should never need to be overwritten. Changing the field reference implies that the injected dependency is no longer being used, which violates the purpose of injecting the common dependency. In the case of the Gson instance, it's because the MainModule provides a single instance so that we have uniform behaviour of (de)serialization across the application.
This makes me think this may not be the correct deserializer to use, then. If I'm understanding correctly, this is what Gson will use when deserializing something in JSON that looks like an integer value, so that you could instead map that to a different type than the Java Integer.
It's probably not of much concern.
Another possible path along this line of thought is to extract the validation that currently occurs in the Rule constructor (which is called by the Builder.build()) into a method like Rule.validate(). The Rule constructor can then call validate() at the end, after it has assigned all of its own fields. In the JSON parsing pathway of the handler, the handler can call rule.validate() on the Rule instance that is created by Gson. However, the cleanest approach may be to use a deserializer/adapter, so that Gson itself knows how to deal with invalid Rule JSON definitions, and we don't have to ensure we call rule.validate() everywhere one may be deserialized. Our existing |
You know, now that I've said this, maybe the correct approach is to combine them. There should be a |
This is probably for later enhancements in this same area: https://stackoverflow.com/questions/21626690/gson-optional-and-required-fields We could use an approach like this to create annotations and register adapters/deserializers to perform validations, like ensuring deserialized reference-type fields are non-null, or numeric types are positive, etc. |
73d8087
to
18356c4
Compare
|
That's fine.
|
6c0d95a
to
2645c40
Compare
2645c40
to
86581a1
Compare
488778b
to
fde579b
Compare
src/test/java/io/cryostat/net/web/http/api/v2/RulesPostHandlerTest.java
Outdated
Show resolved
Hide resolved
I'm done making changes. PR is ready to merge |
…6.0 to 4.8.6.1 (cryostatio#539) build(deps): bump com.github.spotbugs:spotbugs-maven-plugin Bumps [com.github.spotbugs:spotbugs-maven-plugin](https://github.com/spotbugs/spotbugs-maven-plugin) from 4.8.6.0 to 4.8.6.1. - [Release notes](https://github.com/spotbugs/spotbugs-maven-plugin/releases) - [Commits](spotbugs/spotbugs-maven-plugin@spotbugs-maven-plugin-4.8.6.0...spotbugs-maven-plugin-4.8.6.1) --- updated-dependencies: - dependency-name: com.github.spotbugs:spotbugs-maven-plugin dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Fixes #530
@andrewazores I found a
JsonDeserializer
that can reuserequireNonNegative()
and I have some questions about how to implement it.private final Gson gson
needs to be modified in order to register theType Adapter
forIntegerDeserializer
. Why isgson
final
?IntegerDeserializer
gets passed JSON values without their keys, which means I'm not sure how to pass the correctAttribute
torequireNonNegative()
.IntegerDeserializer
only works if our numeric values are of typeInteger
, notint
. How will this affect performance?Here's the output as of now:
Some of the other approaches I've thought of include parsing
params.getBody()
before callinggson.fromJson(...)
or using aRule.Builder
similar to the JSON form pathway. I'd like to avoid parsing the JSON more than once though, and am not sure how to efficiently map key/value pairs from theparams.getBody()
string to theRule.Builder
.