eclipse-ee4j / yasson

Eclipse Yasson project
https://projects.eclipse.org/projects/ee4j.yasson
Other
204 stars 96 forks source link

Input ignored after unbound key #563

Closed feefifo closed 2 years ago

feefifo commented 2 years ago

When a key with an object for a value is not recognized, the remainder of the input is ignored.

There are a couple of reasons I believe the must-ignore policy does not apply here, and that this is a bug.

When JSON Binding implementation during deserialization encounters key in key/value pair that it does not recognize, it should treat the rest of the JSON document as if the element simply did not appear, and in particular, the implementation MUST NOT treat this as an error condition.

  1. The specification says that the rest of the document should be treated as if the element did not appear. My interpretation is that only that key-value is to be ignored, otherwise the specification would say something like, "treat the rest of the JSON document as if it simply did not appear."
  2. Remaining input is not ignored unless the unrecognized key has an object value. Strings, numbers, etc. do not trigger this problem. This suggests that the behavior is accidental rather than a deliberate interpretation applying the must-ignore policy to "the rest of the document."
  3. This behavior violently violates the principle of least surprise.

Here is a test:

import java.util.Map;

import jakarta.json.bind.JsonbBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class JsonbTest {

    private static final String EXAMPLE = "{\"obj\":{\"foo\":\"bar\"},\"key\":\"val\"}";

    public static class Fail {
        public String key;
    }

    public static class Pass extends Fail {
        public Map<String, String> obj;
    }

    @ParameterizedTest
    @ValueSource(classes = { Pass.class, Fail.class })
    void bindWithMissingProperties(Class<? extends Fail> clz) {
        Fail fail = JsonbBuilder.create().fromJson(EXAMPLE, clz);
        Assertions.assertEquals("val", fail.key);
    }

}
Verdent commented 2 years ago

Hi, thank you for reaching out. This is definitely a bug. I will make a fix for this.