Skip to content
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

Make it easier to exclude fields in deep object hierarchy #511

Open
neibarbosa opened this issue Oct 29, 2023 · 0 comments
Open

Make it easier to exclude fields in deep object hierarchy #511

neibarbosa opened this issue Oct 29, 2023 · 0 comments

Comments

@neibarbosa
Copy link

Hello there,

First of all, thanks for awesome work.

I am upgrading easy-random from 3.7.0 to 5.0.0 and I have a scenario where I want to generate a random bean but excluding a field deep down in the object hierarchy. Supposing that I have the structure below:

public class Person {
    private Address address;
    private String name;
    ...
}

public class Address {
    private Street street;
    ...
}

public class Street {
    private String name;
    ...
}

I want to generate a random instance of Person but excluding their street name. In 3.7.0 I could use nextObject(Person.class, "street.name"), reflecting the path of fields using a dot-separated string as excluded field.

RandomTestUtils.nextRandom(Person.class, "name"));

> [name=<null>,address=[street=[name=nHfelv]]]

RandomTestUtils.nextRandom(Person.class, "address.street.name"));

> [name=dumIVTe,address=[street=[name=<null>]]]

In 5.0.0, according to #349, I understand I should implement an ExclusionPolicy.

However it is very hard to implement a policy that complies to my case without having access to the stack of RandomizationContextStackItem and to method getFieldFullName both in RandomizationContext. ExclusionPolicy gives me a handle to a RandomizerContext, not a RandomizationContext.

Is there an easy way to emulate the old behavior using version 5.0.0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant