Skip to content

Commit 3655adb

Browse files
Deprecate example field (#1637)
* Update contribution guide to use 1.15 generator * Deprecate example field in favor of example_string The example field using google.protobuf.Any was pretty silly and just happened to work because of how we were using it. It's confusing to users and breaks some tooling, so it is best deprecated. This is an unfortunate break from the "each field maps to the openapiv2 schema" precedent we've maintained so far, but there is no other good way that I can think of to safely deprecate this.
1 parent 0688fa4 commit 3655adb

File tree

8 files changed

+390
-375
lines changed

8 files changed

+390
-375
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ All submissions, including submissions by project members, require review.
2525
Great! It should be as simple as this (run from the root of the directory):
2626

2727
```bash
28-
docker run -v $(pwd):/src/grpc-gateway --rm docker.pkg.github.com/grpc-ecosystem/grpc-gateway/build-env:1.14 \
28+
docker run -v $(pwd):/src/grpc-gateway --rm docker.pkg.github.com/grpc-ecosystem/grpc-gateway/build-env:1.15 \
2929
/bin/bash -c 'cd /src/grpc-gateway && \
3030
make realclean && \
3131
make examples && \

examples/internal/proto/examplepb/a_bit_of_everything.pb.go

Lines changed: 239 additions & 239 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/internal/proto/examplepb/a_bit_of_everything.proto

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,13 @@ message ABitOfEverything {
194194
url: "https://github.com/grpc-ecosystem/grpc-gateway";
195195
description: "Find out more about ABitOfEverything";
196196
}
197-
example: { value: '{ "uuid": "0cf361e1-4b44-483d-a159-54dabdf7e814" }' }
197+
example_string: "{\"uuid\": \"0cf361e1-4b44-483d-a159-54dabdf7e814\"}"
198198
};
199199

200200
// Nested is nested type.
201201
message Nested {
202202
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
203-
example: { value: '{ "ok": "TRUE" }' }
203+
example_string: "{\"ok\": \"TRUE\"}"
204204
};
205205
// name is nested field.
206206
string name = 1;
@@ -276,7 +276,7 @@ message ABitOfEverything {
276276
// ABitOfEverythingRepeated is used to validate repeated path parameter functionality
277277
message ABitOfEverythingRepeated {
278278
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
279-
example: { value: '{ "path_repeated_bool_value": [true, true, false, true], "path_repeated_int32_value": [1, 2, 3] }' }
279+
example_string: "{\"path_repeated_bool_value\": [true, true, false, true], \"path_repeated_int32_value\": [1, 2, 3]}"
280280
};
281281

282282
// repeated values. they are comma-separated in path

protoc-gen-swagger/genswagger/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ go_test(
4040
"//protoc-gen-grpc-gateway/httprule:go_default_library",
4141
"//protoc-gen-swagger/options:go_default_library",
4242
"@com_github_golang_protobuf//proto:go_default_library",
43-
"@io_bazel_rules_go//proto/wkt:any_go_proto",
4443
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
4544
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
4645
"@io_bazel_rules_go//proto/wkt:struct_go_proto",

protoc-gen-swagger/genswagger/template.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,9 @@ func swaggerSchemaFromProtoSchema(s *swagger_options.Schema, reg *descriptor.Reg
18771877
if s != nil && s.Example != nil {
18781878
ret.Example = json.RawMessage(s.Example.Value)
18791879
}
1880+
if s != nil && s.ExampleString != "" {
1881+
ret.Example = json.RawMessage(s.ExampleString)
1882+
}
18801883

18811884
return ret
18821885
}

protoc-gen-swagger/genswagger/template_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/golang/protobuf/proto"
1212
protodescriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
1313
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
14-
"github.com/golang/protobuf/ptypes/any"
1514
structpb "github.com/golang/protobuf/ptypes/struct"
1615
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
1716
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/httprule"
@@ -2515,10 +2514,7 @@ func TestRenderMessagesAsDefinition(t *testing.T) {
25152514
},
25162515
schema: map[string]swagger_options.Schema{
25172516
"Message": swagger_options.Schema{
2518-
Example: &any.Any{
2519-
TypeUrl: "this_isnt_used",
2520-
Value: []byte(`{"foo":"bar"}`),
2521-
},
2517+
ExampleString: `{"foo":"bar"}`,
25222518
},
25232519
},
25242520
defs: map[string]swaggerSchemaObject{
@@ -2535,9 +2531,7 @@ func TestRenderMessagesAsDefinition(t *testing.T) {
25352531
},
25362532
schema: map[string]swagger_options.Schema{
25372533
"Message": swagger_options.Schema{
2538-
Example: &any.Any{
2539-
Value: []byte(`XXXX anything goes XXXX`),
2540-
},
2534+
ExampleString: `XXXX anything goes XXXX`,
25412535
},
25422536
},
25432537
defs: map[string]swaggerSchemaObject{

protoc-gen-swagger/options/openapiv2.pb.go

Lines changed: 135 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoc-gen-swagger/options/openapiv2.proto

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,13 @@ message Schema {
334334
// Additional external documentation for this schema.
335335
ExternalDocumentation external_docs = 5;
336336
// A free-form property to include an example of an instance for this schema.
337-
google.protobuf.Any example = 6;
337+
// Deprecated, please use example_string instead.
338+
google.protobuf.Any example = 6 [
339+
deprecated = true
340+
];
341+
// A free-form property to include a JSON example of this field. This is copied
342+
// verbatim to the output swagger.json. Quotes must be escaped.
343+
string example_string = 7;
338344
}
339345

340346
// `JSONSchema` represents properties from JSON Schema taken, and as used, in

0 commit comments

Comments
 (0)