Skip to content

Commit

Permalink
Merge branch '2.6.x' into 2.7.x
Browse files Browse the repository at this point in the history
Closes gh-31499
  • Loading branch information
wilkinsona committed Jun 22, 2022
2 parents e967d03 + 522ea0a commit 268f13d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
@@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
@@ -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.
Expand Down Expand Up @@ -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>> {
Expand Down
@@ -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.
Expand Down Expand Up @@ -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\"}"));
}

}
@@ -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.
Expand All @@ -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;

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

}

0 comments on commit 268f13d

Please sign in to comment.