diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java index b24f98e36a..b867f7db0d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,12 +39,12 @@ public class BasicJsonParser extends AbstractJsonParser { @Override public Map parseMap(String json) { - return parseMap(json, this::parseMapInternal); + return tryParse(() -> parseMap(json, this::parseMapInternal), Exception.class); } @Override public List parseList(String json) { - return parseList(json, this::parseListInternal); + return tryParse(() -> parseList(json, this::parseListInternal), Exception.class); } private List parseListInternal(String json) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java index 9a20ede18f..497eadb4f5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,12 +41,14 @@ public class GsonJsonParser extends AbstractJsonParser { @Override public Map parseMap(String json) { - return parseMap(json, (trimmed) -> this.gson.fromJson(trimmed, MAP_TYPE.getType())); + return tryParse(() -> parseMap(json, (trimmed) -> this.gson.fromJson(trimmed, MAP_TYPE.getType())), + Exception.class); } @Override public List parseList(String json) { - return parseList(json, (trimmed) -> this.gson.fromJson(trimmed, LIST_TYPE.getType())); + return tryParse(() -> parseList(json, (trimmed) -> this.gson.fromJson(trimmed, LIST_TYPE.getType())), + Exception.class); } private static final class MapTypeToken extends TypeToken> { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java index 8878ca388d..6ff6f1d64f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -175,4 +175,15 @@ abstract class AbstractJsonParserTests { assertThat(map.get("foo")).isEqualTo("\"bar\""); } + @Test + void listWithMalformedMap() { + assertThatExceptionOfType(JsonParseException.class) + .isThrownBy(() -> this.parser.parseList("[tru,erqett,{\"foo\":fatrue,true,true,true,tr''ue}]")); + } + + @Test + void mapWithKeyAndNoValue() { + assertThatExceptionOfType(JsonParseException.class).isThrownBy(() -> this.parser.parseMap("{\"foo\"}")); + } + } 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 e597ffb276..7df627fa0a 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.boot.json; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.yaml.snakeyaml.constructor.ConstructorException; @@ -40,4 +41,16 @@ class YamlJsonParserTests extends AbstractJsonParserTests { .withCauseInstanceOf(IllegalStateException.class); } + @Test + @Override + @Disabled("SnakeYaml does not fail when a map is malformed") + void listWithMalformedMap() { + } + + @Test + @Override + @Disabled("SnakeYaml does not fail when a map has a key with no value") + void mapWithKeyAndNoValue() { + } + }