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

jsonpb: unexpected output for small negative google.protobuf.Duration #883

Closed
AlekSi opened this issue Jun 28, 2019 · 4 comments · Fixed by #885
Closed

jsonpb: unexpected output for small negative google.protobuf.Duration #883

AlekSi opened this issue Jun 28, 2019 · 4 comments · Fixed by #885
Assignees
Labels

Comments

@AlekSi
Copy link

AlekSi commented Jun 28, 2019

What version of protobuf and what language are you using?
Version: v1.3.1

What did you do?
https://github.com/AlekSi/go-bug/tree/master/jsonpb

func TestDuration(t *testing.T) {
	d := -time.Nanosecond
	dp := ptypes.DurationProto(d)
	m := &jsonpb.Marshaler{}
	s, err := m.MarshalToString(dp)
	if err != nil {
		t.Fatal(err)
	}
	if s != `"-0.00000001s"` {
		t.Errorf("Unexpected result: %s", s)
	}
}

What did you expect to see?
"-0.00000001s"

What did you see instead?
Unexpected result: "0.-00000001s"

@dsnet
Copy link
Member

dsnet commented Jun 28, 2019

Thank you for the report.

@cybrcodr, can you confirm whether this was fixed in v2?

@dsnet dsnet changed the title Unexpected jsonpb output for small negative google.protobuf.Duration jsonpb: unexpected output for small negative google.protobuf.Duration Jun 28, 2019
@dsnet
Copy link
Member

dsnet commented Jun 28, 2019

Also, if the fix is simple, it may be doing in v1 as well.

@cybrcodr
Copy link
Contributor

This is fixed in v2.

Minor, I think the posted sample code is comparing with slightly wrong value, a negative nanosecond is "-0.000000001s" (8 zeroes).

The bug in V1 is that it puts the negative sign after the decimal. Yeah, I can look into fixing this for v1. Logic can be carried over from --

https://go.googlesource.com/protobuf/+/refs/heads/master/encoding/protojson/well_known_types.go#651

cybrcodr added a commit that referenced this issue Jul 1, 2019
Fix output for negative nanoseconds with zero second.
Add validation on min/max seconds.

Fixes #883.
@AlekSi
Copy link
Author

AlekSi commented Jul 2, 2019

Thank you. Do you plan to release v1.3.2 any time soon?

thaJeztah added a commit to thaJeztah/swarmkit that referenced this issue Oct 20, 2019
full diff: golang/protobuf@v1.2.0...v1.3.2

protobuf v1.3.2:

- golang/protobuf#785: grpc code generation: add an UnimplementedServer type implementing each server interface, returning an unimplemented error for each method
- golang/protobuf#851: convert prints to os.Stderr to use log.Printf
- golang/protobuf#883: jsonpb: fix marshaling of Duration with negative nanoseconds

protobuf v1.3.1:

- The set of dependencies specified in go.mod has now been reduced to only the standard library.

protobuf v1.3.0:

- golang/protobuf#699: add a go.mod module file
- golang/protobuf#701: stop generating package "// import" comment
- golang/protobuf#741: deprecate {Unm,M}arshalMessageSet{JSON}
- golang/protobuf#760: different internal implementation of oneofs
    - `.pb.go` files generated by [email protected] will require the [email protected] package to work
- various minor changes to code generation

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants