You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/mapping/customizing_openapi_output.md
+55Lines changed: 55 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -231,6 +231,61 @@ This is how the OpenAPI file would be rendered in [Postman](https://www.getpostm
231
231
232
232
For a more detailed example of a proto file that has Go, templates enabled, [see the examples](https://github.com/grpc-ecosystem/grpc-gateway/blob/main/examples/internal/proto/examplepb/use_go_template.proto).
233
233
234
+
### Using custom values
235
+
236
+
Custom values can be specified in the [Go templates](https://golang.org/pkg/text/template/) that generate your proto file comments.
237
+
238
+
A use case might be to interpolate different external documentation URLs when rendering documentation for different environments.
239
+
240
+
#### How to use it
241
+
242
+
The `use_go_templates` option has to be enabled as a prerequisite.
243
+
244
+
Provide customized values in the format of `go_template_args=my_key=my_value`. `{{arg "my_key"}}` will be replaced with `my_value` in the Go template.
245
+
246
+
Specify the `go_template_args` option multiple times if needed.
// {{.MethodDescriptorProto.Name}} is a call with the method(s) {{$first := true}}{{range .Bindings}}{{if $first}}{{$first = false}}{{else}}, {{end}}{{.HTTPMethod}}{{end}} within the "{{.Service.Name}}" service.
277
+
// It takes in "{{.RequestType.Name}}" and returns a "{{.ResponseType.Name}}".
278
+
// This only works in the {{arg "environment"}} domain.
279
+
//
280
+
rpc Login (LoginRequest) returns (LoginReply) {
281
+
option (google.api.http) = {
282
+
post: "/v1/example/login"
283
+
body: "*"
284
+
};
285
+
}
286
+
}
287
+
```
288
+
234
289
## Other plugin options
235
290
236
291
A comprehensive list of OpenAPI plugin options can be found [here](https://github.com/grpc-ecosystem/grpc-gateway/blob/main/protoc-gen-openapiv2/main.go). Options can be passed via `protoc` CLI:
Copy file name to clipboardExpand all lines: protoc-gen-openapiv2/main.go
+7Lines changed: 7 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,7 @@ var (
30
30
useFQNForOpenAPIName=flag.Bool("fqn_for_openapi_name", false, "if set, the object's OpenAPI names will use the fully qualified names from the proto definition (ie my.package.MyMessage.MyInnerMessage). DEPRECATED: prefer `openapi_naming_strategy=fqn`")
31
31
openAPINamingStrategy=flag.String("openapi_naming_strategy", "", "use the given OpenAPI naming strategy. Allowed values are `legacy`, `fqn`, `simple`. If unset, either `legacy` or `fqn` are selected, depending on the value of the `fqn_for_openapi_name` flag")
32
32
useGoTemplate=flag.Bool("use_go_templates", false, "if set, you can use Go templates in protofile comments")
33
+
goTemplateArgs=utilities.StringArrayFlag(flag.CommandLine, "go_template_args", "provide a custom value that can override a key in the Go template. Requires the `use_go_templates` option to be set")
33
34
ignoreComments=flag.Bool("ignore_comments", false, "if set, all protofile comments are excluded from output")
34
35
removeInternalComments=flag.Bool("remove_internal_comments", false, "if set, removes all substrings in comments that start with `(--` and end with `--)` as specified in https://google.aip.dev/192#internal-comments")
35
36
disableDefaultErrors=flag.Bool("disable_default_errors", false, "if set, disables generation of default errors. This is useful if you have defined custom error handling")
0 commit comments