Skip to content

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

Open
@neibarbosa

Description

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?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions