Skip to content

Error when trying to call endpoint with empty parameter of Enum array #1711

Closed
@ghostiam

Description

🐛 Bug Report

Error when trying to call endpoint with empty parameter of Enum array (select -- from Swagger UI)

curl -X GET "http://127.0.0.1:3000/endpoint?entities="
{
  "code": 3,
  "message": "parsing list \"entities\": \"\" is not a valid value"
}

Function that returns an error: Parse -> populateFieldValueFromPath
https://github.com/grpc-ecosystem/grpc-gateway/blob/v2/runtime/query.go#L53

I think we can ignore all empty parameters, since grpc does not distinguish nil from the default value (as far as I know, I may be wrong).
If you agree, I can fix this bug.

To Reproduce

enum LogEntity {
  UNSPECIFIED = 0;
  SPOT = 1;
  GROUP = 2;
}

message Request{
  repeated LogEntity entities = 1;
}
message Response{
}

service Test {
  rpc Endpoint (Request) returns (Response) {
    option (google.api.http) = {
      get: "/endpoint"
    };
  }
}
curl -X GET "http://127.0.0.1:3000/endpoint?entities="

From debugger

?actions=&entities=

Снимок экрана 2020-10-02 в 16 53 02

Expected behavior

Empty fields should be ignored.

Actual Behavior

Error: parsing list "entities": "" is not a valid value.

Your Environment

grpc-gateway v2@v2.0.0-beta.5

UPD: 🙈 Crutch

emptyValueFixer := runtime.WithMetadata(func(ctx context.Context, req *http.Request) metadata.MD {
	_ = req.ParseForm()

	values := req.Form
	for k, v := range values {
		if len(v) == 1 && v[0] == "" {
			delete(values, k)
		}
	}

	return nil
})

UPD (2022-10-25):

func GWEmptyValueFixer() runtime.ServeMuxOption {
	return runtime.WithMetadata(func(ctx context.Context, req *http.Request) metadata.MD {
		values := req.URL.Query()
		for k, v := range values {
			if len(v) == 1 && v[0] == "" {
				delete(values, k)
			}
		}

		return nil
	})
}

grpcMux := runtime.NewServeMux(
	GWEmptyValueFixer(),
)

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions