Browse Source

Merge branch '2.6.x' into 2.7.x

Closes gh-31499
pull/31518/head
Andy Wilkinson 4 years ago
parent
commit
268f13dbd7
  1. 6
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java
  2. 8
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java
  3. 13
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java
  4. 15
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/YamlJsonParserTests.java

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

@ -1,5 +1,5 @@ @@ -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 { @@ -39,12 +39,12 @@ public class BasicJsonParser extends AbstractJsonParser {
@Override
public Map<String, Object> parseMap(String json) {
return parseMap(json, this::parseMapInternal);
return tryParse(() -> parseMap(json, this::parseMapInternal), Exception.class);
}
@Override
public List<Object> parseList(String json) {
return parseList(json, this::parseListInternal);
return tryParse(() -> parseList(json, this::parseListInternal), Exception.class);
}
private List<Object> parseListInternal(String json) {

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

@ -1,5 +1,5 @@ @@ -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 { @@ -41,12 +41,14 @@ public class GsonJsonParser extends AbstractJsonParser {
@Override
public Map<String, Object> 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<Object> 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<Map<String, Object>> {

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

@ -1,5 +1,5 @@ @@ -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 { @@ -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\"}"));
}
}

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

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -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() {
}
}

Loading…
Cancel
Save