Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto type support to remove dependency among tests #4458

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rRajivramachandran
Copy link

@rRajivramachandran rRajivramachandran commented Nov 11, 2023

The test com.alibaba.fastjson.serializer.TestParse.testParse fails when run standalone or if it is run before com.alibaba.json.bvt.support.spring.FastJsonRedisSerializerTest.test_6. This PR removes such a dependency.

Steps to reproduce

  1. Create a custom test suite CustomTestSuite.java and change pom.xml to just run this using mvn test. Changes can be pulled from the following branch test-setup.
  2. mvn install -pl .
  3. mvn test
  4. Reverse order in test suite and run mvn test again
  5. To see the test fail independently run mvn test -Dtest=com.alibaba.fastjson.serializer.TestParse#testParse

Issue and root cause

The following are the logs when TestParse is run before FastJsonRedisSerializerTest

[INFO] Running com.alibaba.fastjson.serializer.CustomSuiteTest
Nov 05, 2023 6:56:36 PM com.alibaba.fastjson.serializer.TestParse testParse
INFO: parsing json string:{"@type":"com.alibaba.fastjson.serializer.TestBean","data":{"@type":"com.alibaba.fastjson.JSONObject","key":"value"},"name":"tester"}
[ERROR] Tests run: 7, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.889 s <<< FAILURE! - in com.alibaba.fastjson.serializer.CustomSuiteTest
[ERROR] com.alibaba.fastjson.serializer.TestParse.testParse  Time elapsed: 1.171 s  <<< ERROR!
com.alibaba.fastjson.JSONException: autoType is not support. com.alibaba.fastjson.serializer.TestBean
	at com.alibaba.fastjson.serializer.TestParse.testParse(TestParse.java:37)

However when the order of test classes are reversed, it passes:

Running com.alibaba.fastjson.serializer.CustomSuiteTest
Nov 05, 2023 7:00:36 PM com.alibaba.fastjson.serializer.TestParse testParse
INFO: parsing json string:{"@type":"com.alibaba.fastjson.serializer.TestBean","data":{"@type":"com.alibaba.fastjson.JSONObject","key":"value"},"name":"tester"}
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.712 s - in com.alibaba.fastjson.serializer.CustomSuiteTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0

This happens because a TestBean class object is created and serialized producing an automatic type as indicated in the logs parsing json string:{"@type":"com.alibaba.fastjson.serializer.TestBean","data":{"@type":"com.alibaba.fastjson.JSONObject","key":"value"},"name":"tester"} during the run of TestParse. This string is attempted to be deserialized at TestParse. Due to security reasons, deserialization of auto type is set as a configurable flag, and if it is not enabled, the above seen error occurs ParserConfig. The FastJsonRedisSerializerTest sets this property on the global instance. The same global instance gets used when TestParse is run after it because it does not pass any config while calling JSON.parse(). This is the reason the test passes.

Fix:

To remove this dependency and not create any more dependencies, the flag is set on a local config object created for this test. This ParseConfig object is explicitly passed for deserialization.

Verification of fix:

Test passes when run standalone. (mvn test -Dtest=com.alibaba.fastjson.serializer.TestParse#testParse)

@CLAassistant
Copy link

CLAassistant commented Nov 11, 2023

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants