[#4966] Fixing flaky test UnitTestSwaggerUtils#testSwagger
without adding dependencies
#4984
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
The method
testSwagger ()
inUnitTestSwaggerUtils
from moduleswagger/swagger-generator/generator-core
compares pretty-printed YAML strings to verify correctness. However, since the OpenAPI object is constructed withMap
/Set
iteration, the key order in the serialized YAML is non-deterministic. When the iteration order changes, the string comparison fails, resulting in flaky behavior.This test was flagged by the NonDex tool, which detects unreliable tests caused by hidden assumptions in Java API ordering. To reproduce, run an upper test that calls
testSwagger
, for example:Affected Tests
This change fixes the below tests:
org.apache.servicecomb.swagger.generator.core.TestPojo.testResponseEntity
org.apache.servicecomb.swagger.generator.core.TestSwaggerUtils.testSchemaMethod
org.apache.servicecomb.swagger.generator.core.TestPojoExample.testPojoExample1
Fix
Instead of comparing raw YAML strings or using JSONAssert as my previous PR, the new implementation parses both expected and actual OpenAPI YAML into JsonNode objects using Jackson’s ObjectMapper and YAMLFactory.
It then checks structural equality via JsonNode.equals(), ensuring order-insensitive comparison while preserving strict value matching.
Rerunning NonDex with this change shows a stable passing result.
PR Checklist
UnitTestSwaggerUtils#testSwagger
(#4966 )mvn clean install -Pit
to make sure basic checks pass. A more thorough check will be performed on your pull request automatically.[SCB-XXX]
, where you replaceSCB-XXX
with the appropriate JIRA issue.