[hibernate-issues] [Hibernate-JIRA] Created: (HV-409) Provide the possibility to specify parameter names for reporting method level constraint violations

Gunnar Morling (JIRA) noreply at atlassian.com
Sun Jan 9 08:22:05 EST 2011


Provide the possibility to specify parameter names for reporting method level constraint violations
---------------------------------------------------------------------------------------------------

                 Key: HV-409
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-409
             Project: Hibernate Validator
          Issue Type: New Feature
          Components: engine
            Reporter: Gunnar Morling


When reporting a method level constraint violation besides the concerned method the parameter causing the violation is a substantial information. As the Java byte code usually doesn't contain any information on parameter names only the index of the parameter can be returned in the resulting MethodConstraintViolation.

In order to produce expressive constraint violations it is desirable to have the possibility to specify parameter names in some way. One approach for this might be to allow parameter names being specified with the help of an appropriate annotation. Using JSR 330's @Named annotation for instance this could look like this:

{code:java}
someMethod(@Named("param1") @NotNull String param1, @Named("customer") @NotNull @Valid Customer customer) {
    //...
}
{code}

In case any of the method's constraints is violated the resulting MethodConstraintViolation would refer to this name in getName() and getPropertyPath():

{code:java}
assertEquals( constraintViolation.getParameterName(), "customer" );
assertEquals(
    constraintViolation.getPropertyPath().toString(),
    "SomeType#someMethod(customer).name"
);
{code}

This approach was prototyped in https://github.com/gunnarmorling/hibernate-validator/commits/method-level-validation_HV-347 but was removed for HV 4.2 with [e7e3c5f6e97d850b65579fc542599d4c9241048d|https://github.com/gunnarmorling/hibernate-validator/commit/e7e3c5f6e97d850b65579fc542599d4c9241048d] and [8fbc2cca1481563309a03f58ccb4037134772801|https://github.com/gunnarmorling/hibernate-validator/commit/8fbc2cca1481563309a03f58ccb4037134772801] to have time for some more considerations.

In especially an algorithm for retrieving the effective name of a method parameter has to be defined. This might look like this:

* If the validated method doesn't override any method from a super type the parameter names are taken from this method (if given)
* If the validated method does override a method from a super type, the closest names found up in the inheritance tree are taken (e.g. when validating A#foo() overriding B#foo() in turn overriding C#foo() and both B and C provide parameter names for foo(), then the names from B#foo() are taken)
* If the name for a parameter is given in two or more equally distant types, the names of any from the closest types are taken, from which of this is not defined (e.g. when validating A#foo() implementing B#foo() and C#foo() and both B and C specify parameter names for foo(), the names either from B or C are taken) 

When implementing this feature HV should not require new strict dependencies; so if for instance working with @Named, javax.inject-1.jar should be added in optional scope and the type javax.inject.Named should be accessed only using reflection.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list