[bv-dev] Should getters be considered methods during validation

Gunnar Morling gunnar at hibernate.org
Wed Nov 7 08:22:53 EST 2012


Hi,

2012/11/7 Hardy Ferentschik <hardy at hibernate.org>

> Hi,
>
> I think I would like to clarify a few things first. We keep just talking
> about getters.
> What does this actually mean? Do we talk about any method starting with
> 'get' or
> do we mean getters for properties in the Java Bean sense (aka getters for
> fields with
> the matching name)?
>
> Do you want to exclude any method starting with 'get' from method
> validation or just
> properties getters? If the latter, are we not introducing some quite
> arbitrary distinction
> between methods starting with 'get'?
>
> Also what's about methods starting with 'is'? Wouldn't we have to exclude
> them
> as well in this case?
>
> Also, in the current specification (1.0, 3.1.2. Field and property
> validation) is the intention
> to only validate a getter when it is a Java Bean property (backed up by a
> matching field)?
> I think this is not very clear and not explicitly tested in the TCK.
>

A JavaBeans property IMO doesn't have to be backed by a field. Properties
are identified looking for methods in the form

public <PropertyType> get<PropertyName>();
public void set<PropertyName>(<PropertyType> a);

There may be read-/writable, read-only and write-only properties, For
boolean properties the form

public boolean is<PropertyName>();

is allowed for the read method (see [1] and [2]). There may also be
properties which are computed dynamically.

So AFAICS we would have to exclude all methods adhering to the pattern
<PropertyType>
{get|is}<PropertyName>() from method-validation. As outlined before this
would be kind of irregular.


> On 6 Jan 2012, at 6:53 PM, Emmanuel Bernard wrote:
>
> > If we treat getters as regular methods, we would add a new
> > behavior to all existing constrained beans. Things that were constrained
> > at specific lifecycles boundaries would now be constrained every time a
> > getter is called. That would break backward compatibility.
>
> Backward compatibility is indeed an issue. However, instead of imo
> arbitrarily
> considering a getXYZ not as a method, I would have just not enabled method
> validation
> out of the box. I would have made it an active choice.


Right, in case a user actively enables method validation, there should be
no surprise for the user if getter methods are validated upon invocation.
That's also the approach followed by Seam Validation and Spring 3.1, both
integrating with the proprietary method validation feature of Hibernate
Validator 4.2. I'm also not aware of any issue, bug report in HV etc.
regarding getters being validated.


> Alternatively I would add an option
> to switch between ignoring getters and taking them into consideration for
> method validation.
> However, I don't think  @ValidateOnCall is a good option though. This
> seems to me to
> intrusive. I rather add a configuration option for the ValidatorFactory.
>

A configuration option on VF might indeed be a possibility. I think
validating getter methods should be the default setting :)


>
> > Even if a getter was to be constrained on call - you would probably want
> > to constrain on the setter
>
> What if other method modify the state of the property (not just a setter).
> Maybe the intend
> is to verify that the object is in a certain state when I request it.
>
> > But I do like the simplicity of the rule claiming that all methods
> > are constrained regardless of their similarity to the Bean
> > specification. I do not currently thing that this argument alone
> > outweighs the other problems.
>
> I think it is a argument for consistency. Excluding getter methods from a
> general method validation
> framework seems to introduce unnecessary inconsistency and might even
> exclude the framework
> as general method validation framework of choice.
>

+1


>
> > ### Forcing a getter to behave as a regular method
> >
> > To solve this use case, we can introduce a `@ValidateOnCall`
> > annotation that should be placed on the getter method.
>
> See above.
>

No big fan of an annotation either. Globally configuring this makes more
sense IMO. If really fine-grained control is required, validation groups
may be used.


>
> --Hardy
>

--Gunnar

[1] http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html
[2]
http://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/



>
>
> _______________________________________________
> beanvalidation-dev mailing list
> beanvalidation-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/beanvalidation-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/beanvalidation-dev/attachments/20121107/02b666ee/attachment-0001.html 


More information about the beanvalidation-dev mailing list