-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Past/Future annotation cannot be applied to java8 date types #328
Comments
Another option to solve this could be to use https://github.com/benas/random-beans/blob/master/random-beans/src/main/java/io/github/benas/randombeans/randomizers/registry/TimeRandomizerRegistry.java to look up the Java 8 date type. |
It is not possible to "combine" criteria of two randomizer registries today. In the given example, these two criteria are "Custom date type" (coming from
I won't do it that way because we will need to check all types supported by the annotation with multiple if/then statement. I'm not sure (yet) if this is a design "bug" or a feature request, so I planned it for the next major version v4 to take some time and think how to correctly tackle the case. In the meantime, it is always possible to use a custom randomizer for the field and leverage the import io.github.benas.randombeans.EnhancedRandomBuilder;
import io.github.benas.randombeans.FieldDefinitionBuilder;
import io.github.benas.randombeans.api.EnhancedRandom;
import io.github.benas.randombeans.api.Randomizer;
import io.github.benas.randombeans.randomizers.range.LocalDateRangeRandomizer;
import javax.validation.constraints.Past;
import java.time.LocalDate;
public class Foo {
@Past
private LocalDate birthDate;
public static void main(String[] args) {
EnhancedRandom enhancedRandom = new EnhancedRandomBuilder()
.randomize(FieldDefinitionBuilder.field().named("birthDate").ofType(LocalDate.class).inClass(Foo.class).get(), new Randomizer<LocalDate>() {
LocalDateRangeRandomizer localDateRangeRandomizer = new LocalDateRangeRandomizer(LocalDate.now().minusYears(10), LocalDate.now());
@Override
public LocalDate getRandomValue() {
return localDateRangeRandomizer.getRandomValue();
}
})
.build();
Foo foo = enhancedRandom.nextObject(Foo.class);
System.out.println(foo.birthDate);
}
} |
@PascalSchumacher Thanks! Sorry for misunderstanding, I thought you mean using |
@neetkee A fix for this issue has been deployed in version Many thanks to @PascalSchumacher for the fix! |
If we will put Past annotation on java 8 date type:
we will get the exception:
The problem is in PastAnnotationHandler, I guess. It returns DateRangeRandomizer without respecting the type. Should we check for type in this class, something like this?
Or there is a better way to do this?
The text was updated successfully, but these errors were encountered: