From b5fe3c3494ac921c0368bc48ed197bfa15d3a94e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 2 Jun 2020 22:46:47 -0700 Subject: [PATCH] Refine YAML type restriction error Closes gh-21596 --- .../java/org/springframework/boot/json/YamlJsonParser.java | 6 +++++- .../org/springframework/boot/json/YamlJsonParserTests.java | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/YamlJsonParser.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/YamlJsonParser.java index afec8622d9f..e25dbd2d4bb 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/YamlJsonParser.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/YamlJsonParser.java @@ -26,6 +26,8 @@ import java.util.stream.Collectors; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; +import org.springframework.util.Assert; + /** * Thin wrapper to adapt Snake {@link Yaml} to {@link JsonParser}. * @@ -63,7 +65,9 @@ public class YamlJsonParser extends AbstractJsonParser { @Override protected Class getClassForName(String name) throws ClassNotFoundException { - return (SUPPORTED_TYPES.contains(name)) ? super.getClassForName(name) : null; + Assert.state(SUPPORTED_TYPES.contains(name), + () -> "Unsupported '" + name + "' type encountered in YAML document"); + return super.getClassForName(name); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/YamlJsonParserTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/YamlJsonParserTests.java index c1da2c59e2b..a9046a744bd 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/YamlJsonParserTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/YamlJsonParserTests.java @@ -36,7 +36,8 @@ public class YamlJsonParserTests extends AbstractJsonParserTests { @Test public void customTypesAreNotLoaded() throws Exception { assertThatExceptionOfType(ConstructorException.class) - .isThrownBy(() -> getParser().parseMap("{value: !!java.net.URL [\"http://localhost:9000/\"]}")); + .isThrownBy(() -> getParser().parseMap("{value: !!java.net.URL [\"http://localhost:9000/\"]}")) + .withCauseInstanceOf(IllegalStateException.class); } }