Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

additionalItems can blow up when rendering a message for a non-homogeneous array #1157

Closed
michaelmior opened this issue Sep 5, 2023 · 4 comments
Labels
Bug Something doesn't work the way it should. Error Reporting Issues related to clearer or more robust validation error reporting

Comments

@michaelmior
Copy link

The extras_msg function sometimes throws an exception when everything in extras is not the same type because sorting fails.

Assuming the specific sort order is not terribly important, changing from

        return ", ".join(repr(extra) for extra in sorted(extras)), verb

to

        return ", ".join(extra for extra in sorted(map(repr, extras))), verb

should solve the problem.

@Julian
Copy link
Member

Julian commented Sep 5, 2023

Hi. Thanks. Can you include a full reproducing example?

@Julian Julian added the Waiting for Author A PR waiting for author changes or issue waiting on reporter feedback label Sep 5, 2023
@michaelmior
Copy link
Author

Unfortunately this was part of a larger job that failed and I didn't catch the specific input. I do however have a stack trace.

Traceback (most recent call last):
  File "/home/mmior/apps/json-fetcher/download.py", line 57, in <module>
    jsonschema.validate(instance=doc_obj, schema=schema)
  File "/home/mmior/.local/share/virtualenvs/json-fetcher-ZSn11VnQ/lib/python3.9/site-packages/jsonschema/validators.py", line 1305, in validate
    error = exceptions.best_match(validator.iter_errors(instance))
  File "/home/mmior/.local/share/virtualenvs/json-fetcher-ZSn11VnQ/lib/python3.9/site-packages/jsonschema/exceptions.py", line 444, in best_match
    best = next(errors, None)
  File "/home/mmior/.local/share/virtualenvs/json-fetcher-ZSn11VnQ/lib/python3.9/site-packages/jsonschema/validators.py", line 368, in iter_errors
    for error in errors:
  File "/home/mmior/.local/share/virtualenvs/json-fetcher-ZSn11VnQ/lib/python3.9/site-packages/jsonschema/_validators.py", line 57, in additionalProperties
    yield ValidationError(error % extras_msg(extras))
  File "/home/mmior/.local/share/virtualenvs/json-fetcher-ZSn11VnQ/lib/python3.9/site-packages/jsonschema/_utils.py", line 94, in extras_msg
    return ", ".join(repr(extra) for extra in sorted(extras)), verb
TypeError: '<' not supported between instances of 'str' and 'bool'

@davidrios
Copy link

davidrios commented Sep 5, 2023

I have the same issue.

Here's a minimal schema for reproduction:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "type": "object",
  "properties": {
    "array_multiple_types": {
      "oneOf": [
        {
          "type": "array",
          "items": [
            {
              "const": "test1"
            }
          ],
          "additionalItems": false,
          "minItems": 1
        },
        {
          "type": "array",
          "items": [
            {
              "const": "test2"
            },
            {
              "type": "string"
            },
            {
              "type": "number"
            }
          ],
          "additionalItems": false,
          "minItems": 3
        }
      ]
    }
  }
}

and an instance to be validated:

{
  "array_multiple_types": [
    "test2",
    "aaa",
    99
  ]
}

Although the proposed solution does fix the problem, my first instinct would be to not sort a list if I'm not sure it's homogenous. Sorting also makes the error message slightly more confusing as it shows elements in a different ordering from the validated instance. I would just remove it.

return ", ".join(repr(extra) for extra in extras), verb

Thanks for the project BTW!

@Julian Julian added Bug Something doesn't work the way it should. and removed Waiting for Author A PR waiting for author changes or issue waiting on reporter feedback labels Sep 8, 2023
@Julian
Copy link
Member

Julian commented Sep 8, 2023

Thanks, that helps!

@Julian Julian added the Error Reporting Issues related to clearer or more robust validation error reporting label Sep 8, 2023
@Julian Julian changed the title ValueError on exception generation additionalItems can blow up when rendering a message for a non-homogeneous array Sep 9, 2023
Julian added a commit to json-schema-org/JSON-Schema-Test-Suite that referenced this issue Oct 20, 2023
@Julian Julian closed this as completed in 8cff13d Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something doesn't work the way it should. Error Reporting Issues related to clearer or more robust validation error reporting
Projects
None yet
Development

No branches or pull requests

3 participants