Comp-490-SeniorProject / site

MIT License
0 stars 1 forks source link

Fix encoding/decoding for OrjsonField #61

Closed MarkKoz closed 2 years ago

MarkKoz commented 2 years ago

The encoder and decoder are expected to be objects following the JSONEncoder and JSONDecoder interfaces, respectively. Thus, the orjson dumps and loads functions cannot be used directly.

Define JSONEncoder and JSONDecoder subclasses which wrap orjson, and use these as the encoder and decoder for OrjsonField.

This field was only used by the /api/notification_settings/ settings endpoint, so that endpoint should now be able to work. Previously, it always reported a JSON validation error for the condition field. To test it, the following code can be used

(async () => {
    let response = await fetch(
        "/api/notification_settings/",
        {
            method: "POST",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'X-CSRFToken': getCookie('csrftoken'),
            },
            mode: 'same-origin',
            body: JSON.stringify({
                condition: {"var": 1},
                destination: ["EM", "WE"],
                test: 1, // This test needs to be created beforehand
                message: "hello"
            }),
        }
    );
    let content = await response.json();
    console.log(content);
})();