Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ocaml_protoc_plugin/deserialize_json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 .
Expand Down
11 changes: 11 additions & 0 deletions test/json_enum_initialization.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto2";

message A {
optional B.Kind kind = 1;
}

message B {
enum Kind {
ZERO = 0;
}
}
7 changes: 7 additions & 0 deletions test/json_enum_initialization_test.ml
Original file line number Diff line number Diff line change
@@ -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 |}]