From 32d090ed3ec552ac8f825da25ed3ffd79447ff1a Mon Sep 17 00:00:00 2001 From: Kyle Aure Date: Wed, 25 Mar 2026 13:34:30 -0500 Subject: [PATCH 1/3] test: add tests for malformed resources [URL, URI] --- .../UnmarshallingUnsupportedTypesTest.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/eclipse/yasson/defaultmapping/specific/UnmarshallingUnsupportedTypesTest.java b/src/test/java/org/eclipse/yasson/defaultmapping/specific/UnmarshallingUnsupportedTypesTest.java index 4963b12e9..bda21fa47 100644 --- a/src/test/java/org/eclipse/yasson/defaultmapping/specific/UnmarshallingUnsupportedTypesTest.java +++ b/src/test/java/org/eclipse/yasson/defaultmapping/specific/UnmarshallingUnsupportedTypesTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2026 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -15,6 +15,8 @@ import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; +import java.net.URI; +import java.net.URL; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; @@ -30,6 +32,7 @@ import org.eclipse.yasson.TestTypeToken; import org.eclipse.yasson.defaultmapping.generics.model.GenericTestClass; +import org.eclipse.yasson.defaultmapping.generics.model.ScalarValueWrapper; import org.eclipse.yasson.defaultmapping.specific.model.ClassWithUnsupportedFields; import org.eclipse.yasson.defaultmapping.specific.model.CustomUnsupportedInterface; import org.eclipse.yasson.defaultmapping.specific.model.SupportedTypes; @@ -223,11 +226,23 @@ public void testEmptyStringAsOptionalLong() { Type type = new TestTypeToken>(){}.getType(); assertFail("{\"field1\":\"\"}", type,"field1", Long.class); //We are reusing Long deserializer } + + @Test + public void testMalformedURL() { + Type type = new TestTypeToken>(){}.getType(); + assertFail("{\"value\":\"www.oracle.com\"}", type, "value", URL.class); + } + + @Test + public void testMalformedURI() { + Type type = new TestTypeToken>(){}.getType(); + assertFail("{\"value\":\"www .oracle .com\"}", type, "value", URI.class); + } private void assertFail(String json, Type type, String failureProperty, Class failurePropertyClass) { try { defaultJsonb.fromJson(json, type); - fail(); + fail("Expected to catch JsonbException but did not"); } catch (JsonbException e) { if(!e.getMessage().contains(failureProperty) || !e.getMessage().contains(failurePropertyClass.getName())) { fail("Expected error message to contain '" + failureProperty + "' and '" + failurePropertyClass.getName() + "', but was: " + From f18c562adf1ced07e43b8d3c040765cf4da6bae6 Mon Sep 17 00:00:00 2001 From: Kyle Aure Date: Wed, 25 Mar 2026 13:46:14 -0500 Subject: [PATCH 2/3] fix: handle maformed resources [URL, URI] --- .../internal/deserializer/types/UriDeserializer.java | 12 ++++++++++-- .../internal/deserializer/types/UrlDeserializer.java | 10 +++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/eclipse/yasson/internal/deserializer/types/UriDeserializer.java b/src/main/java/org/eclipse/yasson/internal/deserializer/types/UriDeserializer.java index 26ea0fb8e..51c3fb7cc 100644 --- a/src/main/java/org/eclipse/yasson/internal/deserializer/types/UriDeserializer.java +++ b/src/main/java/org/eclipse/yasson/internal/deserializer/types/UriDeserializer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -14,9 +14,12 @@ import java.lang.reflect.Type; import java.net.URI; +import java.net.URISyntaxException; import org.eclipse.yasson.internal.DeserializationContextImpl; +import jakarta.json.bind.JsonbException; + /** * Deserializer of the {@link URI} type. */ @@ -28,6 +31,11 @@ class UriDeserializer extends TypeDeserializer { @Override Object deserializeStringValue(String value, DeserializationContextImpl context, Type rType) { - return URI.create(value); + try { + return new URI(value); + } catch (URISyntaxException e) { + // Exception will be caught and wrapped + throw new JsonbException("java.net.URI could not parse value " + value, e); + } } } diff --git a/src/main/java/org/eclipse/yasson/internal/deserializer/types/UrlDeserializer.java b/src/main/java/org/eclipse/yasson/internal/deserializer/types/UrlDeserializer.java index 54cd2f346..c73b51801 100644 --- a/src/main/java/org/eclipse/yasson/internal/deserializer/types/UrlDeserializer.java +++ b/src/main/java/org/eclipse/yasson/internal/deserializer/types/UrlDeserializer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -12,6 +12,8 @@ package org.eclipse.yasson.internal.deserializer.types; +import jakarta.json.bind.JsonbException; + import java.lang.reflect.Type; import java.net.MalformedURLException; import java.net.URL; @@ -29,12 +31,10 @@ class UrlDeserializer extends TypeDeserializer { @Override Object deserializeStringValue(String value, DeserializationContextImpl context, Type rType) { - URL url = null; try { - url = new URL(value); + return new URL(value); } catch (MalformedURLException e) { - e.printStackTrace(); + throw new JsonbException("java.net.URL could not parse value " + value, e); } - return url; } } From f9d97db907e3c3267c3db0b75bcf01be4670b936 Mon Sep 17 00:00:00 2001 From: Kyle Aure Date: Wed, 25 Mar 2026 13:48:10 -0500 Subject: [PATCH 3/3] refactor: tabs -> spaces and stylecheck --- .../deserializer/types/UriDeserializer.java | 14 ++++++------ .../deserializer/types/UrlDeserializer.java | 7 +++--- .../UnmarshallingUnsupportedTypesTest.java | 22 +++++++++---------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/eclipse/yasson/internal/deserializer/types/UriDeserializer.java b/src/main/java/org/eclipse/yasson/internal/deserializer/types/UriDeserializer.java index 51c3fb7cc..d7b1b8702 100644 --- a/src/main/java/org/eclipse/yasson/internal/deserializer/types/UriDeserializer.java +++ b/src/main/java/org/eclipse/yasson/internal/deserializer/types/UriDeserializer.java @@ -16,10 +16,10 @@ import java.net.URI; import java.net.URISyntaxException; -import org.eclipse.yasson.internal.DeserializationContextImpl; - import jakarta.json.bind.JsonbException; +import org.eclipse.yasson.internal.DeserializationContextImpl; + /** * Deserializer of the {@link URI} type. */ @@ -32,10 +32,10 @@ class UriDeserializer extends TypeDeserializer { @Override Object deserializeStringValue(String value, DeserializationContextImpl context, Type rType) { try { - return new URI(value); - } catch (URISyntaxException e) { - // Exception will be caught and wrapped - throw new JsonbException("java.net.URI could not parse value " + value, e); - } + return new URI(value); + } catch (URISyntaxException e) { + // Exception will be caught and wrapped + throw new JsonbException("java.net.URI could not parse value " + value, e); + } } } diff --git a/src/main/java/org/eclipse/yasson/internal/deserializer/types/UrlDeserializer.java b/src/main/java/org/eclipse/yasson/internal/deserializer/types/UrlDeserializer.java index c73b51801..dbe79806b 100644 --- a/src/main/java/org/eclipse/yasson/internal/deserializer/types/UrlDeserializer.java +++ b/src/main/java/org/eclipse/yasson/internal/deserializer/types/UrlDeserializer.java @@ -12,12 +12,12 @@ package org.eclipse.yasson.internal.deserializer.types; -import jakarta.json.bind.JsonbException; - import java.lang.reflect.Type; import java.net.MalformedURLException; import java.net.URL; +import jakarta.json.bind.JsonbException; + import org.eclipse.yasson.internal.DeserializationContextImpl; /** @@ -34,7 +34,8 @@ Object deserializeStringValue(String value, DeserializationContextImpl context, try { return new URL(value); } catch (MalformedURLException e) { - throw new JsonbException("java.net.URL could not parse value " + value, e); + // Exception will be caught and wrapped + throw new JsonbException("java.net.URL could not parse value " + value, e); } } } diff --git a/src/test/java/org/eclipse/yasson/defaultmapping/specific/UnmarshallingUnsupportedTypesTest.java b/src/test/java/org/eclipse/yasson/defaultmapping/specific/UnmarshallingUnsupportedTypesTest.java index bda21fa47..c9f5991be 100644 --- a/src/test/java/org/eclipse/yasson/defaultmapping/specific/UnmarshallingUnsupportedTypesTest.java +++ b/src/test/java/org/eclipse/yasson/defaultmapping/specific/UnmarshallingUnsupportedTypesTest.java @@ -72,7 +72,7 @@ public void setValue(String value) { String expected = "{\"customInterface\":{\"value\":\"value1\"}}"; assertEquals(expected, defaultJsonb.toJson(unsupported)); try { - defaultJsonb.fromJson(expected, ClassWithUnsupportedFields.class); + defaultJsonb.fromJson(expected, ClassWithUnsupportedFields.class); fail("Should report an error"); } catch (JsonbException e) { assertTrue(e.getMessage().contains("Cannot infer a type")); @@ -136,11 +136,11 @@ public void testMissingFieldDefaultNull() { @Test public void testMissingFieldIgnored() { - assertThrows(JsonbException.class, () -> { - Jsonb defaultConfig = JsonbBuilder.create(new JsonbConfig().setProperty(FAIL_ON_UNKNOWN_PROPERTIES, true)); - String json = "{\"nestedPojo\":{\"integerValue\":10,\"missingField\":5},\"optionalLong\":11}"; - SupportedTypes result = defaultConfig.fromJson(json, SupportedTypes.class); - }); + assertThrows(JsonbException.class, () -> { + Jsonb defaultConfig = JsonbBuilder.create(new JsonbConfig().setProperty(FAIL_ON_UNKNOWN_PROPERTIES, true)); + String json = "{\"nestedPojo\":{\"integerValue\":10,\"missingField\":5},\"optionalLong\":11}"; + SupportedTypes result = defaultConfig.fromJson(json, SupportedTypes.class); + }); } @Test @@ -229,19 +229,19 @@ public void testEmptyStringAsOptionalLong() { @Test public void testMalformedURL() { - Type type = new TestTypeToken>(){}.getType(); - assertFail("{\"value\":\"www.oracle.com\"}", type, "value", URL.class); + Type type = new TestTypeToken>(){}.getType(); + assertFail("{\"value\":\"www.oracle.com\"}", type, "value", URL.class); } @Test public void testMalformedURI() { - Type type = new TestTypeToken>(){}.getType(); - assertFail("{\"value\":\"www .oracle .com\"}", type, "value", URI.class); + Type type = new TestTypeToken>(){}.getType(); + assertFail("{\"value\":\"www .oracle .com\"}", type, "value", URI.class); } private void assertFail(String json, Type type, String failureProperty, Class failurePropertyClass) { try { - defaultJsonb.fromJson(json, type); + defaultJsonb.fromJson(json, type); fail("Expected to catch JsonbException but did not"); } catch (JsonbException e) { if(!e.getMessage().contains(failureProperty) || !e.getMessage().contains(failurePropertyClass.getName())) {