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

     1  // Copyright 2019 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 aip0233
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/googleapis/api-linter/lint"
    21  	"github.com/googleapis/api-linter/locations"
    22  	"github.com/googleapis/api-linter/rules/internal/utils"
    23  	"github.com/jhump/protoreflect/desc"
    24  )
    25  
    26  // Batch Create method should have a properly named Response message.
    27  var responseMessageName = &lint.MethodRule{
    28  	Name:   lint.NewRuleName(233, "response-message-name"),
    29  	OnlyIf: isBatchCreateMethod,
    30  	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
    31  		// Proper response name should be the concatenation of the method name and
    32  		// "Response"
    33  		want := m.GetName() + "Response"
    34  
    35  		// If this is an LRO, then use the annotated response type instead of
    36  		// the actual RPC return type.
    37  		got := m.GetOutputType().GetName()
    38  		if utils.IsOperation(m.GetOutputType()) {
    39  			got = utils.GetOperationInfo(m).GetResponseType()
    40  		}
    41  
    42  		// Rule check: Establish that for methods such as `BatchCreateFoos`, the
    43  		// response message should be named `BatchCreateFoosResponse`
    44  		//
    45  		// Note: If `got` is empty string, this is an unannotated LRO.
    46  		// The AIP-151 rule will whine about that, and this rule should not as it
    47  		// would be confusing.
    48  		if got != want && got != "" {
    49  			return []lint.Problem{{
    50  				Message: fmt.Sprintf(
    51  					"Batch Create RPCs should have a properly named response message %q, but not %q",
    52  					want, got,
    53  				),
    54  				Descriptor: m,
    55  				Location:   locations.MethodResponseType(m),
    56  				Suggestion: want,
    57  			}}
    58  		}
    59  
    60  		return nil
    61  	},
    62  }