json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

Wrong default value when used with oneOf, anyOf

Open mpviau opened this issue 4 years ago • 3 comments

Hi,

I have being playing around with the new ApplyDefaultsStrategy, it's a very nice enhancement. In my case, I'd like to have a single property with different default value in each branch of oneOf or anyOf. However, with the current implementation, the result always includes the default value from the schema defined in the first branch, regardless if it's the one actually validated. I know the documentation says the property is updated in place regardless if the schema is valid or not, but this leads to inconsistent results.

I have a few ideas to work around this, but it's getting late. What do you think the best approach would look like?

mpviau avatar Feb 20 '22 06:02 mpviau

I ended up not using the ApplyDefaultsStrategy, but managed to add a "properties" listener that does what I want. Here is what it looks like:

class DefaultValuesWalkListener implements JsonSchemaWalkListener {

        private final Set<String> resetingPaths = new HashSet<>();

        public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMessages)  {
            // do nothing
        }

        public WalkFlow onWalkStart(WalkEvent walkEvent) {
            JsonNode node = walkEvent.getNode();
            if ("properties".equals(walkEvent.getKeywordName()) && node.isObject()) {
                Iterator<Map.Entry<String, JsonNode>> props = walkEvent.getSchemaNode().get("properties").fields();
                while (props.hasNext()) {
                    Map.Entry<String,JsonNode> entry = props.next();
                    String propName = entry.getKey();
                    JsonNode value = node.get(propName);
                    String propPath = walkEvent.getSchemaPath() + "/" + propName;
                    if (resetingPaths.contains(propPath) || value == null) {
                        JsonNode defaultValue = entry.getValue().get("default");
                        if (defaultValue == null) {
                            // no default defined, it might not be the first time we go through this node,
                            // so remove the previously set default just in case
                            ((ObjectNode) node).remove(propName);
                        } else {
                            // set default, either the value is missing, or another default was set previously
                            ((ObjectNode) node).set(propName, defaultValue);
                        }
                        resetingPaths.add(propPath);
                    }
                }
            }
            return WalkFlow.CONTINUE;
        }
}

It is not stateless, so I will likely need to create a new listener for each validation. I don't know if there is a way to put it inside the validation context.

mpviau avatar Feb 20 '22 19:02 mpviau

I myself am not familiar with the walking feature, so I would like other experts to comment on your implementation. However, I think you can try to use the validation context as it is used to make the complex validators stateful. Good luck!

stevehu avatar Feb 22 '22 01:02 stevehu

Close the issue as there is no action. Please open another one if the issue still exists in the latest version.

stevehu avatar May 30 '24 20:05 stevehu