Skip to content
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

Optional strict handling of BeanMapper mappings #75

Closed
robert-bor opened this issue Aug 7, 2017 · 1 comment
Closed

Optional strict handling of BeanMapper mappings #75

robert-bor opened this issue Aug 7, 2017 · 1 comment

Comments

@robert-bor
Copy link
Contributor

Currently, the following convention applies within 42:

  • all fields in a Form method must be writable to an Entity
  • all fields in a Result method must be readable from an Entity

At the moment this is not enforced. The consequence of this lenient approach is that mappings may fail silently.

It would be beneficial if BeanMapper enforces that all Form/Result properties are mappable to/from an Entity. If this happens at mapping time, a proper error is generated, instead of failing silently.

@robert-bor
Copy link
Contributor Author

The approach will be the following:

  • introduce a property for activating strict mappings for Form and Result classes, default: true
  • introduce a property for the suffix of a Form class, default: Form
  • introduce a property for the suffix of a Result class, default: Result

When the mapping is made for the first time (this can be checked against the cache), the strict rules must be checked:

  • if a getter or public field in a Form class cannot be mapped to the target, throw an exception
  • if a setter or public field in a Result class cannot be mapped from the source, throw an exception

On top of these settings, it must be possible to preregister source/target pairs and check these when the BeanMapper bean is created. This is functionality which is similar to Hibernate's ddl-auto=validate.

BeanMapper starter can make use of the preregistrations to component scan annotations which it will supply (BeanMapsTo for Form and BeanMapsFrom for Result). Classes with these annotations and their counterparts will be registered as pairs. As soon as the bean is created, the checks on these pairs will be performed, any failure of which results in an exception.

robert-bor added a commit that referenced this issue Oct 3, 2017
Issue #75. Added ability to add pairs of classes which must be checked as soon as a BeanMapper instance is created.

As soon as a BeanMapper instance is created (method build on BeanMapperBuilder), it will validate its
sets of Bean pairs. The strict side will be verified so that all properties contain matching properties
on the other side. If this is not the case, an exception will be thrown. The exception contains information
on all the Bean pairs and every mismatching property.

Within BeanMatchStore, there are two routes for a BeanMatch to be created. The main route is the existing one where
a match is set up runtime (or reused if already existing). The second route is the pre-bean instantiation validation
where are registered strict bean pairs are validated upfront. In both cases the validation is done at the time
of BeanMatch construction. The Bean pair validation routine makes use of the underlying exceptions to report them
back all at once.

Configuration allows the setting of the strict mapping convention. If this is enabled, any source class with the
set suffix (default 'Form') will be considered strict. Same counts for any target class with the set suffix
(default 'Result'). This means that the opposing side must have matches for all properties. If these are missing,
this will result in an exception. The principle is basically the same as strict bean pairs, but runtime instead of
before bean instantiation.

Note that strict mapping convention is disabled for dynamically generated classes (downsizeSource/downsizeTarget),
since these concepts rely on the fact that no full match can be supplied.
robert-bor added a commit that referenced this issue Oct 4, 2017
Issue #75. Added ability to add pairs of classes which must be checked as soon as a BeanMapper instance is created.

As soon as a BeanMapper instance is created (method build on BeanMapperBuilder), it will validate its
sets of Bean pairs. The strict side will be verified so that all properties contain matching properties
on the other side. If this is not the case, an exception will be thrown. The exception contains information
on all the Bean pairs and every mismatching property.

Within BeanMatchStore, there are two routes for a BeanMatch to be created. The main route is the existing one where
a match is set up runtime (or reused if already existing). The second route is the pre-bean instantiation validation
where are registered strict bean pairs are validated upfront. In both cases the validation is done at the time
of BeanMatch construction. The Bean pair validation routine makes use of the underlying exceptions to report them
back all at once.

Configuration allows the setting of the strict mapping convention. If this is enabled, any source class with the
set suffix (default 'Form') will be considered strict. Same counts for any target class with the set suffix
(default 'Result'). This means that the opposing side must have matches for all properties. If these are missing,
this will result in an exception. The principle is basically the same as strict bean pairs, but runtime instead of
before bean instantiation.

Note that strict mapping convention is disabled for dynamically generated classes (downsizeSource/downsizeTarget),
since these concepts rely on the fact that no full match can be supplied.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant