dice icon indicating copy to clipboard operation
dice copied to clipboard

Enforce value to be stored / returned as `json.marshal` in `set` cmd via Http protocol

Open vinitparekh17 opened this issue 1 year ago • 7 comments

Steps to reproduce

curl --location 'http://localhost:8082/set' \
--header 'Content-Type: application/json' \
--header 'Cache-Control: no-cache' \
--data '{
    "key": "foo",
    "value": {
        "foo": {
            "bar": "fooBar"
        }
    }
}'


curl --location 'http://localhost:8082/get' \
--header 'Content-Type: application/json' \
--header 'Cache-Control: no-cache' \
--data '{
    "key": "foo"
}'

Expected output

"{"foo":{"bar":"fooBar"}}"

Observed output

"map[foo:map[bar:fooBar]]"

Suggestion

Please ensure that the value is properly serialized using json.Marshal before being sent as a response in the HTTP protocol.

Another Solution (Not Recommended, but Supported by Redis)

Redis does allow storing JSON-like values using the SET command. If clients attempt to set a key with a JSON object directly, they will encounter an error due to invalid arguments.

To store JSON values, clients must manually stringify the value before passing it to the SET command.

If a client provides a JSON object without stringifying it, the server should return an error indicating invalid arguments. It's generally advisable to use a JSON.SET for handling JSON data.

  • this should be expected from Dice via HTTP protocol

Expectations for resolution

This issue will be considered resolved when the following things are done

  1. changes in the dice code to meet the expected behavior
  2. addition of relevant test case to ensure we catch the regression

You can find the tests under the tests directory of the dice repository and the steps to run are in the README file. Refer to the following links to set up DiceDB and Redis 7.2.5 locally

vinitparekh17 avatar Sep 27 '24 07:09 vinitparekh17

can i take this up ?

rishavvajpayee avatar Sep 27 '24 10:09 rishavvajpayee

Hey, I am fairly new to this project and I want to take this opportunity to get into contributing to DiceDB. Can I take up this task and return with a PR?

TheMonkDev avatar Sep 27 '24 11:09 TheMonkDev

In expected response we need a json response or a json string?

 {
     "foo": {
         "bar": 1
     }
 } 
 

or

"{"foo":{"bar":"fooBar"}}"

lovish2525 avatar Sep 27 '24 13:09 lovish2525

In expected response we need a json response or a json string?

 {
     "foo": {
         "bar": 1
     }
 } 
 

or

"{"foo":{"bar":"fooBar"}}"

strigified response 2nd one

vinitparekh17 avatar Sep 27 '24 16:09 vinitparekh17

@vinitparekh17 Is it fine if the stringified version has escape characters in response?

lovish2525 avatar Sep 27 '24 16:09 lovish2525

Created a PR here - https://github.com/DiceDB/dice/pull/757/files

lovish2525 avatar Sep 27 '24 17:09 lovish2525

cc: @lucifercr07

vinitparekh17 avatar Sep 28 '24 05:09 vinitparekh17