Skip to content

Commit 522ea0a

Browse files
committed
Handle malformed JSON more consistently
Closes gh-31301
1 parent 79d3e30 commit 522ea0a

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,12 +39,12 @@ public class BasicJsonParser extends AbstractJsonParser {
3939

4040
@Override
4141
public Map<String, Object> parseMap(String json) {
42-
return parseMap(json, this::parseMapInternal);
42+
return tryParse(() -> parseMap(json, this::parseMapInternal), Exception.class);
4343
}
4444

4545
@Override
4646
public List<Object> parseList(String json) {
47-
return parseList(json, this::parseListInternal);
47+
return tryParse(() -> parseList(json, this::parseListInternal), Exception.class);
4848
}
4949

5050
private List<Object> parseListInternal(String json) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,12 +41,14 @@ public class GsonJsonParser extends AbstractJsonParser {
4141

4242
@Override
4343
public Map<String, Object> parseMap(String json) {
44-
return parseMap(json, (trimmed) -> this.gson.fromJson(trimmed, MAP_TYPE.getType()));
44+
return tryParse(() -> parseMap(json, (trimmed) -> this.gson.fromJson(trimmed, MAP_TYPE.getType())),
45+
Exception.class);
4546
}
4647

4748
@Override
4849
public List<Object> parseList(String json) {
49-
return parseList(json, (trimmed) -> this.gson.fromJson(trimmed, LIST_TYPE.getType()));
50+
return tryParse(() -> parseList(json, (trimmed) -> this.gson.fromJson(trimmed, LIST_TYPE.getType())),
51+
Exception.class);
5052
}
5153

5254
private static final class MapTypeToken extends TypeToken<Map<String, Object>> {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -175,4 +175,15 @@ void escapeDoubleQuote() {
175175
assertThat(map.get("foo")).isEqualTo("\"bar\"");
176176
}
177177

178+
@Test
179+
void listWithMalformedMap() {
180+
assertThatExceptionOfType(JsonParseException.class)
181+
.isThrownBy(() -> this.parser.parseList("[tru,erqett,{\"foo\":fatrue,true,true,true,tr''ue}]"));
182+
}
183+
184+
@Test
185+
void mapWithKeyAndNoValue() {
186+
assertThatExceptionOfType(JsonParseException.class).isThrownBy(() -> this.parser.parseMap("{\"foo\"}"));
187+
}
188+
178189
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/YamlJsonParserTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.json;
1818

19+
import org.junit.jupiter.api.Disabled;
1920
import org.junit.jupiter.api.Test;
2021
import org.yaml.snakeyaml.constructor.ConstructorException;
2122

@@ -40,4 +41,16 @@ void customTypesAreNotLoaded() {
4041
.withCauseInstanceOf(IllegalStateException.class);
4142
}
4243

44+
@Test
45+
@Override
46+
@Disabled("SnakeYaml does not fail when a map is malformed")
47+
void listWithMalformedMap() {
48+
}
49+
50+
@Test
51+
@Override
52+
@Disabled("SnakeYaml does not fail when a map has a key with no value")
53+
void mapWithKeyAndNoValue() {
54+
}
55+
4356
}

0 commit comments

Comments
 (0)