Greedy (?) object typing leads to semantically wrong error message
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 35/100
Línea de trabajo
Reproduce el esquema y los datos del issue mediante jsonschema.validate y, después, inspecciona la ruta de validación en validators.py. Compara cómo las ramas de anyOf producen el ValidationError; done debe significar que los datos de entidad no válidos informan de BREAK_ENTITY sin entity_property, con cobertura para los casos de orden de objetos demostrados.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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'}
- Lenguaje dominante
- Python
- Estrellas
- 5k
- Forks
- 694
- Merge medio
- 1 d 20 h
- PR fusionados (30 d)
- 6
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de python-jsonschema/jsonschema
-
Iterator error contexts lose parent links and absolute pathsPosiblemente ocupada @DawnofGenX la tomó hace 5 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
python-jsonschema/jsonschema#1584 · 3 comentarios ·
-
regex format checker crashes with RecursionError on deeply nested patternsPosiblemente ocupada @DawnofGenX la tomó hace 5 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
python-jsonschema/jsonschema#1538 · 1 comentario ·
-
is_duration raises an uncaught decimal.Overflow on a duration with a large exponentQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. AbiertoNeeds Test Upstream
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
python-jsonschema/jsonschema#1511 · 1 comentario ·
-
regex format checker crashes with ValueError on conflicting inline flagsPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
python-jsonschema/jsonschema#1558 · 2 comentarios ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
python-jsonschema/jsonschema#1547 ·
Todos los issues de python-jsonschema/jsonschema
Issues similares
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 60/100
521xueweihan/HelloGitHub#3924 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
wilbowes/EchoMuse#869 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
-
Claiming namespace `jft63`Abiertonamespace operations
Dificultad 1/5 Menos de una hora Aptitud para principiantes 72/100
EclipseFdn/open-vsx.org#14043 ·
Los mantenedores suelen responder en 1 día
-
test: TestServeUntilStale races the server's close against the client's sendall (BrokenPipeError under load)Posiblemente ocupada @evoludigit la tomó hoy. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 89/100
Los mantenedores suelen responder en 1 día