Skip to content

Commit 6e44fe9

Browse files
committed
fix yojson: nested sum type was badly encoded
1 parent f8d5eb9 commit 6e44fe9

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/compilerlib/pb_codegen_encode_yojson.ml

+3-4
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,16 @@ let gen_rft_variant sc rf_label { Ot.v_constructors; _ } =
133133
let json_label =
134134
Pb_codegen_util.camel_case_of_constructor vc_constructor
135135
in
136-
F.linep sc "| %s v ->" vc_constructor;
137136
F.sub_scope sc (fun sc ->
138137
match vc_field_type with
139138
| Ot.Vct_nullary ->
140-
F.linep sc "(\"%s\", `Null) :: assoc" json_label
139+
F.linep sc "| %s -> (\"%s\", `Null) :: assoc" vc_constructor json_label
141140
| Ot.Vct_non_nullary_constructor field_type ->
142141
(match
143142
gen_field var_name json_label field_type vc_payload_kind
144143
with
145-
| None -> F.linep sc "(\"%s\", `Null) :: assoc" json_label
146-
| Some exp -> F.linep sc "%s :: assoc " exp)))
144+
| None -> F.linep sc "| %s -> (\"%s\", `Null) :: assoc" vc_constructor json_label
145+
| Some exp -> F.linep sc "| %s v -> %s :: assoc" vc_constructor exp)))
147146
v_constructors);
148147

149148
F.linep sc "in (* match v.%s *)" rf_label

src/tests/yojson/dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
(deps
99
(:file yojson_unittest.proto))
1010
(action
11-
(run %{project_root}/src/ocaml-protoc/ocaml_protoc.exe %{file} --yojson
11+
(run %{project_root}/src/ocaml-protoc/ocaml_protoc.exe %{file} --yojson --pp
1212
--ml_out=.)))

src/tests/yojson/yojson_unittest.proto

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ message AllBasicTypes {
2727
repeated string repeated14 = 34;
2828
}
2929

30+
message Empty {}
31+
3032
message SmallMessage {
3133
string sm_string = 1;
3234
}
@@ -74,3 +76,12 @@ message Test {
7476
repeated Enum repeated_enum = 10;
7577
repeated ABasicMessage basic = 11;
7678
}
79+
80+
message NestedUnit {
81+
oneof yolo {
82+
int32 x = 1;
83+
Empty empty = 2;
84+
}
85+
86+
string other = 3;
87+
}

0 commit comments

Comments
 (0)