Skip to content

Commit

Permalink
Deprecate example field in favor of example_string
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
johanbrandhorst committed Sep 2, 2020
1 parent 86cf24a commit d7add58
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 374 deletions.
478 changes: 239 additions & 239 deletions examples/internal/proto/examplepb/a_bit_of_everything.pb.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/internal/proto/examplepb/a_bit_of_everything.proto
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ message ABitOfEverything {
url: "https://github.com/grpc-ecosystem/grpc-gateway";
description: "Find out more about ABitOfEverything";
}
example: { value: '{ "uuid": "0cf361e1-4b44-483d-a159-54dabdf7e814" }' }
example_string: "{\"uuid\": \"0cf361e1-4b44-483d-a159-54dabdf7e814\"}"
};

// Nested is nested type.
message Nested {
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
example: { value: '{ "ok": "TRUE" }' }
example_string: "{\"ok\": \"TRUE\"}"
};
// name is nested field.
string name = 1;
Expand Down Expand Up @@ -276,7 +276,7 @@ message ABitOfEverything {
// ABitOfEverythingRepeated is used to validate repeated path parameter functionality
message ABitOfEverythingRepeated {
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
example: { value: '{ "path_repeated_bool_value": [true, true, false, true], "path_repeated_int32_value": [1, 2, 3] }' }
example_string: "{\"path_repeated_bool_value\": [true, true, false, true], \"path_repeated_int32_value\": [1, 2, 3]}"
};

// repeated values. they are comma-separated in path
Expand Down
1 change: 0 additions & 1 deletion protoc-gen-swagger/genswagger/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ go_test(
"//protoc-gen-grpc-gateway/httprule:go_default_library",
"//protoc-gen-swagger/options:go_default_library",
"@com_github_golang_protobuf//proto:go_default_library",
"@io_bazel_rules_go//proto/wkt:any_go_proto",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
"@io_bazel_rules_go//proto/wkt:struct_go_proto",
Expand Down
3 changes: 3 additions & 0 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,9 @@ func swaggerSchemaFromProtoSchema(s *swagger_options.Schema, reg *descriptor.Reg
if s != nil && s.Example != nil {
ret.Example = json.RawMessage(s.Example.Value)
}
if s != nil && s.ExampleString != "" {
ret.Example = json.RawMessage(s.ExampleString)
}

return ret
}
Expand Down
10 changes: 2 additions & 8 deletions protoc-gen-swagger/genswagger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/golang/protobuf/proto"
protodescriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/golang/protobuf/ptypes/any"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/httprule"
Expand Down Expand Up @@ -2515,10 +2514,7 @@ func TestRenderMessagesAsDefinition(t *testing.T) {
},
schema: map[string]swagger_options.Schema{
"Message": swagger_options.Schema{
Example: &any.Any{
TypeUrl: "this_isnt_used",
Value: []byte(`{"foo":"bar"}`),
},
ExampleString: `{"foo":"bar"}`,
},
},
defs: map[string]swaggerSchemaObject{
Expand All @@ -2535,9 +2531,7 @@ func TestRenderMessagesAsDefinition(t *testing.T) {
},
schema: map[string]swagger_options.Schema{
"Message": swagger_options.Schema{
Example: &any.Any{
Value: []byte(`XXXX anything goes XXXX`),
},
ExampleString: `XXXX anything goes XXXX`,
},
},
defs: map[string]swaggerSchemaObject{
Expand Down
257 changes: 135 additions & 122 deletions protoc-gen-swagger/options/openapiv2.pb.go

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion protoc-gen-swagger/options/openapiv2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,13 @@ message Schema {
// Additional external documentation for this schema.
ExternalDocumentation external_docs = 5;
// A free-form property to include an example of an instance for this schema.
google.protobuf.Any example = 6;
// Deprecated, please use example_string instead.
google.protobuf.Any example = 6 [
deprecated = true
];
// A free-form property to include a JSON example of this field. This is copied
// verbatim to the output swagger.json. Quotes must be escaped.
string example_string = 7;
}

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

0 comments on commit d7add58

Please sign in to comment.