Skip to content
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

ProtoReflect().GetUnknown() Not Working #1618

Open
frankgreco opened this issue May 30, 2024 · 4 comments
Open

ProtoReflect().GetUnknown() Not Working #1618

frankgreco opened this issue May 30, 2024 · 4 comments

Comments

@frankgreco
Copy link

What version of protobuf and what language are you using?
v1.5.4

What did you do?

func TestUnknownFields(t *testing.T) {
	request := new(helloworld.HelloRequest)

	err := (&protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal([]byte(
		`{"foo":"bar"}`,
	), request)

	assert.NoError(t, err)
	assert.Equal(t, 1, len(request.ProtoReflect().GetUnknown()))
}

What did you expect to see?
I expected the assertions to pass and there to be one unknown item.

What did you see instead?
The last assertion failed. The actual length is 0.

@puellanivis
Copy link
Collaborator

// If DiscardUnknown is set, unknown fields and enum name values are ignored.

You’re explicitly telling protojson to discard those unknown values.

@frankgreco
Copy link
Author

@puellanivis that field has no impact on the length of request.ProtoReflect().GetUnknown(). I get the same result regardless of the value.

@puellanivis
Copy link
Collaborator

This seems related to: #1390

Right now, there’s no defined way to know the wire format for these unknown JSON fields, so they cannot be rendered into the GetUnknown() response.

@neild
Copy link
Contributor

neild commented May 31, 2024

Unknown fields are only preserved in the binary encoding.

Setting DiscardUnknown tells protojson.Unmarshal to discard unknown fields (as the name says). If you don't set DiscardUnknown, protojson.Unmarshal will return an error if it encounters any field names it does not recognize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants