Restrict JSR-303 annotations to supported field types #1063
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.
I found that JSR-303 annotations were getting applied to types that didn't support them. For example, if a "pattern" was specified on a "string" property that also had a "format" of "uuid", then the generated field type is
UUID
class (rather thanString
) and@Pattern
was still applied. Because that annotation isn't valid on aUUID
type, then validators generate errors.Continuing with this example, because a schema may be used to generate source classes in non-Java languages, there is still value to keep the "pattern" restriction on the source json schema itself for languages that might not have an in-built UUID type.
This patch updates the various JSR-303 Rules classes to enhance them to check the generated class type of potentially annotated fields against the classes/types that are documented to support the relevant annotation (as documented in each annotation's JavaDoc).
In a second logical commit, Unit tests were added for thes JSR-303 rules classes. This required updating Mockito to a newer version that (properly) supports mocking final classes.
In a third logical commit, integration tests were updated with schemas that generate unsupported field types for the various cases, in addition to the supported types.