fix: fix JSON validation to accept plain strings#138
fix: fix JSON validation to accept plain strings#13818202781743 merged 2 commits intolinuxdeepin:masterfrom
Conversation
The stringToQVariant function was incorrectly rejecting plain strings
that weren't valid JSON by returning an empty QVariant. This caused
valid configuration values to be filtered out when they should have been
accepted as regular strings.
1. Modified stringToQVariant to wrap single JSON values in an array
for parsing
2. Added fallback to return the original string if JSON parsing fails
3. Enhanced isValidTextJsonValue to better distinguish between malformed
JSON and plain text
4. Added QJsonArray include for array operations
The fix ensures that:
- Valid JSON objects and arrays are properly parsed
- Single JSON values (numbers, booleans, null, quoted strings) are
supported
- Plain text strings that aren't JSON are accepted as-is
- Only strings starting with { or [ that fail JSON parsing are rejected
as malformed JSON
Log: Fixed issue where plain text strings were incorrectly filtered out
in configuration values
Influence:
1. Test configuration values with plain text strings containing special
characters
2. Verify JSON objects and arrays are still properly validated
3. Test single JSON values like numbers, booleans, and null
4. Test strings that look like JSON but are malformed (should be
rejected)
5. Verify mixed content with both JSON and plain text scenarios
fix: 修复JSON验证以接受普通字符串
stringToQVariant函数之前错误地拒绝了不是有效JSON的普通字符串,返回空的
QVariant。这导致有效的配置值被错误过滤,而它们本应作为普通字符串被接受。
1. 修改stringToQVariant函数,将单个JSON值包装在数组中进行解析
2. 添加回退机制,当JSON解析失败时返回原始字符串
3. 增强isValidTextJsonValue函数,更好地区分格式错误的JSON和纯文本
4. 添加QJsonArray包含以支持数组操作
修复确保:
- 有效的JSON对象和数组被正确解析
- 单个JSON值(数字、布尔值、null、带引号的字符串)得到支持
- 不是JSON的纯文本字符串被原样接受
- 只有以{或[开头但JSON解析失败的字符串被拒绝为格式错误的JSON
Log: 修复了配置值中普通字符串被错误过滤的问题
Influence:
1. 测试包含特殊字符的纯文本字符串配置值
2. 验证JSON对象和数组是否仍被正确验证
3. 测试单个JSON值,如数字、布尔值和null
4. 测试看起来像JSON但格式错误的字符串(应被拒绝)
5. 验证同时包含JSON和纯文本的混合内容场景
PMS: BUG-349663
Change-Id: Ieb2deccba3ba4e89266a9e8027f2377abbe26a48
Reviewer's GuideAdjusts JSON handling utilities so that plain text strings are accepted as-is, while still correctly parsing full JSON documents and single JSON values and more accurately rejecting only truly malformed JSON that looks like structured data. Flow diagram for updated stringToQVariant JSON handlingflowchart TD
A[Input string s] --> B[Parse s as JSON document]
B --> C{JSON parse error?}
C -->|No| D[Return doc.toVariant]
C -->|Yes| E["Wrap s as [s]"]
E --> F[Parse wrapped JSON as document]
F --> G{Wrapped JSON parse error?}
G -->|No| H[Get array from wrappedDoc]
H --> I{Array is empty?}
I -->|No| J[Return first element toVariant]
I -->|Yes| K[Return s as plain string]
G -->|Yes| K[Return s as plain string]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The JSON parsing logic is now duplicated between stringToQVariant and isValidTextJsonValue; consider extracting the "parse as document, then as single value" flow into a shared helper to keep behavior consistent and reduce maintenance overhead.
- When wrapping the string as JSON (e.g., QString("[") + s + QString("]")), you can simplify and slightly optimize this by using QStringLiteral or direct concatenation like "[" + s + "]" to avoid unnecessary temporary QString constructions.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The JSON parsing logic is now duplicated between stringToQVariant and isValidTextJsonValue; consider extracting the "parse as document, then as single value" flow into a shared helper to keep behavior consistent and reduce maintenance overhead.
- When wrapping the string as JSON (e.g., QString("[") + s + QString("]")), you can simplify and slightly optimize this by using QStringLiteral or direct concatenation like "[" + s + "]" to avoid unnecessary temporary QString constructions.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
Fixes configuration value handling so non-JSON text is preserved as plain strings instead of being rejected, while still supporting JSON objects/arrays and standalone JSON primitives.
Changes:
- Update
stringToQVariantto parse standalone JSON primitives by wrapping input in an array, with a fallback to return the original string on parse failure. - Update
isValidTextJsonValueto accept plain text as valid input while still rejecting malformed JSON that appears to be a structured JSON (starts with{or[). - Add
QJsonArrayinclude to support array extraction after wrapped parsing.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
1. Added QJsonObject include for proper JSON handling 2. Modified qvariantToString and qvariantToStringCompact to add quotes around string values when converting QVariant to string 3. Changed stringToQVariant to handle JSON parsing more robustly with clearer logic 4. Updated isValidTextJsonValue to validate JSON values more accurately by checking trimmed input and proper JSON parsing 5. Fixed getCommand output to use qvariantToString directly instead of adding extra quotes Log: Fixed JSON string formatting in configuration output Influence: 1. Test configuration value display to ensure string values are properly quoted 2. Verify JSON array and object parsing from string input 3. Test edge cases like empty strings, whitespace-only strings, and malformed JSON 4. Validate that numeric, boolean, and null JSON values are handled correctly 5. Check that complex nested JSON structures are properly converted 6. Test command line output formatting for different data types fix: 改进变体转换中的JSON字符串处理 1. 添加QJsonObject包含以支持正确的JSON处理 2. 修改qvariantToString和qvariantToStringCompact,在将QVariant转换为字符 串时为字符串值添加引号 3. 改进stringToQVariant,使用更清晰的逻辑更稳健地处理JSON解析 4. 更新isValidTextJsonValue,通过检查修剪后的输入和正确的JSON解析来更准 确地验证JSON值 5. 修复getCommand输出,直接使用qvariantToString而不是添加额外的引号 Log: 修复配置输出中的JSON字符串格式问题 Influence: 1. 测试配置值显示,确保字符串值被正确添加引号 2. 验证从字符串输入解析JSON数组和对象 3. 测试边界情况,如空字符串、仅空白字符的字符串和格式错误的JSON 4. 验证数字、布尔值和null JSON值是否正确处理 5. 检查复杂的嵌套JSON结构是否正确转换 6. 测试不同数据类型的命令行输出格式 PMS: BUG-349663 Change-Id: I1f2f441405b0ac73896b222339dc02c5c863d0f2
deepin pr auto reviewGit Diff 代码审查报告我已对提供的 Git diff 进行了全面审查,主要关注语法逻辑、代码质量、代码性能和代码安全等方面。以下是我的详细分析和建议: 1. 语法逻辑分析1.1 JSON 字符串处理逻辑在 const QJsonValue jsonVal = QJsonValue::fromVariant(v);
if (jsonVal.isString()) {
return QString("\"%1\"").arg(jsonVal.toString());
}问题:这个逻辑存在潜在问题。当字符串本身包含引号时,可能会导致 JSON 格式错误。例如,如果输入字符串是 建议:应该使用 1.2
|
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 18202781743, mhduiy The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
The stringToQVariant function was incorrectly rejecting plain strings
that weren't valid JSON by returning an empty QVariant. This caused
valid configuration values to be filtered out when they should have been
accepted as regular strings.
for parsing
JSON and plain text
The fix ensures that:
supported
as malformed JSON
Log: Fixed issue where plain text strings were incorrectly filtered out
in configuration values
Influence:
characters
rejected)
fix: 修复JSON验证以接受普通字符串
stringToQVariant函数之前错误地拒绝了不是有效JSON的普通字符串,返回空的
QVariant。这导致有效的配置值被错误过滤,而它们本应作为普通字符串被接受。
修复确保:
Log: 修复了配置值中普通字符串被错误过滤的问题
Influence:
PMS: BUG-349663
Change-Id: Ieb2deccba3ba4e89266a9e8027f2377abbe26a48
Summary by Sourcery
Relax JSON value handling to correctly parse single JSON values and accept non-JSON text as plain strings in configuration values.
Bug Fixes:
Enhancements: