Skip to content

fix: 支持 object 类型 schema 使用 children 字段定义子属性#7714

Closed
OMSociety wants to merge 1 commit intoAstrBotDevs:masterfrom
OMSociety:fix-object-schema-children
Closed

fix: 支持 object 类型 schema 使用 children 字段定义子属性#7714
OMSociety wants to merge 1 commit intoAstrBotDevs:masterfrom
OMSociety:fix-object-schema-children

Conversation

@OMSociety
Copy link
Copy Markdown

@OMSociety OMSociety commented Apr 21, 2026

问题描述

加载 astrbot_plugin_schedule_assistant 插件时报错:

KeyError: 'items'
  File "/AstrBot/astrbot/core/config/astrbot_config.py", line 85, in _parse_schema
    _parse_schema(v["items"], conf[k])

根本原因

_conf_schema.jsonapple_calendar 字段定义为:

"apple_calendar": {
  "type": "object",
  "children": { ... }  // 使用 children 而非 items
}

astrbot_config.py 第 85 行强制要求 v["items"],导致 KeyError

修复方案

在解析 object 类型 schema 时,同时支持 itemschildren 两种写法,兼容新旧插件配置。

# 修复前
_parse_schema(v["items"], conf[k])

# 修复后
sub_schema = v.get("items") or v.get("children")
if sub_schema:
    _parse_schema(sub_schema, conf[k])

测试验证

已在本地修改 /AstrBot/astrbot/core/config/astrbot_config.py 确认逻辑正确。

Summary by Sourcery

Support both items and children when parsing object-type configuration schemas to avoid KeyError and maintain compatibility with different plugin schema formats.

Bug Fixes:

  • Prevent KeyError when loading plugins whose object-type schema defines sub-properties via children instead of items.

Enhancements:

  • Make configuration schema parsing more flexible by allowing object-type fields to use either items or children for nested definitions.

修复 KeyError: 'items' 问题,当 schema 中 object 类型使用 children 而非
items 定义子属性时也能正常解析。

Fixes: 插件加载失败,_conf_schema.json 中 object 类型字段缺少 items

Co-authored-by: Flandre Scarlet <flan@slandre.com>
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Apr 21, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +85 to +87
# 优先使用 items,其次使用 children(兼容两种 schema 写法)
sub_schema = v.get("items") or v.get("children")
if sub_schema:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +86 to +88
sub_schema = v.get("items") or v.get("children")
if sub_schema:
_parse_schema(sub_schema, conf[k])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

这里的逻辑 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 字段处理逻辑应附带相应的单元测试以确保稳定性。

Suggested change
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
  1. When implementing similar functionality for different cases, refactor the logic into a shared helper function to avoid code duplication.
  2. New functionality, such as handling attachments, should be accompanied by corresponding unit tests.

@OMSociety OMSociety closed this by deleting the head repository Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant