|
| 1 | +package com.alibaba.fastjson2; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.*; |
| 9 | + |
| 10 | +public class JSONWriterPathTest { |
| 11 | + /** |
| 12 | + * Test for reproducing the bug with Unicode surrogate pairs in JSON field names. |
| 13 | + */ |
| 14 | + @Test |
| 15 | + public void testUnicodeSurrogatePairsInPath() { |
| 16 | + // Create a map with a key that contains Unicode surrogate pairs |
| 17 | + Map<String, Object> map = new HashMap<>(); |
| 18 | + // This emoji is represented by surrogate pairs: \uD83D\uDE00 (grinning face) |
| 19 | + String keyWithSurrogatePairs = "key" + "\uD83D\uDE00"; // "key😀" |
| 20 | + map.put(keyWithSurrogatePairs, "value"); |
| 21 | + |
| 22 | + // Also add a nested object to trigger reference path handling |
| 23 | + Map<String, Object> nested = new HashMap<>(); |
| 24 | + nested.put("nestedKey", "nestedValue"); |
| 25 | + map.put("nested", nested); |
| 26 | + |
| 27 | + // Enable reference detection to trigger the Path.toString() code path |
| 28 | + try (JSONWriter writer = JSONWriter.of(JSONWriter.Feature.ReferenceDetection)) { |
| 29 | + // Use JSONObjectWriter directly instead of ObjectWriterCreator |
| 30 | + writer.write(map); |
| 31 | + |
| 32 | + String json = writer.toString(); |
| 33 | + System.out.println("Generated JSON: " + json); |
| 34 | + assertNotNull(json); |
| 35 | + // The test passes if no exception is thrown during serialization |
| 36 | + // Check that the value is present in the output |
| 37 | + assertTrue(json.contains("value"), "JSON should contain the value 'value'"); |
| 38 | + } catch (Exception e) { |
| 39 | + fail("Exception thrown during JSON serialization with Unicode surrogate pairs: " + e.getMessage(), e); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Additional test with more complex Unicode surrogate pairs. |
| 45 | + */ |
| 46 | + @Test |
| 47 | + public void testComplexUnicodeSurrogatePairsInPath() { |
| 48 | + Map<String, Object> map = new HashMap<>(); |
| 49 | + // Using various emojis that are represented with surrogate pairs |
| 50 | + map.put("key😀", "value1"); // Grinning face |
| 51 | + map.put("key🌍", "value2"); // Earth globe |
| 52 | + map.put("key👍", "value3"); // Thumbs up |
| 53 | + |
| 54 | + try (JSONWriter writer = JSONWriter.of(JSONWriter.Feature.ReferenceDetection, JSONWriter.Feature.PrettyFormat)) { |
| 55 | + // Use JSONObjectWriter directly instead of ObjectWriterCreator |
| 56 | + writer.write(map); |
| 57 | + |
| 58 | + String json = writer.toString(); |
| 59 | + System.out.println("Generated JSON: " + json); |
| 60 | + assertNotNull(json); |
| 61 | + // The test passes if no exception is thrown during serialization |
| 62 | + // Check that the values are present in the output |
| 63 | + assertTrue(json.contains("value1"), "JSON should contain the value 'value1'"); |
| 64 | + assertTrue(json.contains("value2"), "JSON should contain the value 'value2'"); |
| 65 | + assertTrue(json.contains("value3"), "JSON should contain the value 'value3'"); |
| 66 | + } catch (Exception e) { |
| 67 | + fail("Exception thrown during JSON serialization with complex Unicode surrogate pairs: " + e.getMessage(), e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Test that specifically targets the bug with surrogate pairs in path handling. |
| 73 | + * This test creates a circular reference to force the Path.toString() method to be called. |
| 74 | + */ |
| 75 | + @Test |
| 76 | + public void testSurrogatePairsInPathWithCircularReference() { |
| 77 | + // Create objects with circular references |
| 78 | + Map<String, Object> parent = new HashMap<>(); |
| 79 | + Map<String, Object> child = new HashMap<>(); |
| 80 | + |
| 81 | + // Use keys with surrogate pairs |
| 82 | + parent.put("parent😀", child); |
| 83 | + child.put("child😀", parent); // Circular reference |
| 84 | + |
| 85 | + // Enable reference detection to trigger the Path.toString() code path |
| 86 | + try (JSONWriter writer = JSONWriter.of(JSONWriter.Feature.ReferenceDetection)) { |
| 87 | + // Use JSONObjectWriter directly instead of ObjectWriterCreator |
| 88 | + writer.write(parent); |
| 89 | + |
| 90 | + String json = writer.toString(); |
| 91 | + System.out.println("Generated JSON with circular reference: " + json); |
| 92 | + assertNotNull(json); |
| 93 | + // Should not throw any exception |
| 94 | + } catch (Exception e) { |
| 95 | + fail("Exception thrown during JSON serialization with surrogate pairs and circular reference: " + e.getMessage(), e); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments