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

Add support for method's deprecated option #4373

Merged
merged 5 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,8 @@ func renderServices(services []*descriptor.Service, paths *openapiPathsObject, r
continue
}

deprecated := meth.GetOptions().GetDeprecated()

for bIdx, b := range meth.Bindings {
operationFunc := operationForMethod(b.HTTPMethod)
// Iterate over all the OpenAPI parameters
Expand Down Expand Up @@ -1508,6 +1510,7 @@ func renderServices(services []*descriptor.Service, paths *openapiPathsObject, r
operationObject := &openapiOperationObject{
Parameters: parameters,
Responses: openapiResponsesObject{},
Deprecated: deprecated,
}

if !reg.GetDisableDefaultResponses() {
Expand Down Expand Up @@ -1566,14 +1569,17 @@ func renderServices(services []*descriptor.Service, paths *openapiPathsObject, r
grpclog.Error(err)
return err
}

opts, err := getMethodOpenAPIOption(reg, meth)
if opts != nil {
if err != nil {
panic(err)
}
operationObject.ExternalDocs = protoExternalDocumentationToOpenAPIExternalDocumentation(opts.ExternalDocs, reg, meth)
// TODO(ivucica): this would be better supported by looking whether the method is deprecated in the proto file
operationObject.Deprecated = opts.Deprecated

if opts.Deprecated {
operationObject.Deprecated = true
}

if opts.Summary != "" {
operationObject.Summary = opts.Summary
Expand Down
123 changes: 123 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10789,3 +10789,126 @@ func TestApiVisibilityOption(t *testing.T) {
t.Fatal("Definition should be excluded by api visibility option")
}
}

func TestRenderServicesOptionDeprecated(t *testing.T) {
testCases := [...]struct {
testName string
methodOptions descriptorpb.MethodOptions
openapiOperation *openapi_options.Operation
expectedDeprecated bool
}{
{
testName: "method option",
methodOptions: descriptorpb.MethodOptions{
Deprecated: proto.Bool(true),
},
expectedDeprecated: true,
},
{
testName: "openapi option",
openapiOperation: &openapi_options.Operation{
Deprecated: true,
},
expectedDeprecated: true,
},
{
testName: "empty openapi doesn't override method option",
methodOptions: descriptorpb.MethodOptions{
Deprecated: proto.Bool(true),
},
openapiOperation: &openapi_options.Operation{},
expectedDeprecated: true,
},
}

for _, tc := range testCases {
tc := tc

t.Run(tc.testName, func(t *testing.T) {
msgdesc := &descriptorpb.DescriptorProto{
Name: proto.String("ExampleMessage"),
}

meth := &descriptorpb.MethodDescriptorProto{
Name: proto.String("Example"),
InputType: proto.String("ExampleMessage"),
OutputType: proto.String("ExampleMessage"),
Options: &tc.methodOptions,
}

svc := &descriptorpb.ServiceDescriptorProto{
Name: proto.String("ExampleService"),
Method: []*descriptorpb.MethodDescriptorProto{meth},
}

msg := &descriptor.Message{
DescriptorProto: msgdesc,
}

file := descriptor.File{
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
SourceCodeInfo: &descriptorpb.SourceCodeInfo{},
Name: proto.String("example.proto"),
Package: proto.String("example"),
MessageType: []*descriptorpb.DescriptorProto{msgdesc},
Service: []*descriptorpb.ServiceDescriptorProto{svc},
Options: &descriptorpb.FileOptions{
GoPackage: proto.String("github.com/grpc-ecosystem/grpc-gateway/runtime/internal/examplepb;example"),
},
},
GoPkg: descriptor.GoPackage{
Path: "example.com/path/to/example/example.pb",
Name: "example_pb",
},
Messages: []*descriptor.Message{msg},
Services: []*descriptor.Service{
{
ServiceDescriptorProto: svc,
Methods: []*descriptor.Method{
{
MethodDescriptorProto: meth,
RequestType: msg,
ResponseType: msg,
Bindings: []*descriptor.Binding{
{
HTTPMethod: "GET",
PathTmpl: httprule.Template{
Version: 1,
OpCodes: []int{0, 0},
Template: "/v1/echo",
},
},
},
},
},
},
},
}

if tc.openapiOperation != nil {
proto.SetExtension(
proto.Message(file.Services[0].Methods[0].Options),
openapi_options.E_Openapiv2Operation,
tc.openapiOperation,
)
}

reg := descriptor.NewRegistry()
fileCL := crossLinkFixture(&file)

if err := reg.Load(reqFromFile(fileCL)); err != nil {
t.Errorf("reg.Load(%#v) failed with %v; want success", file, err)
}

result, err := applyTemplate(param{File: fileCL, reg: reg})
if err != nil {
t.Fatalf("applyTemplate(%#v) failed with %v; want success", file, err)
}

got := result.getPathItemObject("/v1/echo").Get.Deprecated
if got != tc.expectedDeprecated {
t.Fatalf("Wrong deprecated field, got %v want %v", got, tc.expectedDeprecated)
}
})
}
}
Loading