Skip to content

Commit

Permalink
fix: jackson deserialization is case-insensitive (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
YongwuHe authored Oct 12, 2023
1 parent 0c92a29 commit 0230131
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ private void configMapper() {
MAPPER.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
MAPPER.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
MAPPER.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
MAPPER.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
}

private void customTimeFormatSerializer(SimpleModule module) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,37 @@ void testFastUtil() throws Throwable {
assertNotNull(deserializeJackTestType);
}

@Test
void testCaseSensitiveProperties() throws Throwable {
final CaseSensitive caseSensitive = new CaseSensitive();
caseSensitive.setAmountPaid("100");
caseSensitive.setAmountpaid("200");
final String jackJson = JacksonSerializer.INSTANCE.serialize(caseSensitive);
final CaseSensitive deserializeJackTestType = JacksonSerializer.INSTANCE.deserialize(jackJson, CaseSensitive.class);
assertNotNull(deserializeJackTestType);
assertEquals("100", deserializeJackTestType.getAmountPaid());
assertEquals("200", deserializeJackTestType.getAmountpaid());
}

static class CaseSensitive {
private String amountPaid;
private String amountpaid;

public String getAmountPaid() {
return amountPaid;
}

public void setAmountPaid(String amountPaid) {
this.amountPaid = amountPaid;
}

public String getAmountpaid() {
return amountpaid;
}

public void setAmountpaid(String amountpaid) {
this.amountpaid = amountpaid;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ private boolean needRecord() {
size = Array.getLength(result);
}
if (size > RESULT_SIZE_MAX) {
String methodInfo = methodSignatureKey == null ? buildDuplicateMethodKey() : methodSignatureKey;
LogManager.warn(NEED_RECORD_TITLE,
StringUtil.format("do not record method, cuz result size:%s > max limit: %s, method info: %s",
String.valueOf(size), String.valueOf(RESULT_SIZE_MAX), methodSignatureKey));
String.valueOf(size), String.valueOf(RESULT_SIZE_MAX), methodInfo));
return false;
}
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.stream.Stream;
import org.mockito.stubbing.Answer;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -438,4 +439,11 @@ void invalidOperation() throws Throwable {
final MockResult replay = extractor.replay();
assertEquals(MockResult.IGNORE_MOCK_RESULT, replay);
}

@Test
void emptyMethodKeyAndExceedSize() throws NoSuchMethodException {
Method testEmptyArgs = DynamicClassExtractorTest.class.getDeclaredMethod("invalidOperation");
DynamicClassExtractor extractor = new DynamicClassExtractor(testEmptyArgs, new Object[0]);
assertDoesNotThrow(() -> extractor.recordResponse(new int[1001]));
}
}

0 comments on commit 0230131

Please sign in to comment.