Skip to content

Commit

Permalink
fix hash collision for codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed Apr 14, 2024
1 parent 4ba62ad commit 04688d0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.alibaba.fastjson2.internal.processor.issues;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONCompiled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2411Bean {
static JSONWriter.Feature[] writerFeatures = {
JSONWriter.Feature.FieldBased,
JSONWriter.Feature.WriteNameAsSymbol
};
static JSONReader.Feature[] readerFeatures = {
JSONReader.Feature.UseDefaultConstructorAsPossible,
JSONReader.Feature.ErrorOnNoneSerializable, JSONReader.Feature.IgnoreAutoTypeNotMatch,
JSONReader.Feature.UseNativeObject, JSONReader.Feature.FieldBased
};

@Test
public void test() {
Bean course = new Bean();
course.courseId = "6bad799a1c894893bedade17215244a1";
course.userId = "4b99d48f87f84868a59aa3b3ce82fd56";

String json = JSON.toJSONString(course, writerFeatures);

Bean result = JSON.parseObject(json, Bean.class, readerFeatures);

assertEquals(course.userId, result.userId);
assertEquals(course.courseId, result.courseId);
}

@JSONCompiled
public static class Bean {
public String courseId;
public String userId;
public String studyRate;
public String resourceId;
public String providerCorpCode;
public String userAgent;
public String sourceId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,8 @@ private static void genRead243(
Block.IfStmt nextIfMatchStmt = label.ifStmt(nextIfMatch);
genReadFieldValue(nextIfMatchStmt, fieldReaderIndex, info, fieldReader, jsonReader, object, forLabel, false);
nextIfMatchStmt.continueStmt(forLabel);
label.breakStmt();
}
label.breakStmt();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private JCTree.JCMethodDecl genReadObject(JCTree.JCIdent objectType, JCTree.JCEx

JCTree.JCLabeledStatement switchLabel = label("_switch", null);
ListBuffer<JCTree.JCCase> cases = new ListBuffer<>();
for (int i = 0; i < fieldsSize; ++i) {
for (int i = 0; i < hashCode32Keys.length; ++i) {
java.util.List<Long> hashCode64Array = map.get(hashCode32Keys[i]);
List<JCTree.JCStatement> stmts = List.nil();
Long fieldNameHash = null;
Expand Down Expand Up @@ -457,6 +457,7 @@ private List<JCTree.JCStatement> genRead243(
for (int i = 0; i < labels.length; i++) {
int name0 = switchKeys[i];
java.util.List<AttributeInfo> fieldReaders = name0Map.get(name0);
ListBuffer<JCTree.JCStatement> caseStmts = new ListBuffer<>();
for (int j = 0; j < fieldReaders.size(); j++) {
AttributeInfo fieldReader = fieldReaders.get(j);
int fieldReaderIndex = readerIndexMap.get(fieldReader);
Expand Down Expand Up @@ -834,8 +835,10 @@ private List<JCTree.JCStatement> genRead243(
throw new IllegalStateException("fieldNameLength " + fieldNameLength);
}
List<JCTree.JCStatement> readFieldValueStmts = genReadFieldValue(fieldReader, jsonReaderIdent, fieldReaderIndex, structInfo, loopLabel, objectIdent, isJsonb);
cases.append(defCase(literal(name0), List.of(defIf(nextIfMethod, block(readFieldValueStmts), null), defBreak(switchLabel))));
caseStmts.append(defIf(nextIfMethod, block(readFieldValueStmts), null));
}
caseStmts.append(defBreak(switchLabel));
cases.append(defCase(literal(name0), caseStmts.toList()));
}
switchLabel.body = defSwitch(method(field(jsonReaderIdent, "getRawInt")), cases.toList());
stmts = stmts.append(switchLabel);
Expand Down

0 comments on commit 04688d0

Please sign in to comment.