|
4 | 4 |
|
5 | 5 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
6 | 6 |
|
| 7 | +import tools.jackson.core.JacksonException; |
7 | 8 | import tools.jackson.core.JsonParser; |
8 | 9 | import tools.jackson.databind.*; |
9 | 10 | import tools.jackson.databind.annotation.JsonDeserialize; |
@@ -99,6 +100,95 @@ public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, |
99 | 100 | assertEquals(1, exception.getPath().size()); |
100 | 101 | } |
101 | 102 |
|
| 103 | + @Test |
| 104 | + void jacksonExceptionWithoutPathGetsUnknownPropertyPath() { |
| 105 | + for (boolean wrap : new boolean[] { true, false }) { |
| 106 | + DatabindException[] original = new DatabindException[1]; |
| 107 | + Object[] bean = new Object[1]; |
| 108 | + ObjectMapper jacksonFailure = JsonMapper.builder() |
| 109 | + .configure(DeserializationFeature.WRAP_EXCEPTIONS, wrap) |
| 110 | + .addHandler(new DeserializationProblemHandler() { |
| 111 | + @Override |
| 112 | + public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, |
| 113 | + ValueDeserializer<?> deser, Object beanOrClass, String propName) { |
| 114 | + bean[0] = beanOrClass; |
| 115 | + original[0] = DatabindException.from(p, "handler failure"); |
| 116 | + throw original[0]; |
| 117 | + } |
| 118 | + }).build(); |
| 119 | + DatabindException exception = assertThrows(DatabindException.class, |
| 120 | + () -> jacksonFailure.readValue("{\"unknown\":1}", Bean.class)); |
| 121 | + assertSame(original[0], exception); |
| 122 | + assertEquals(1, exception.getPath().size()); |
| 123 | + assertSame(bean[0], exception.getPath().get(0).from()); |
| 124 | + assertEquals("unknown", exception.getPath().get(0).getPropertyName()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void inputMismatchGetsUnknownPropertyPath() { |
| 130 | + ObjectMapper jacksonFailure = JsonMapper.builder() |
| 131 | + .addHandler(new DeserializationProblemHandler() { |
| 132 | + @Override |
| 133 | + public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, |
| 134 | + ValueDeserializer<?> deser, Object beanOrClass, String propName) { |
| 135 | + return ctxt.reportInputMismatch(Bean.class, "handler failure"); |
| 136 | + } |
| 137 | + }).build(); |
| 138 | + DatabindException exception = assertThrows(DatabindException.class, |
| 139 | + () -> jacksonFailure.readValue("{\"unknown\":1}", Bean.class)); |
| 140 | + assertEquals(1, exception.getPath().size()); |
| 141 | + assertEquals("unknown", exception.getPath().get(0).getPropertyName()); |
| 142 | + assertInstanceOf(Bean.class, exception.getPath().get(0).from()); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void nestedReadFailureGetsUnknownPropertyPrefix() { |
| 147 | + JacksonException[] original = new JacksonException[1]; |
| 148 | + ObjectMapper jacksonFailure = JsonMapper.builder() |
| 149 | + .addHandler(new DeserializationProblemHandler() { |
| 150 | + @Override |
| 151 | + public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, |
| 152 | + ValueDeserializer<?> deser, Object beanOrClass, String propName) { |
| 153 | + try { |
| 154 | + ctxt.readValue(p, Bean.class); |
| 155 | + } catch (JacksonException e) { |
| 156 | + original[0] = e; |
| 157 | + throw e; |
| 158 | + } |
| 159 | + return true; |
| 160 | + } |
| 161 | + }).build(); |
| 162 | + DatabindException exception = assertThrows(DatabindException.class, |
| 163 | + () -> jacksonFailure.readValue("{\"unknown\":{\"value\":\"invalid\"}}", Bean.class)); |
| 164 | + assertSame(original[0], exception); |
| 165 | + assertEquals(2, exception.getPath().size()); |
| 166 | + assertEquals("unknown", exception.getPath().get(0).getPropertyName()); |
| 167 | + assertEquals("value", exception.getPath().get(1).getPropertyName()); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void existingUnknownPropertyPrefixIsNotDuplicated() { |
| 172 | + DatabindException[] original = new DatabindException[1]; |
| 173 | + ObjectMapper jacksonFailure = JsonMapper.builder() |
| 174 | + .addHandler(new DeserializationProblemHandler() { |
| 175 | + @Override |
| 176 | + public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, |
| 177 | + ValueDeserializer<?> deser, Object beanOrClass, String propName) { |
| 178 | + original[0] = DatabindException.from(p, "handler failure"); |
| 179 | + original[0].prependPath(Bean.class, "value"); |
| 180 | + original[0].prependPath(beanOrClass, propName); |
| 181 | + throw original[0]; |
| 182 | + } |
| 183 | + }).build(); |
| 184 | + DatabindException exception = assertThrows(DatabindException.class, |
| 185 | + () -> jacksonFailure.readValue("{\"unknown\":1}", Bean.class)); |
| 186 | + assertSame(original[0], exception); |
| 187 | + assertEquals(2, exception.getPath().size()); |
| 188 | + assertEquals("unknown", exception.getPath().get(0).getPropertyName()); |
| 189 | + assertEquals("value", exception.getPath().get(1).getPropertyName()); |
| 190 | + } |
| 191 | + |
102 | 192 | @Test |
103 | 193 | void defaultUnknownPropertyHasOnePathSegment() { |
104 | 194 | ObjectMapper strict = JsonMapper.builder() |
|
0 commit comments