From 522ea0a90e369549a75ff035771587c0a971bad6 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 9 Jun 2022 07:22:48 +0100 Subject: [PATCH] Handle malformed JSON more consistently Closes gh-31301 --- .../boot/json/BasicJsonParser.java | 6 +++--- .../springframework/boot/json/GsonJsonParser.java | 8 +++++--- .../boot/json/AbstractJsonParserTests.java | 13 ++++++++++++- .../boot/json/YamlJsonParserTests.java | 15 ++++++++++++++- 4 files changed, 34 insertions(+), 8 deletions(-) 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 b24f98e36a0e..b867f7db0d7b 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 9a20ede18f22..497eadb4f560 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 8878ca388d18..6ff6f1d64fd7 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 @@ void escapeDoubleQuote() { 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 e597ffb276ba..7df627fa0a77 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 @@ void customTypesAreNotLoaded() { .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() { + } + }