Skip to content

CollectorContext is empty in walk return value #849

@timocov

Description

@timocov

Having isResetCollectorContext=true is a good way to keep collector context "pure" between runs. But if you need to get collector context, the only option you have is to set isResetCollectorContext to false and then reset the context manually after you're done. It is so because of this code - while the library copies collector context into ValidationResult, it keeps its reference and when execution reaches finally block it will reset the context in both returned value and actual "current" state. CollectorContext creates new objects on reset, but as soon as we're keeping the reference to the same CollectorContext object in ValidationResult it will be reset everywhere.

I don't know if it was done intentionally, but it looks like a bug for me and probably ValidationResult should have a copy of CollectorContext. Otherwise you have to do something like this:

try {
    ValidationResult result = schema.walk(object, true);
    // do something here
} finally {
    schema.getCollectorContext().reset();
}

i.e. sort of replicate the code that the library already has. Additionally, you can easily forget to reset it in some places (if you do multiple operations with the schema in different functions, and isResetCollectorContext is assigned at the schema creation so you have to either use multiple schemas that are different in this flag only or somehow make sure that you always reset the context).

Metadata

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