podam icon indicating copy to clipboard operation
podam copied to clipboard

PODAM does not populate fields when constraint annotations not from javax.validation.constraints package present

Open ignaciolarranaga opened this issue 10 years ago • 15 comments

For example, a POJO with an string an a validation NotEmpty is not populated. A short field with a @Min annotation is neither populated.

ignaciolarranaga avatar Mar 31 '16 14:03 ignaciolarranaga

Podam does not honour all Hibernate validation annotations. I'm flagging this for future consideration unless, of course, you want to implement the remaining changes.

mtedone avatar Mar 31 '16 15:03 mtedone

org.hibernate.validator.constraints.NotEmpty is indeed not supported, but javax.validation.constraints.Min is supported for short type.

daivanov avatar Jul 03 '16 12:07 daivanov

In general Podam cannot support all imaginable validation annotations. If you face such need you always can extend DataProviderStrategy with adding custom AttributeStrategy implementing what you need.

daivanov avatar Aug 25 '16 13:08 daivanov

Hello, I am facing the same problem. What I think @ignaciolarranaga was saying is that every time podam find a org.hibernate.validator.constraints annotation (or maybe any other type of annotation) it just ignore the attribute. So, for example, the following class

public class Test {

    @PodamStringValue(length = 3)
    @Length(min = 3, max = 3)
    String varA;

    @PodamStringValue(length = 3)
    String varB;
}

Will result in generating an object with only varB populated. varA will be ignored I think because the Hibernate annotation clash

maxdidato avatar Jan 11 '17 17:01 maxdidato

Can you use javax.validation.constraints.Size instead of I guess org.hibernate.validator.constraints.Length?

daivanov avatar Jan 11 '17 17:01 daivanov

Hello. I think I could try but what I would like to understand is this part of code

if (annotation.annotationType().getAnnotation(Constraint.class) != null) {
    if (annotation instanceof NotNull ||
                    annotation.annotationType().getName().equals("org.hibernate.validator.constraints.NotEmpty")) {
            /* We don't need to do anything for NotNull constraint */
            iter.remove();
    } else if (!NotNull.class.getPackage().equals(annotationClass.getPackage())) {
        LOG.warn("Please, register AttributeStratergy for custom "
                + "constraint {}, in DataProviderStrategy! Value "
                + "will be left to null", annotation);
    }
} else {
    iter.remove();
}

It seems that any Constraint annotation used on attribute level will force the user to provide a specific AttributeStrategy or the attribute itself would be left empty. Is there a reason for that? As podam doesn't support any validation annotation (apart for Email as I could see), would not make sense removing the else if branch completely?

I was preparing a pull request with a failing test and I could easily fix the issue removing the else if branch. But I could not figure out the reason for it

maxdidato avatar Jan 11 '17 18:01 maxdidato

Hi!

Podam supports all constraint annotations from package javax.validation.constraints with notable exception of a javax.validation.constraints.Pattern. Also, as you can see from the snippet, Podam supports org.hibernate.validator.constraints.NotEmpty, which appears to be Podam's default strategy.

What Podam does not support, is custom Hibernate constraints from package org.hibernate.validator.constraints. Including org.hibernate.validator.constraints.Email.

So, if you want to limit size of a string, you have two options:

  1. Use javax.validation.constraints.Size
  2. Create custom AttributeStrategy for org.hibernate.validator.constraints.Length

What is valid here is that Podam selects only one constraint, if multiple constraints are attached to a field. Could you please file a bug for this or I will do it later myself.

daivanov avatar Jan 11 '17 20:01 daivanov

Thanks for the explanation. In my case my pojo contains custom Hibernate validation tags but I don't want them to be considered by podam. That is why on top of the Hibernate annotation I have added a podam annotation, to instruct the generator on how I want my field populated. Since Hibernate validations are not supported why don't you ignore them completely Rather than applying the BeanValidation strategy?

maxdidato avatar Jan 11 '17 20:01 maxdidato

The reason for this is that org.hibernate.validator.HibernateValidator or whatever non JavaEE validator will report errors for default stuff injected by Podam, but null values are fine, unless there is also NotNull/NotEmpty annotation.

daivanov avatar Jan 11 '17 20:01 daivanov

I understand. Thanks for making it clear On Wed, 11 Jan 2017 at 20:22, Daniil Ivanov [email protected] wrote:

The reason for this is that org.hibernate.validator.HibernateValidator or whatever non JavaEE validator will report errors for default stuff injected by Podam, but null values are fine, unless there is also NotNull/NotEmpty annotation.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/devopsfolks/podam/issues/189#issuecomment-271982894, or mute the thread https://github.com/notifications/unsubscribe-auth/ACwgWVH8Cr7supg-SyOk4YX57Cd-JCwlks5rRTnmgaJpZM4H81d8 .

maxdidato avatar Jan 11 '17 20:01 maxdidato

In general this is rather controversial topic as there are people, who do not want hibernate-validator as Podam dependency and there are people, who want Hibernate constraint annotations support.

daivanov avatar Jan 11 '17 20:01 daivanov

In order to raise an error during the generation of random values,Podam should trigger the validation engine of Hibernate, is that right? On Wed, 11 Jan 2017 at 20:29, Daniil Ivanov [email protected] wrote:

In general this is rather controversial topic as there are people, who do not want hibernate-validator as Podam dependency and there are people, who want Hibernate constraint annotations support.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/devopsfolks/podam/issues/189#issuecomment-271984924, or mute the thread https://github.com/notifications/unsubscribe-auth/ACwgWZPiwLMkogeUGrkTTeXwD_ht0Fspks5rRTu9gaJpZM4H81d8 .

maxdidato avatar Jan 11 '17 20:01 maxdidato

Something will call the validation engine, not Podam. Please, file an issue regarding multiple annotations on the same field.

daivanov avatar Jan 11 '17 21:01 daivanov

I re-used Issue #205 to report this bug. Do you want to make a PR fixing it or should I do it?

daivanov avatar Jan 12 '17 07:01 daivanov

I can do it if you want On Thu, 12 Jan 2017 at 07:50, Daniil Ivanov [email protected] wrote:

I re-used Issue #205 https://github.com/devopsfolks/podam/issues/205 to report this bug. Do you want to make a PR fixing it or should I do it?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/devopsfolks/podam/issues/189#issuecomment-272098666, or mute the thread https://github.com/notifications/unsubscribe-auth/ACwgWY-kEaZ2uP-ZiOTWaOyhASFIUb4Oks5rRdswgaJpZM4H81d8 .

maxdidato avatar Jan 12 '17 10:01 maxdidato