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

Handling of Null or Empty Fields in JSON Serialization for gRPC Responses #1463

Closed
cubxxw opened this issue Nov 22, 2023 — with Slack · 5 comments
Closed

Handling of Null or Empty Fields in JSON Serialization for gRPC Responses #1463

cubxxw opened this issue Nov 22, 2023 — with Slack · 5 comments
Assignees
Labels
bug Categorizes issue or PR as related to a bug. feature Categorizes issue or PR as related to a new feature. good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines.
Milestone

Comments

Copy link
Contributor

cubxxw commented Nov 22, 2023

Problem Description

In our current implementation using protocol buffers with gRPC in Go, we've encountered an issue where JSON serialization omits fields with null or empty values due to the omitempty option. This behavior is not always desirable, especially when the client expects all fields, regardless of whether they hold a value or not.

Impact

This behavior can lead to inconsistent API responses and potential misunderstandings on the client side, as the client might expect all defined fields to be present in the response.

Possible Solution

We are looking for a solution that allows us to include all defined fields in the JSON response, even if their value is null or empty, without modifying the default behavior set by the omitempty option in the protocol buffer compiler for Go.

Considerations

  • We would like to maintain the integrity of the protocol buffer specifications and avoid custom plugins if possible.
  • It's essential that the solution aligns with best practices for JSON serialization in gRPC while ensuring backward compatibility.

References

We've noted that similar issues have been discussed in the context of the Go-Kratos framework (e.g., go-kratos/kratos#1539 and go-kratos/kratos#1952). We're looking for advice or solutions that may have been identified in these or other discussions.

Request

We kindly request guidance on how to approach this serialization concern while adhering to the gRPC and protocol buffer standards in Go. Any suggestions or recommendations for practices or tools to resolve this issue would be greatly appreciated.

@cubxxw cubxxw added good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. bug Categorizes issue or PR as related to a bug. feature Categorizes issue or PR as related to a new feature. labels Nov 22, 2023 — with Slack
@cubxxw cubxxw added this to the v3.5 milestone Nov 22, 2023 — with Slack
@kubbot
Copy link
Contributor

kubbot commented Nov 22, 2023

This issue is available for anyone to work on. Make sure to reference this issue in your pull request. ✨ Thank you for your contribution! ✨
Join slack 🤖 to connect and communicate with our developers.
If you wish to accept this assignment, please leave a comment in the comments section: /accept.🎯

@kvii
Copy link
Contributor

kvii commented Nov 22, 2023

apiresp.ApiSuccess can use this code:

var marshalOptions = protojson.MarshalOptions{
	EmitUnpopulated: true,
	UseEnumNumbers:  true,
}

func ApiSuccess(data any) *ApiResponse {
	if format, ok := data.(ApiFormat); ok {
		format.ApiFormat()
	}
	if isAllFieldsPrivate(data) {
		return &ApiResponse{}
	}
	if m, ok := data.(proto.Message); ok {
		b, err := marshalOptions.Marshal(m)
		if err == nil {
			return &ApiResponse{
				Data: json.RawMessage(b),
			}
		}
	}

	return &ApiResponse{
		Data: data,
	}
}

Note: protojson marshal int64 field to string (1 -> "1"). Cause json not support number > 2^53. We can use double type in field to avoid this.

@kubbot
Copy link
Contributor

kubbot commented Jan 22, 2024

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@kubbot
Copy link
Contributor

kubbot commented Feb 2, 2024

This issue was closed because it has been stalled for 7 days with no activity.

@kubbot kubbot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 2, 2024
@cubxxw cubxxw reopened this Mar 18, 2024
@kubbot
Copy link
Contributor

kubbot commented Jul 1, 2024

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Categorizes issue or PR as related to a bug. feature Categorizes issue or PR as related to a new feature. good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines.
Projects
None yet
Development

No branches or pull requests

4 participants