Greedy (?) object typing leads to semantically wrong error message
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Riproduci lo schema e i dati dell’issue tramite jsonschema.validate, quindi esamina il percorso di validazione in validators.py. Confronta il modo in cui i rami anyOf producono il ValidationError; done deve significare che i dati dell’entità non validi segnalano BREAK_ENTITY senza entity_property, con copertura per i casi di ordine degli oggetti dimostrati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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'}
- Lingua principale
- Python
- Stelle
- 5k
- Fork
- 694
- Merge medio
- 1g 20h
- PR unite (30g)
- 6
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python-jsonschema/jsonschema
-
Iterator error contexts lose parent links and absolute pathsForse già presa @DawnofGenX l’ha presa 6 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
python-jsonschema/jsonschema#1584 · 3 commenti ·
-
regex format checker crashes with RecursionError on deeply nested patternsForse già presa @DawnofGenX l’ha presa 6 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-jsonschema/jsonschema#1538 · 1 commento ·
-
is_duration raises an uncaught decimal.Overflow on a duration with a large exponentForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. ApertaNeeds Test Upstream
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-jsonschema/jsonschema#1511 · 1 commento ·
-
regex format checker crashes with ValueError on conflicting inline flagsForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
python-jsonschema/jsonschema#1558 · 2 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
python-jsonschema/jsonschema#1547 ·
Tutte le issue di python-jsonschema/jsonschema
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 2 giorni
-
Status: Triage Type: Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
-
Objects.List treats versions=false (and similar flags) as trueForse già presa @andaaron l’ha presa oggi. Apertapriority: p2 type: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
googleapis/storage-testbench#829 ·
I maintainer di solito rispondono entro 2 giorni
-
[Bug]: memory provider treats 1:1 DMs as shared surfaces, so DM turns never receive reviewed recordsAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
rlaope/oh-my-hermes#2088 ·
I maintainer di solito rispondono entro 1 giorno