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

Support mapping request body to map type #765

Open
wdec opened this issue Oct 2, 2018 · 8 comments
Open

Support mapping request body to map type #765

wdec opened this issue Oct 2, 2018 · 8 comments

Comments

@wdec
Copy link

wdec commented Oct 2, 2018

Is there a way tomap the message body to a keyed map?
I.e. with a proto definition containing:

    rpc UpdateConfig (ServiceConfig) returns (OpResult) {
        option (google.api.http) = {
			post: "/v1/robotctl/services/configure"
			body: "config"
		};
    }

message ServiceConfig {
    ServicesReq services            = 1;
    map<string, string> config                   = 2;
}

The idea being that the body of the received message would map to a key in the, eg map<config, body_of_message>

Currently on compiling for grpc-gateway, the following error is thrown - which is odd given that there is no repeated element:

--grpc-gateway_out: repeated field not allowed in field path: config in config

@johanbrandhorst
Copy link
Collaborator

Yeah I don't think this is possible, the proto3 map is just syntactic sugar for:

message map {
    string key = 1;
    string value = 2;
}

message ServiceConfig {
    repeated map config = 1;
}

So that's why you get this cryptic error message. I think technically there's no reason this couldn't work, but clearly it's not currently supported by the gateway. What can I do to help you get a pull request to implement this in?

@johanbrandhorst johanbrandhorst changed the title Compilation error when mapping message body to map Support mapping request body to map type Oct 2, 2018
@wdec
Copy link
Author

wdec commented Oct 2, 2018

Sounds like a nice feature I could add. I'll get looking into it.

@wora
Copy link
Contributor

wora commented Oct 7, 2018

Recently, Google updated the gRPC Transcoding spec, aka google.api.http annotation, to allow repeated field and mapped field as HTTP request/response body. This is to allow third party implementations, such as gRPC Gateway, to provide advanced features that customers may need. Most implementations may not provide such advanced features. Except for backward compatibility, there is no general advantage to use such advanced features. It also creates more work to OpenAPI generator, documentation generator, and other tools. Just FYI.

Disclaim: I am a co-author of gRPC Transcoding spec.

@jwierzba-wish
Copy link

Is this still not supported?

@johanbrandhorst
Copy link
Collaborator

I don't believe it is. Are you interesting in implementing this?

@momom-i
Copy link
Contributor

momom-i commented May 11, 2022

@johanbrandhorst Hi, I've contributed to this project's issues couple times and I'm interested in implementing this.
If I can and this is not supported yet, I'd appreciate it if you could give me some advice about it.

@johanbrandhorst
Copy link
Collaborator

Thanks for your interest! The thing I like to do first with something like this is to create a failing test. You can try adding an example like the one above to one of our example files or add a test to the generator tests. I went and looked at our code and discovered the allow_repeated_fields_in_body parameter:

allowRepeatedFieldsInBody = flag.Bool("allow_repeated_fields_in_body", false, "allows to use repeated field in `body` and `response_body` field of `google.api.http` annotation option")
. Maybe just setting this fixes the issue 🤔?

@momom-i
Copy link
Contributor

momom-i commented May 17, 2022

Ahh, that parameter is exactly what I've been looking for, thank you!

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

No branches or pull requests

5 participants