Skip to content

Commit

Permalink
Fix encoding body json object back
Browse files Browse the repository at this point in the history
The method was marked private and wrong method from `IntoLua` trait was
used instead.
  • Loading branch information
khvzak committed Jun 7, 2024
1 parent 4e5fea9 commit 27d190d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
10 changes: 8 additions & 2 deletions casper-server/src/lua/http/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::{Duration, Instant};

use futures::{Stream, TryStreamExt};
use mlua::{
AnyUserData, Error as LuaError, ErrorContext as _, ExternalError, FromLua, IntoLua, Lua,
AnyUserData, Error as LuaError, ErrorContext as _, ExternalError, FromLua, Lua,
OwnedAnyUserData, Result as LuaResult, String as LuaString, UserData, Value,
};
use ntex::http::body::{self, BodySize, BoxedBodyStream, MessageBody, ResponseBody, SizedStream};
Expand Down Expand Up @@ -453,7 +453,7 @@ mod tests {
use std::io::{Error as IoError, ErrorKind};
use std::time::Duration;

use mlua::{chunk, Lua, Result as LuaResult};
use mlua::{chunk, Lua, Result as LuaResult, Value};
use ntex::http::body::BoxedBodyStream;
use tokio_stream::{self as stream, StreamExt};

Expand Down Expand Up @@ -700,12 +700,18 @@ mod tests {
let lua = Lua::new();
super::super::super::bytes::register_types(&lua)?;

lua.globals().set(
"json_encode",
lua.create_function(|_, value: Value| Ok(serde_json::to_string(&value).unwrap()))?,
)?;

let body = LuaBody::from(r#"{"hello": "world"}"#);
lua.load(chunk! {
local json = $body:json()
assert(typeof(json) == "JsonObject", "variable is not JsonObject")
assert(json.hello == "world", "`json.hello` is not 'world'")
assert($body:json() ~= nil, "`json()` method must not consume body")
assert(json_encode(json) == "{\"hello\":\"world\"}", "`body_json` must be encoded correctly")
})
.exec_async()
.await
Expand Down
9 changes: 7 additions & 2 deletions casper-server/src/lua/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ mod tests {

lua.globals()
.set("Response", lua.create_proxy::<LuaResponse>()?)?;
lua.globals().set(
"json_encode",
lua.create_function(|_, value: Value| Ok(serde_json::to_string(&value).unwrap()))?,
)?;

// Check JSON parsing
lua.load(chunk! {
Expand All @@ -527,8 +531,9 @@ mod tests {
},
body = "{\"hello\": \"world\"}",
})
local json = resp:body_json()
assert(json.hello == "world", "`json.hello` must be 'world'")
local body_json = resp:body_json()
assert(body_json.hello == "world", "`json.hello` must be 'world'")
assert(json_encode(body_json) == "{\"hello\":\"world\"}", "`body_json` must be encoded correctly")
})
.exec_async()
.await
Expand Down
2 changes: 1 addition & 1 deletion casper-server/src/lua/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl JsonObject {
}

/// Converts this `JsonObject` into a Lua `Value`.
fn into_lua(self, lua: &Lua) -> Result<Value> {
pub(crate) fn into_lua(self, lua: &Lua) -> Result<Value> {
match self.current() {
serde_json::Value::Null => Ok(Value::NULL),
serde_json::Value::Bool(b) => Ok(Value::Boolean(*b)),
Expand Down

0 comments on commit 27d190d

Please sign in to comment.