Stranger6667 / jsonschema-rs

JSON Schema validation library
https://docs.rs/jsonschema
MIT License
482 stars 90 forks source link

[python] Panic on Python 3.12 #439

Closed aMarcireau closed 4 months ago

aMarcireau commented 8 months ago

jsonschema_rs.JSONSchema with Python 3.12 crashes the interpreter when parsing schemas with strings that contain 113 characters or more.

Steps to reproduce (works on Python < 3.12 but crashes on 3.12).

import jsonschema_rs

jsonschema_rs.JSONSchema(
    {
        "$schema": "https://json-schema.org/draft-07/schema",
        "$id": "https://github.com/Stranger6667/jsonschema-rs/blob/master/jsonschema/meta_schemas/draft2020-12/meta/applicator.js",
    }
)

The following snippet does not crash on 3.12 (same schema with fewer characters in "$id").

import jsonschema_rs

jsonschema_rs.JSONSchema(
    {
        "$schema": "https://json-schema.org/draft-07/schema",
        "$id": "https://github.com/Stranger6667/jsonschema-rs/blob/master/jsonschema/meta_schemas/draft2020-12/meta/applicator.j",
    }
)

Error printed when running the first snippet.

thread '<unnamed>' panicked at /Users/alex/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.100/src/ser.rs:2030:51:
byte index 112 is not a char boundary; it is inside '\0' (bytes 111..112) of `github.com/Stranger6667/jsonschema-rs/blob/master/jsonschema/meta_schemas/draft2020-12/meta/applicator.js?`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 190: invalid start byte
thread '<unnamed>' panicked at /Users/alex/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pyo3-0.19.1/src/err/mod.rs:788:5:
Python API call failed
fatal runtime error: failed to initiate panic, error 5
zsh: abort      python test.py
amw commented 7 months ago

I can confirm this. Does anyone have a hint for what should be investigated or upgraded?

Stranger6667 commented 7 months ago

We need to update the string definitions to accommodate changes from Python 3.12 - https://github.com/Stranger6667/jsonschema-rs/blob/master/bindings/python/src/string.rs

Or, better, reuse some definitions from PyO3. Similar changes in orjson

eirnym commented 5 months ago

I agree. Running test suite reveals the same.

INTERNALERROR>     JSONSchema.from_str('{"minimum": 5}').validate,
INTERNALERROR>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> ValueError: Invalid string: expected value at line 1 column 1
matemax commented 4 months ago

Any updates? using jsonschema-rs with version 3.12 is difficult. No releases with wheel for 3.12 and this bug. don't know rust and can't help with this issue. We can't upgrade python version in our project =(