diff --git a/src/ocaml_protoc_plugin/deserialize_json.ml b/src/ocaml_protoc_plugin/deserialize_json.ml index 26d077d..6f2e6d0 100644 --- a/src/ocaml_protoc_plugin/deserialize_json.ml +++ b/src/ocaml_protoc_plugin/deserialize_json.ml @@ -309,8 +309,8 @@ let read_value: type a b. (a, b) spec -> Json.t -> a = function | String -> to_string | Bytes -> to_bytes | Enum (module Enum) -> - let map_enum_json = map_enum_json (module Enum) in - fun json -> map_enum_json json |> to_enum (module Enum) + let map_enum_json = Lazy.from_fun (fun () -> map_enum_json (module Enum)) in + fun json -> Lazy.force map_enum_json json |> to_enum (module Enum) | Message (module Message) -> Message.from_json_exn diff --git a/test/dune b/test/dune index 21e92b2..d74ec12 100644 --- a/test/dune +++ b/test/dune @@ -51,7 +51,7 @@ included2.ml included3_dash.ml service.ml recursive.ml recursive2.ml protocol.ml name_clash.ml name_clash_mangle.ml proto2.ml packed.ml mangle_names.ml extensions.ml options.ml name_clash2.ml empty.ml service_rpc_clash.ml service_empty_package.ml - deprecated.ml json_encoding.ml comments.ml) + deprecated.ml json_encoding.ml comments.ml json_enum_initialization.ml) (deps (:plugin %{bin:protoc-gen-ocaml}) (:proto @@ -61,7 +61,7 @@ recursive.proto recursive2.proto protocol.proto name_clash.proto name_clash_mangle.proto proto2.proto packed.proto mangle_names.proto extensions.proto options.proto name_clash2.proto empty.proto service_rpc_clash.proto service_empty_package.proto - deprecated.proto json_encoding.proto comments.proto) + deprecated.proto json_encoding.proto comments.proto json_enum_initialization.proto) ) (action (run %{bin:protoc} -I %{read-lines:google_include} -I . diff --git a/test/json_enum_initialization.proto b/test/json_enum_initialization.proto new file mode 100644 index 0000000..bac7e31 --- /dev/null +++ b/test/json_enum_initialization.proto @@ -0,0 +1,11 @@ +syntax = "proto2"; + +message A { + optional B.Kind kind = 1; +} + +message B { + enum Kind { + ZERO = 0; + } +} diff --git a/test/json_enum_initialization_test.ml b/test/json_enum_initialization_test.ml new file mode 100644 index 0000000..1184d6d --- /dev/null +++ b/test/json_enum_initialization_test.ml @@ -0,0 +1,7 @@ +open Json_enum_initialization + +let%expect_test "JSON decoder initialization with a cross-message enum reference" = + let decoded = A.from_json_exn (`Assoc ["kind", `String "ZERO"]) in + let expected = A.make ~kind:B.Kind.ZERO () in + Printf.printf "%b\n" (A.equal decoded expected); + [%expect {| true |}]