-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Validator cant get parameter name on method of an interface
PostPosted: Tue Jan 20, 2015 5:38 am 
Newbie

Joined: Tue Jan 20, 2015 5:28 am
Posts: 5
Hello ,

I'm using Hibernate validator 5.1.3 , and when I want to validate a method , I cant get the parameter name ( but i have arg0 , arg1 , etc instead) to build the error validation messages. The problem is due to the fact that the method annotation must be on an interface , but the ParameterNameDiscover is trying to get the parameter name form the debug information (but there is no parameter name information in interface compiled class , only in the implementation ).
I've tried to move the validation annotation on the implementated class ( because the parameter name are in this compiled class) but I have an error from hibernate validator :
Quote:
A method overriding another method must not alter the parameter constraint configuration


here is my code (with annotation on interface) :

Interface :
Code:
@Validated
public interface NomenclatureVenteRest {
   
    public Response recupererArborescenceNomenclatureVente(
   @FieldValidation(required="dateAppli") StimeDate dateAppli,
   @FieldValidation(check = GENE_M010_PAYS) String codePays,
   @FieldValidation(check = GENE_M010_ENSEIGNE) List<String> listeCodeEns) throws StimeFunctionalException;
}

Implementation :
Code:
@Path("/")
@Service(value = "nomenclatureVenteRestImpl")
public class NomenclatureVenteRestImpl implements NomenclatureVenteRest {
   
    @GET
    @Path("/")
    @Produces(RestUtils.UTF8_ENCODED_JSON)
    @Override
    public Response recupererArborescenceNomenclatureVente(
      @QueryParam("dateAppli") StimeDate dateAppli,
      @QueryParam("codePays")String codePays,
      @QueryParam("listeCodeEns")List<String> listeCodeEns) throws StimeFunctionalException{
   
      ...Business logic...
    }
}


Quote:
@FieldValidation is my custom constraint validation


If I put annotation validation on both interface and implementation , I don't have the error message about the overriding check rule , but the validation is applied on the interface , so I can't get the parameters names.

Please help me :-)

Regards ,

RĂ©gis LIMARE


Top
 Profile  
 
 Post subject: Re: Validator cant get parameter name on method of an interface
PostPosted: Wed Jan 21, 2015 3:52 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

Hibernate Validator uses the ParameterNameProvider contract to resolve parameter names (see https://docs.jboss.org/hibernate/validator/5.2/reference/en-US/html_single/#section-parameter-name-provider). In HV 5.2 there will be an implementation based on Java 8 parameter names (it's part of the Alpha release already). You might give that a try, this should also work for interfaces.

You mentioned "ParameterNameDiscover", is this a custom ParameterNameProvider implementation?

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validator cant get parameter name on method of an interface
PostPosted: Wed Jan 21, 2015 5:36 am 
Newbie

Joined: Tue Jan 20, 2015 5:28 am
Posts: 5
ParameterNameDiscover is the spring mechanism used to find the parameters names. I ve seen the new funtionnality of the 5.2 release but I m working on websphere 8.5 , so I m stucked with jdk 1.6 , 1.7.
In the meanwhile I will add field in my custom contraint annotation to set the parameter name.

The problem is just before the call to the spring parameter name provider

in org.hibernate.validator.internal.engine.ValidationContext

Code:
public List<String> getParameterNames() {
      if ( parameterNameProvider == null ) {
         return null;
      }
      else if ( executable.getElementType() == ElementType.METHOD ) {
         return parameterNameProvider.getParameterNames( (Method) executable.getMember() );
      }
      else {
         return parameterNameProvider.getParameterNames( (Constructor<?>) executable.getMember() );
      }
   }


the code (Method) executable.getMember() retrieves the method of the interface. It is to note that the implementation of the interface is in the ValidationContext.rootBean so it should be possible to pass the method of the implementation instead of the method of the interface. Or it should be possible to give the possiblity to access the validationContext from our validator ?

Sorry for my english


Top
 Profile  
 
 Post subject: Re: Validator cant get parameter name on method of an interface
PostPosted: Wed Jan 21, 2015 6:24 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

I don't think we can get a handle to the implementation method at this point.

Really the question is how to get method parameter names for interface methods. Have you looked into the ParaNamer library? IIRC, they provide a way where at build time the names of all parameters are injected into a constant within the class. This constant then is queried at runtime to obtain parameter names. Then you could provide an implementation of Spring's ParameterNameDiscoverer using that ParaNamer strategy. Or you could provide a ParameterNameDiscoverer which e.g. uses values given via annotations (@Name("myParam") String myParam).

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validator cant get parameter name on method of an interface
PostPosted: Wed Jan 21, 2015 10:51 am 
Newbie

Joined: Tue Jan 20, 2015 5:28 am
Posts: 5
I 've tried to extends ParameterNameDiscoverer of spring , but it's not only used by my validation . I have modified my custom annotation constraint to add the name of the checked parameter.
I'm not sure there is an other way , because when you compile (in debug ) an interface , the compiler don't add the name of the parameters of the methods , I can only find the information in the implementation.


Top
 Profile  
 
 Post subject: Re: Validator cant get parameter name on method of an interface
PostPosted: Thu Jan 22, 2015 3:46 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Have you looked into ParaNamer's (https://github.com/paul-hammant/paranamer) DefaultParanamer and AnnotationParanamer? Both provide solutions which don't rely on debug symbols.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validator cant get parameter name on method of an interface
PostPosted: Thu Jan 22, 2015 8:48 am 
Newbie

Joined: Tue Jan 20, 2015 5:28 am
Posts: 5
I will try it , I just need to find how to configure a provider via spring


Top
 Profile  
 
 Post subject: Re: Validator cant get parameter name on method of an interface
PostPosted: Thu Jan 22, 2015 8:53 am 
Newbie

Joined: Tue Jan 20, 2015 5:28 am
Posts: 5
I 've checked the documentation and I need to add additionnal information like annotation with name of the param , or java doc , or other things. I will keep my work around (I have added the name in my custom constraint annotation)

Code:
@Target({ PARAMETER, ANNOTATION_TYPE ,FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldValidator.class)
@Documented
public @interface FieldValidation {

    String message() default "";

    Class<?>[] groups() default {};

    boolean required() default false;

    String name();

    FieldTypeValidator[] check() default {};

    Class<? extends Payload>[] payload() default {};
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.