Greedy (?) object typing leads to semantically wrong error message
まだ誰も着手していません。
評価
調査の方向性
jsonschema.validate を使って issue のスキーマとデータを再現し、その後 validators.py の検証パスを調べます。anyOf の各分岐がどのように ValidationError を生成するかを比較します。done は、無効なエンティティデータが entity_property なしで BREAK_ENTITY を報告することを意味し、実証されたオブジェクト順序のケースをカバーする必要があります。
索引モデルが issue の本文から書いたものです。
説明
Hi, thanks for a great project!
We've stumbled across an issue with misleading error messages, although I'm not sure if there is a useful fix.
jsonschemaversion:3.2.0- Python:
3.8.5
When you define two objects (e.g., person and entity), which share a number of properties but define others which are mutually exclusive across the two, and one object (say, entity) also requires fields where the other doesn't, properties are reported in a ValidationError as unexpected when from the user perspective, they aren't.
Expected behaviour
Given the definitions
person(properties:person_property(optional),alias(optional))entity(properties:entity_property(required),alias(optional))
and data that introduces an additional property BREAK_ENTITY (which is disallowed via "additionalProperties": false) to an otherwise valid entity data object, I expect the error message to raise the additional key, but not any other (semantically valid) keys.
Actual behaviour
The error messages raises two unexpected keys: the additional one (correct), and entity_property (incorrect).
Without having had a look at the source code, I assume that the additional property trips the error, and then trying to type the object and collecting unexpected properties is greedy, and in this case assumes person whereas we're looking at entity really, which would be valid if only the additional property was removed. I guess changing the heuristic to find the best match may come at a performance penalty though, as you'd have to look at all potential best matches?
This also happens independently from which of the two objects is defined first, but depends on the order of objects in anyOf.
Reproducing the issue
(Minimalized (well, more or less) schema
{
"$schema": "http://json-schema.org/draft-07/schema",
"additionalProperties": false,
"type": "object",
"properties": {
"parties": {
"items": {
"anyOf": [
{
"$ref": "#/definitions/person"
},
{
"$ref": "#/definitions/entity"
}
]
},
"minItems": 1,
"type": "array",
"uniqueItems": true
}
},
"required": [
"parties"
],
"definitions": {
"person": {
"type": "object",
"additionalProperties": false,
"properties": {
"person_property": {
"type": "string",
"minLength": 1
},
"alias": {
"$ref": "#/definitions/alias"
}
}
},
"entity": {
"additionalProperties": false,
"properties": {
"entity_property": {
"type": "string",
"minLength": 1
},
"alias": {
"$ref": "#/definitions/alias"
}
},
"required": [
"entity_property"
],
"type": "object"
},
"alias": {
"type": "string",
"minLength": 1
}
}
}
Valid data
{
"parties": [
{
"entity_property": "Entity",
"alias": "Entity alias"
},
{
"person_property": "Person",
"alias": "Person alias"
}
]
}
Invalid person data (yields expected message)
The following invalid person data breaks and yields the expected error message: jsonschema.exceptions.ValidationError: Additional properties are not allowed ('BREAK_PERSON' was unexpected).
{
"parties": [
{
"entity_property": "Entity",
"alias": "Entity alias"
},
{
"person_property": "Person",
"alias": "Person alias",
"BREAK_PERSON": "BREAK"
}
]
}
ISSUE: Invalid entity data (yields unexpected message)
The following invalid entity data yields the expected error, albeit with an unexpected error message jsonschema.exceptions.ValidationError: Additional properties are not allowed ('BREAK_ENTITY', 'entity_property' were unexpected) (, 'entity_property' being the unexpected part, full trace further below)
{
"parties": [
{
"entity_property": "Entity",
"alias": "Entity alias",
"BREAK_ENTITY": "BREAK"
},
{
"person_property": "Person",
"alias": "Person alias"
}
]
}
Trace
Linebreaks included for legibility.
Traceback (most recent call last):
File "repr_bug.py", line 27, in <module>
validate(args.data, args.schema)
File "repr_bug.py", line 14, in validate
jsonschema.validate(instance=data, schema=schema_data)
File "~/venv/lib/python3.8/site-packages/jsonschema/validators.py", line 934, in validate
raise error
jsonschema.exceptions.ValidationError:
Additional properties are not allowed ('BREAK_ENTITY', 'entity_property' were unexpected)
Failed validating 'additionalProperties' in schema[0]:
{'additionalProperties': False,
'properties': {'alias': {'$ref': '#/definitions/alias'},
'person_property': {'minLength': 1, 'type': 'string'}},
'type': 'object'}
On instance:
{'BREAK_ENTITY': 'BREAK',
'alias': 'Entity alias',
'entity_property': 'Entity'}
- 主要言語
- Python
- スター
- 5k
- フォーク
- 694
- 平均マージ
- 1日 20時間
- マージ済み PR(30日)
- 6
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python-jsonschema/jsonschema のほかの issue
-
Iterator error contexts lose parent links and absolute paths対応中かも @DawnofGenX が 6 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
python-jsonschema/jsonschema#1584 · コメント 3 件 ·
-
regex format checker crashes with RecursionError on deeply nested patterns対応中かも @DawnofGenX が 6 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
python-jsonschema/jsonschema#1538 · コメント 1 件 ·
-
is_duration raises an uncaught decimal.Overflow on a duration with a large exponent再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンNeeds Test Upstream
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
python-jsonschema/jsonschema#1511 · コメント 1 件 ·
-
regex format checker crashes with ValueError on conflicting inline flags対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
python-jsonschema/jsonschema#1558 · コメント 2 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
python-jsonschema/jsonschema#1547 ·
python-jsonschema/jsonschema の issue をすべて見る
似ている issue
-
comp/gateway comp/plugins duplicate P3 type/bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
NousResearch/hermes-agent#137019 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
bot-triaged module: docs triaged
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
new-entry
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
BinWang28/audio-ai-hub#194 ·
-
confluence: a paged listing's links re-encode a parameter they carry, where real carries it as sentオープンfidelity
難易度 2/5 1〜3時間 初心者へのやさしさ 63/100
メンテナーはふだん 1 日以内に返信