Handle malformed JSON more consistently

Closes gh-31301
pull/31676/head
Andy Wilkinson 2 years ago
parent 79d3e3080f
commit 522ea0a90e

@ -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<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) {

@ -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<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>> {

@ -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\"}"));
}
}

@ -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() {
}
}

Loading…
Cancel
Save