tool_parser_partial_json.rs 4.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Partial JSON Parser Tests
//!
//! Tests for the partial JSON parser with allow_partial_strings flag behavior

use sglang_router_rs::tool_parser::partial_json::PartialJson;

#[test]
fn test_partial_string_flag_disallows_incomplete_strings() {
    // Test case from the bug report: {"name": "
    // With allow_partial_strings=false, should return {} (stop before incomplete string)
    let parser = PartialJson::new(32, true);
    let input = r#"{"name": ""#;

    let result = parser.parse_value(input, false);
    assert!(result.is_ok());

    let (obj, consumed) = result.unwrap();

    // Should parse just the opening brace and stop at the incomplete string
    assert!(obj.is_object());
    let obj_map = obj.as_object().unwrap();

    // Should have empty object (stopped before parsing incomplete "name" key)
    assert!(
        obj_map.is_empty() || !obj_map.contains_key("name"),
        "Should not parse incomplete string key, got: {:?}",
        obj_map
    );

    // Should consume characters up to the incomplete string
    assert!(consumed <= input.len());
}

#[test]
fn test_partial_string_flag_allows_incomplete_strings() {
    // Test case: {"name": "
    // With allow_partial_strings=true, should parse the incomplete string
    let parser = PartialJson::new(32, true);
    let input = r#"{"name": ""#;

    let result = parser.parse_value(input, true);
    assert!(result.is_ok());

    let (obj, consumed) = result.unwrap();

    // Should parse the object with incomplete string value
    assert!(obj.is_object());
    let obj_map = obj.as_object().unwrap();

    // With allow_partial_strings=true, should parse "name" key with empty string value
    assert!(
        obj_map.contains_key("name"),
        "Should parse incomplete string with allow_partial_strings=true"
    );

    assert_eq!(consumed, input.len());
}

#[test]
fn test_partial_string_flag_complete_json() {
    // Test case: {"name": "test"}
    // Both flags should parse complete JSON the same way
    let input = r#"{"name": "test"}"#;

    let parser = PartialJson::new(32, true);
    let result1 = parser.parse_value(input, false);
    assert!(result1.is_ok());
    let (obj1, consumed1) = result1.unwrap();

    let result2 = parser.parse_value(input, true);
    assert!(result2.is_ok());
    let (obj2, consumed2) = result2.unwrap();

    // Both should parse the same complete JSON
    assert_eq!(obj1, obj2);
    assert_eq!(consumed1, consumed2);
    assert_eq!(consumed1, input.len());

    // Check the parsed value
    assert!(obj1.is_object());
    let obj_map = obj1.as_object().unwrap();
    assert_eq!(obj_map.get("name").and_then(|v| v.as_str()), Some("test"));
}

#[test]
fn test_backward_compatibility_default() {
    // Test that default PartialJson still allows partial strings (backward compatible)
    let parser = PartialJson::default();
    let input = r#"{"name": ""#;

    let result = parser.parse_value(input, true);
    assert!(result.is_ok());

    let (obj, _) = result.unwrap();
    assert!(obj.is_object());

    // Default behavior should allow partial strings
    let obj_map = obj.as_object().unwrap();
    assert!(
        obj_map.contains_key("name"),
        "Default should allow partial strings for backward compatibility"
    );
}

#[test]
fn test_partial_string_in_nested_object() {
    // Test case: {"tool": {"name": "
    let parser = PartialJson::new(32, true);
    let input = r#"{"tool": {"name": ""#;

    let result = parser.parse_value(input, false);
    assert!(result.is_ok());

    let (obj, _) = result.unwrap();
    assert!(obj.is_object());

    // With allow_partial_strings=false, should stop before incomplete nested string
    let obj_map = obj.as_object().unwrap();
    if let Some(tool) = obj_map.get("tool") {
        if let Some(tool_map) = tool.as_object() {
            assert!(
                !tool_map.contains_key("name")
                    || tool_map.get("name").and_then(|v| v.as_str()).is_none(),
                "Should not parse incomplete nested string"
            );
        }
    }
}

#[test]
fn test_bug_fix_exact_scenario() {
    // This test verifies the exact bug scenario from the issue:
    // buffer = "{\"name\": \""
    // flags = Allow.ALL & ~Allow.STR
    // Python returns: Parsed object: {}, consumed length: 10

    let parser = PartialJson::new(32, true);
    let input = r#"{"name": ""#;

    let result = parser.parse_value(input, false);
    assert!(result.is_ok());

    let (obj, consumed) = result.unwrap();

    // Should return empty object (not {"name": null} or {"name": ""})
    assert!(obj.is_object());
    let obj_map = obj.as_object().unwrap();
    assert!(
        obj_map.is_empty(),
        "Expected empty object, got: {:?}. This matches Python behavior with Allow.ALL & ~Allow.STR",
        obj_map
    );

    // Should consume all characters (10 bytes)
    assert_eq!(consumed, 10, "Should consume all 10 characters");
}