mtedone / podam

PODAM - POjo DAta Mocker
https://mtedone.github.io/podam
MIT License
323 stars 750 forks source link

Fields annotated with @Email generated as null #316

Closed sashasch closed 1 year ago

sashasch commented 1 year ago

For example given Pojo:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable;
import javax.validation.constraints.Email;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@SuperBuilder(toBuilder = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class EmailContact implements Serializable {

    @Email
    private String emailAddress;

    private String displayName;
}

Trying to generate this:

PodamFactory factory = new PodamFactoryImpl();
 factory.manufacturePojoWithFullData(EmailContact.class);

result: EmailContact(emailAddress=null, displayName=SHWUlQFAPF)

Tried customizing the field generation using AttributeStrategy...

factory.getStrategy()
                .addOrReplaceAttributeStrategy(
                        EmailContact.class,
                        "emailAddress",
                        (AttributeStrategy<String>) (attrType, attrAnnotations) -> "dummy@mail.com");

, but the result was the same:

daivanov commented 1 year ago

Hi,

You use lombok and did not specify @Setter and @Getter, so Podam will ignore the property. So you either should use these annotations or make a custom ClassInfoStrategy, which will allow properties without accessors. Note, if you do the latter it for all all classes this will likely had bad side effects.

Thanks, Daniil

sashasch commented 1 year ago

Hi Daniil, First of all let me thank you for quick answer and your library doing great job overall. Unfortunately your answer in not correct. I'm using @Data annotation and it includes the both @Setter and @Getter inside. In addition, you could pay attention that the field "displayName" of the same pojo was generated normally. I have already created workaround by generating the field manually and setting it on Pojo after the library generates it, but it looks worse than if the code was working as expected.

Regards from Alex.

daivanov commented 1 year ago

Hi,

Ok, I'm sorry, I overlooked couple of things here. You have @Email annotation, which we do not support and it would be filled with null. You have to override it with calling addOrReplaceAttributeStrategy(Class<? extends Annotation>, AttributeStrategy<?>), but instead you call addOrReplaceAttributeStrategy(Class<?>, String, AttributeStrategy<?>), which won't override original mapping.

So it should be like this:

factory.getStrategy()
            .addOrReplaceAttributeStrategy(
                    Email.class,
                    (AttributeStrategy<String>) (attrType, attrAnnotations) -> "dummy@mail.com");

Thanks, Daniil