fix: 支持 object 类型 schema 使用 children 字段定义子属性#7714
fix: 支持 object 类型 schema 使用 children 字段定义子属性#7714OMSociety wants to merge 1 commit intoAstrBotDevs:masterfrom
Conversation
修复 KeyError: 'items' 问题,当 schema 中 object 类型使用 children 而非 items 定义子属性时也能正常解析。 Fixes: 插件加载失败,_conf_schema.json 中 object 类型字段缺少 items Co-authored-by: Flandre Scarlet <flan@slandre.com>
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- When neither
itemsnorchildrenis present for anobjectschema, the code now silently leavesconf[k]as an empty dict; consider explicitly handling this case (e.g., raising or logging) so misconfigured schemas are surfaced instead of failing quietly.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- When neither `items` nor `children` is present for an `object` schema, the code now silently leaves `conf[k]` as an empty dict; consider explicitly handling this case (e.g., raising or logging) so misconfigured schemas are surfaced instead of failing quietly.
## Individual Comments
### Comment 1
<location path="astrbot/core/config/astrbot_config.py" line_range="85-87" />
<code_context>
if v["type"] == "object":
conf[k] = {}
- _parse_schema(v["items"], conf[k])
+ # 优先使用 items,其次使用 children(兼容两种 schema 写法)
+ sub_schema = v.get("items") or v.get("children")
+ if sub_schema:
+ _parse_schema(sub_schema, conf[k])
elif v["type"] == "template_list":
</code_context>
<issue_to_address>
**issue (bug_risk):** `items` may be unintentionally ignored when it's an empty dict
Using `v.get("items") or v.get("children")` means an explicitly empty `items` dict (`{}`) will be ignored and `children` used instead, which contradicts the stated precedence. To enforce the intended priority, base the choice on key presence/None, e.g. `sub_schema = v.get("items") if "items" in v else v.get("children")` (or equivalent).
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| # 优先使用 items,其次使用 children(兼容两种 schema 写法) | ||
| sub_schema = v.get("items") or v.get("children") | ||
| if sub_schema: |
There was a problem hiding this comment.
issue (bug_risk): items may be unintentionally ignored when it's an empty dict
Using v.get("items") or v.get("children") means an explicitly empty items dict ({}) will be ignored and children used instead, which contradicts the stated precedence. To enforce the intended priority, base the choice on key presence/None, e.g. sub_schema = v.get("items") if "items" in v else v.get("children") (or equivalent).
There was a problem hiding this comment.
Code Review
This pull request updates the configuration schema parsing in astrbot/core/config/astrbot_config.py to support both items and children keys for object types, enhancing schema compatibility. The review feedback identifies a potential logic error where an empty items dictionary would incorrectly fall back to children due to Python's boolean evaluation. The reviewer suggests a more robust implementation using dict.get(), recommends refactoring this logic into a shared helper function to maintain consistency across the project, and advises adding unit tests for the new functionality.
| sub_schema = v.get("items") or v.get("children") | ||
| if sub_schema: | ||
| _parse_schema(sub_schema, conf[k]) |
There was a problem hiding this comment.
这里的逻辑 v.get("items") or v.get("children") 在 items 为空字典 {} 时会由于 Python 的布尔评估机制而回退到 children。使用 v.get("items", v.get("children")) 能更严谨地表达优先级。
更重要的是,此修复目前是不完整的。astrbot/dashboard/routes/config.py 中的 validate_config 函数仍然硬编码访问 meta["items"]。根据项目规则,建议将此类跨文件的相似逻辑重构为共享的助手函数,以避免代码重复并确保一致性。此外,新增的 schema 字段处理逻辑应附带相应的单元测试以确保稳定性。
| sub_schema = v.get("items") or v.get("children") | |
| if sub_schema: | |
| _parse_schema(sub_schema, conf[k]) | |
| sub_schema = v.get("items", v.get("children")) | |
| if sub_schema: | |
| _parse_schema(sub_schema, conf[k]) |
References
- When implementing similar functionality for different cases, refactor the logic into a shared helper function to avoid code duplication.
- New functionality, such as handling attachments, should be accompanied by corresponding unit tests.
问题描述
加载
astrbot_plugin_schedule_assistant插件时报错:根本原因
_conf_schema.json中apple_calendar字段定义为:但
astrbot_config.py第 85 行强制要求v["items"],导致KeyError。修复方案
在解析
object类型 schema 时,同时支持items和children两种写法,兼容新旧插件配置。测试验证
已在本地修改
/AstrBot/astrbot/core/config/astrbot_config.py确认逻辑正确。Summary by Sourcery
Support both
itemsandchildrenwhen parsing object-type configuration schemas to avoid KeyError and maintain compatibility with different plugin schema formats.Bug Fixes:
childreninstead ofitems.Enhancements:
itemsorchildrenfor nested definitions.