This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch xuanwo/fix-lua-write-binary-bytes
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 65ca205f119e3b3f3900edef327aeca018ecab14
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 17:01:16 2026 +0100

    fix(bindings/lua): accept binary bytes in operator write
    
    `operator_write` converted its payload argument to `String`, so mlua
    rejected any byte sequence that is not valid UTF-8. Lua strings are raw
    byte sequences and `operator_read` already returns `LuaString`, so
    `op:write("a", op:read("b"))` and writes of arbitrary binary payloads
    failed with "invalid utf-8 sequence".
    
    Take the payload as `LuaString` and pass `as_bytes()` to the operator.
    Path arguments stay `String` because OpenDAL paths must be UTF-8.
    
    Add Lua tests covering a binary payload with embedded NUL bytes, a write
    of bytes returned by `read`, and unchanged UTF-8 text handling.
---
 bindings/lua/src/lib.rs            |  9 ++++++--
 bindings/lua/test/opendal_test.lua | 42 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/bindings/lua/src/lib.rs b/bindings/lua/src/lib.rs
index 94ec7020d3..87bb4df9f5 100644
--- a/bindings/lua/src/lib.rs
+++ b/bindings/lua/src/lib.rs
@@ -110,7 +110,12 @@ fn operator_delete(_: &Lua, (operator, path): (LuaTable, 
String)) -> LuaResult<(
     }
 }
 
-fn operator_write(_: &Lua, (operator, path, bytes): (LuaTable, String, 
String)) -> LuaResult<()> {
+// `bytes` must stay `LuaString`: Lua strings are raw byte sequences and may
+// contain arbitrary binary data, which `String` rejects as invalid UTF-8.
+fn operator_write(
+    _: &Lua,
+    (operator, path, bytes): (LuaTable, String, LuaString),
+) -> LuaResult<()> {
     let op = operator.get::<ODOperator>("_operator")?;
     let op = op.operator;
 
@@ -119,7 +124,7 @@ fn operator_write(_: &Lua, (operator, path, bytes): 
(LuaTable, String, String))
     }
 
     let path = path.as_str();
-    let res = op.write(path, bytes);
+    let res = op.write(path, bytes.as_bytes().to_vec());
     match res {
         Ok(_) => Ok(()),
         Err(e) => Err(LuaError::external(e)),
diff --git a/bindings/lua/test/opendal_test.lua 
b/bindings/lua/test/opendal_test.lua
index 834c9fa415..35b52cdedc 100644
--- a/bindings/lua/test/opendal_test.lua
+++ b/bindings/lua/test/opendal_test.lua
@@ -84,4 +84,46 @@ describe("opendal unit test", function()
       assert.are.equal(res, false)
     end)
   end)
+  describe("opendal binary payload", function()
+    -- Lua strings are raw byte sequences, so write must accept bytes that are
+    -- not valid UTF-8, including embedded NUL bytes.
+    it("writes and reads binary bytes", function()
+      local op, err = opendal.operator.new("memory",{root="/tmp"})
+      assert.is_nil(err)
+
+      local payload = "\0\255\128bin"
+      assert.is_nil(op:write("binary.bin", payload))
+
+      local data, read_err = op:read("binary.bin")
+      assert.is_nil(read_err)
+      assert.are.equal(#data, #payload)
+      assert.are.equal(data, payload)
+    end)
+    it("writes bytes returned by read", function()
+      local op, err = opendal.operator.new("memory",{root="/tmp"})
+      assert.is_nil(err)
+
+      local payload = "\0\255\128\0\1\2\253\254"
+      assert.is_nil(op:write("source.bin", payload))
+
+      local copied, read_err = op:read("source.bin")
+      assert.is_nil(read_err)
+      assert.is_nil(op:write("copy.bin", copied))
+
+      local data, copy_err = op:read("copy.bin")
+      assert.is_nil(copy_err)
+      assert.are.equal(data, payload)
+    end)
+    it("writes utf-8 text unchanged", function()
+      local op, err = opendal.operator.new("memory",{root="/tmp"})
+      assert.is_nil(err)
+
+      local text = "hello world, \228\189\160\229\165\189"
+      assert.is_nil(op:write("text.txt", text))
+
+      local data, read_err = op:read("text.txt")
+      assert.is_nil(read_err)
+      assert.are.equal(data, text)
+    end)
+  end)
 end)

Reply via email to