Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Greedy (?) object typing leads to semantically wrong error message

Abierto
#812 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
tooling

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

Enhancement Error Reporting

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.

  • jsonschema version: 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

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python-jsonschema/jsonschema

Todos los issues de python-jsonschema/jsonschema

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.