fix reference for issue #3347

This commit is contained in:
wenshao 2025-02-22 07:53:01 +08:00
parent f90115722e
commit 3806502070
5 changed files with 34 additions and 16 deletions

View File

@ -2432,13 +2432,8 @@ public abstract class JSONReader
break;
case '{':
if (isReference()) {
String path = readReference();
if (path.startsWith("$") || path.equals("..") || path.equals(".")) {
addResolveTask(object, name, JSONPath.of(path));
val = null;
} else {
val = path;
}
addResolveTask(object, name, JSONPath.of(readReference()));
val = null;
} else {
val = readObject();
}

View File

@ -18,7 +18,7 @@ final class JSONReaderASCII
extends JSONReaderUTF8 {
final String str;
static final int ESCAPE_INDEX_NOT_SET = -2;
protected int nextEscapeIndex = ESCAPE_INDEX_NOT_SET;
private int nextEscapeIndex = ESCAPE_INDEX_NOT_SET;
JSONReaderASCII(Context ctx, String str, byte[] bytes, int offset, int length) {
super(ctx, bytes, offset, length);

View File

@ -422,10 +422,10 @@ final class JSONReaderUTF16
return false;
}
return readReference0(chars, offset, end, quote);
return isReference0(chars, offset, end, quote);
}
private boolean readReference0(char[] chars, int offset, int end, char quote) {
private boolean isReference0(char[] chars, int offset, int end, char quote) {
char ch;
offset += 6;
ch = chars[offset];
@ -450,7 +450,9 @@ final class JSONReaderUTF16
ch = chars[offset];
}
if (ch != quote || (offset + 1 < end && chars[offset + 1] == '#')) {
if (ch != quote
|| (offset + 1 < end && (ch = chars[offset + 1]) != '$' && ch != '.' && ch != '@')
) {
return false;
}

View File

@ -6885,10 +6885,10 @@ class JSONReaderUTF8
return false;
}
return readReference0(bytes, offset, end, ch);
return isReference0(bytes, offset, end, ch);
}
private boolean readReference0(byte[] bytes, int offset, int end, int quote) {
private boolean isReference0(byte[] bytes, int offset, int end, int quote) {
int ch;
offset += 6;
ch = bytes[offset];
@ -6913,7 +6913,9 @@ class JSONReaderUTF8
ch = bytes[offset];
}
if (ch != quote || (offset + 1 < end && bytes[offset + 1] == '#')) {
if (ch != quote
|| (offset + 1 < end && (ch = bytes[offset + 1]) != '$' && ch != '.' && ch != '@')
) {
return false;
}

View File

@ -5,11 +5,30 @@ import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Issue3347 {
@Test
public void test() {
String json2 = "{\"*/*\":{\"schema\":{\"$ref\":\"Error-ModelName{namespace='javax.servlet.http', name='HttpServletResponse'}\"}}}";
System.out.println(json2);
JSONObject jsonObject4 = JSON.parseObject(json2, JSONReader.Feature.DisableReferenceDetect);
String expected = "Error-ModelName{namespace='javax.servlet.http', name='HttpServletResponse'}";
{
JSONObject jsonObject4 = JSON.parseObject(json2);
assertEquals(expected, jsonObject4.getJSONObject("*/*").getJSONObject("schema").getString("$ref"));
}
{
JSONObject jsonObject4 = JSON.parseObject(json2.getBytes(StandardCharsets.UTF_8));
assertEquals(expected, jsonObject4.getJSONObject("*/*").getJSONObject("schema").getString("$ref"));
}
{
JSONObject jsonObject4 = JSON.parseObject(json2.toCharArray());
assertEquals(expected, jsonObject4.getJSONObject("*/*").getJSONObject("schema").getString("$ref"));
}
{
JSONObject jsonObject4 = JSON.parseObject(json2, JSONReader.Feature.DisableReferenceDetect);
assertEquals(expected, jsonObject4.getJSONObject("*/*").getJSONObject("schema").getString("$ref"));
}
}
}