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

fix(828): ensuring valid JSON response from REST API #831

Merged
merged 5 commits into from
Mar 14, 2024

Conversation

gabrik
Copy link
Contributor

@gabrik gabrik commented Mar 14, 2024

This PR ensures that the response of the REST plugin is a valid JSON, Closes #828

If a sample has application/json or text/json or text/json5 encoding then we try to convert it to a serde_json::Value if we fail then can be converted to a string via the from_utf8 function then we return its payload as JSON string, otherwise it is encoded as base64 and return as JSON string.
If the sample has a different encoding and can be converted to a string via the from_utf8 function then we return its payload as JSON string , otherwise it is encoded as base64 and return as JSON string.

This is an example of a get with the different cases:

[
    // real json
    {
        "key": "demo/temp/json",
        "value": {
            "hello": "world",
            "this": {
                "is-a": "json"
            }
        },
        "encoding": "application/json",
        "time": "2024-03-14T13:36:57.440128661Z/ff29c02c6c1044c6a1b565c24566b760"
    },
    // empty json
    {
        "key": "demo/temp/empty",
        "value": null,
        "encoding": "application/json",
        "time": "2024-03-14T13:35:31.929630711Z/ff29c02c6c1044c6a1b565c24566b760"
    },
    // text that was inserted as it was a json
    {
        "key": "demo/temp/text",
        "value": "this is a text, but i said it is a json",
        "encoding": "application/json",
        "time": "2024-03-14T13:35:49.283543717Z/ff29c02c6c1044c6a1b565c24566b760"
    },
    // binary, image in this case, that was inserted as json
    {
        "key": "demo/temp/binary",
        "value": "/9j/4AAQSkZJRgABAQAAAAAAAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/wAALCAAgACABAREA/8QAFQABAQAAAAAAAAAAAAAAAAAAAAn/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAA/AKpgAAAAP//Z",
        "encoding": "application/json",
        "time": "2024-03-14T13:41:08.952893450Z/ff29c02c6c1044c6a1b565c24566b760"
    },
    // image inserted as image
        {
        "key": "demo/temp/image2",
        "value": "/9j/4AAQSkZJRgABAQAAAAAAAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/wAALCAAgACABAREA/8QAFQABAQAAAAAAAAAAAAAAAAAAAAn/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAA/AKpgAAAAP//Z",
        "encoding": "image/jpeg",
        "time": "2024-03-14T13:46:20.142042767Z/ff29c02c6c1044c6a1b565c24566b760"
    }
]

Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
@eclipse-zenoh-bot
Copy link
Contributor

@gabrik If this pull request contains a bugfix or a new feature, then please consider using Closes #ISSUE-NUMBER syntax to link it to an issue.

1 similar comment
@eclipse-zenoh-bot
Copy link
Contributor

@gabrik If this pull request contains a bugfix or a new feature, then please consider using Closes #ISSUE-NUMBER syntax to link it to an issue.

Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
@gabrik gabrik requested review from JEnoch and Mallets March 14, 2024 13:44
@eclipse-zenoh-bot
Copy link
Contributor

@gabrik If this pull request contains a bugfix or a new feature, then please consider using Closes #ISSUE-NUMBER syntax to link it to an issue.

@gabrik gabrik marked this pull request as ready for review March 14, 2024 14:20
}

fn payload_to_string(payload: &Payload) -> String {
String::from_utf8(payload.contiguous().to_vec()).unwrap_or(base64_encode(&payload.contiguous()))
Copy link
Member

@Mallets Mallets Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using the .deserialize()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even

pub enum StringOrBase64 {
?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't knew about it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated code to use StringOrBase64::from and then dereferecing to String.

A clone() becomes necessary but should be fine.

Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
@gabrik gabrik requested a review from Mallets March 14, 2024 14:50
Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
}

fn payload_to_json(payload: Payload, encoding: &Encoding) -> serde_json::Value {
match payload.len() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust prefers the usage of is_empty() rather than checking if the len() == 0

Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
@Mallets Mallets merged commit 4d8ec6c into protocol_changes Mar 14, 2024
15 checks passed
@Mallets Mallets deleted the fix/828 branch March 14, 2024 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants