-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
key not found when encoding optional fields #126
Comments
Thank you for the report! I can reproduce this problem. However, you wrote that you observed this problem also with binary encoding, which I could not reproduced. Do you have a minimal example for this? |
Thank you @ahamez 💚 💙 💜 I haven't managed to produce a test case for the binary case yet but I will keep trying. I see the error when I generate code and then convert a binary stream coming from a c# implementation using the same proto file. I can't share either the proto or the binary due to it being a work thing. |
I think I might have fixed this. Could you try the branch fix_optional to check if it resolves your issue? Thank you :-) |
Thank you @ahamez that has fixed the JSON problem. It hasn't helped the binary decode though. I'll try and produce a concrete test case for binary as soon as I can. |
Hi @ahamez, I have got a test case for the binary decode error. defmodule OptionalTest do
use ExUnit.Case
use Protox,
schema: """
syntax = "proto3";
message Msg1 {
Msg2 foo = 1;
optional Msg2 bar = 2;
}
message Msg2 {
int32 buz = 1;
}
"""
test "non-optional = nil, optional = nil" do
msg1 = %Msg1{foo: nil, bar: nil}
assert msg1 |> Msg1.encode!() |> :binary.list_to_bin() |> Msg1.decode!() == msg1
end
test "non-optional = Msg2, optional = nil" do
msg1 = %Msg1{foo: %Msg2{}, bar: nil}
assert msg1 |> Msg1.encode!() |> :binary.list_to_bin() |> Msg1.decode!() == msg1
end
test "non-optional = nil, optional = Msg2" do
msg1 = %Msg1{foo: nil, bar: %Msg2{}}
assert msg1 |> Msg1.encode!() |> :binary.list_to_bin() |> Msg1.decode!() == msg1
end
test "non-optional = Msg2, optional = Msg2" do
msg1 = %Msg1{foo: %Msg2{}, bar: %Msg2{}}
assert msg1 |> Msg1.encode!() |> :binary.list_to_bin() |> Msg1.decode!() == msg1
end
end
It appears to be linked to an optional field being a message. We first noticed it with optional Thanks so much for looking at this. |
Thank your for this test case! You're most probably right, it must be linked to optional nested messages. |
I was easier than I thought 😁. Could you try with the latest commit of the branch |
Amazing, thank you again @ahamez. That has solved all our decoding issues. There is one last thing that might be linked to this. When I compile generated code for a proto that has one of these optional messages there is a compiler warning. Another test case for you: In the syntax = "proto3";
message Msg1 {
Msg2 foo = 1;
optional Msg2 bar = 2;
}
message Msg2 {
int32 buz = 1;
} and run these commands mkdir -p lib/optional
mix protox.generate --output-path=./lib/optional --multiple-files ./optional_test/optional.proto
mix compile and then you should see:
I think this is linked to the same thing. I think there is an extra underscore in the function name |
Thanks for the test case! Just to give you an idea of why there is this extra message Msg1 {
optional int32 foo = 1;
} is exactly the same as: message Msg2 {
oneof _foo {
int32 foo = 1;
}
} I'll try to suppress this warning this week-end 🙂. |
It's now fixed in 1.7.1. Thank you for your report and all your test cases, they were very useful! |
Describe the bug
When using optional fields there seems to be somekind of problem with the naming:
To Reproduce
I wrote this test case to show the issue
output:
Expected behavior
The struct with the optional field is encoded.
Environment (please complete the following information):
Additional context
This is a minimal reproduction. I've observed in binary decode as well as json_decode. Linked to #49/#50.
The text was updated successfully, but these errors were encountered: