github.com/googleapis/api-linter@v1.65.2/rules/aip0235/response_message_name.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aip0235
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"bitbucket.org/creachadair/stringset"
    21  	"github.com/googleapis/api-linter/lint"
    22  	"github.com/googleapis/api-linter/locations"
    23  	"github.com/googleapis/api-linter/rules/internal/utils"
    24  	"github.com/jhump/protoreflect/desc"
    25  )
    26  
    27  // Batch Delete method should have a properly named response message.
    28  var responseMessageName = &lint.MethodRule{
    29  	Name:   lint.NewRuleName(235, "response-message-name"),
    30  	OnlyIf: isBatchDeleteMethod,
    31  	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
    32  		got := m.GetOutputType().GetFullyQualifiedName()
    33  		if utils.IsOperation(m.GetOutputType()) {
    34  			got = utils.GetOperationInfo(m).GetResponseType()
    35  		} else if got != "google.protobuf.Empty" {
    36  			got = m.GetOutputType().GetName()
    37  		}
    38  
    39  		wantSoftDelete := m.GetName() + "Response"
    40  		want := stringset.New(
    41  			"google.protobuf.Empty",
    42  			wantSoftDelete,
    43  		)
    44  
    45  		// Rule check: Establish that for methods such as `BatchDeleteFoos`, the
    46  		// response message should be named `BatchDeleteFoosResponse` or
    47  		// `google.protobuf.Empty`.
    48  		//
    49  		// Note: If `got` is empty string, this is an unannotated LRO.
    50  		// The AIP-151 rule will whine about that, and this rule should not as it
    51  		// would be confusing.
    52  		if !want.Contains(got) && got != "" {
    53  			return []lint.Problem{{
    54  				Message: fmt.Sprintf(
    55  					"Batch Delete RPCs should have response message type of `google.protobuf.Empty` or `%s` (for soft-deletes only), not %q.",
    56  					wantSoftDelete, got,
    57  				),
    58  				Suggestion: "google.protobuf.Empty",
    59  				Descriptor: m,
    60  				Location:   locations.MethodResponseType(m),
    61  			}}
    62  		}
    63  
    64  		return nil
    65  	},
    66  }