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

Do not fail on unmarshal protobuf Any #94

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mocks/operations/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (s Stub) Raw() *commonpb.Operation {
}

func unmarshalNotEmpty(t require.TestingT, message *anypb.Any) proto.Message {
if message == nil || message.MessageIs(&emptypb.Empty{}) {
if message.GetTypeUrl() == "" || message.MessageIs(&emptypb.Empty{}) {
return nil
}

Expand Down
30 changes: 12 additions & 18 deletions operations/alphaops/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,33 +224,27 @@ func validateAndUnmarshal(operation *v1alpha1.Operation) (unmarshalled, error) {
}
}

msgs.request, err = unmarshalNotEmpty(operation.GetRequest())
if err != nil {
errs = errors.Join(errs, fmt.Errorf("request: %w", err))
}
msgs.request = unmarshalNotEmpty(operation.GetRequest())
msgs.resource = unmarshalNotEmpty(operation.GetResource())
msgs.progressData = unmarshalNotEmpty(operation.GetProgressData())

msgs.requestHeaders = http.Header{}
for name, header := range operation.GetRequestHeaders() {
msgs.requestHeaders[name] = header.GetValues()
}

msgs.resource, err = unmarshalNotEmpty(operation.GetResource())
if err != nil {
errs = errors.Join(errs, fmt.Errorf("resource: %w", err))
}

msgs.progressData, err = unmarshalNotEmpty(operation.GetProgressData())
if err != nil {
errs = errors.Join(errs, fmt.Errorf("progress_data: %w", err))
}

return msgs, errs
}

func unmarshalNotEmpty(operation *anypb.Any) (proto.Message, error) {
if operation == nil || operation.MessageIs(&emptypb.Empty{}) {
return nil, nil //nolint:nilnil // don't want to introduce a sentinel error in private method
func unmarshalNotEmpty(message *anypb.Any) proto.Message {
if message.GetTypeUrl() == "" || message.MessageIs(&emptypb.Empty{}) {
return nil
}

res, err := message.UnmarshalNew()
if err != nil {
return nil // ignore the error to be compatible with newer services that may send unknown messages inside Any
}

return operation.UnmarshalNew()
return res
}
24 changes: 11 additions & 13 deletions operations/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,28 +297,26 @@ func validateAndUnmarshal(operation *common.Operation) (unmarshalled, error) {
}
}

msgs.request, err = unmarshalNotEmpty(operation.GetRequest())
if err != nil {
errs = errors.Join(errs, fmt.Errorf("request: %w", err))
}
msgs.request = unmarshalNotEmpty(operation.GetRequest())
msgs.progressData = unmarshalNotEmpty(operation.GetProgressData())

msgs.requestHeaders = http.Header{}
for name, header := range operation.GetRequestHeaders() {
msgs.requestHeaders[name] = header.GetValues()
}

msgs.progressData, err = unmarshalNotEmpty(operation.GetProgressData())
if err != nil {
errs = errors.Join(errs, fmt.Errorf("progress_data: %w", err))
}

return msgs, errs
}

func unmarshalNotEmpty(message *anypb.Any) (proto.Message, error) {
if message == nil || message.MessageIs(&emptypb.Empty{}) {
return nil, nil //nolint:nilnil // don't want to introduce a sentinel error in private method
func unmarshalNotEmpty(message *anypb.Any) proto.Message {
if message.GetTypeUrl() == "" || message.MessageIs(&emptypb.Empty{}) {
return nil
}

res, err := message.UnmarshalNew()
if err != nil {
return nil // ignore the error to be compatible with newer services that may send unknown messages inside Any
}

return message.UnmarshalNew()
return res
}
Loading