github.com/googleapis/api-linter@v1.65.2/rules/aip0151/lro_response_type.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 aip0151
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/googleapis/api-linter/locations"
    21  
    22  	"github.com/googleapis/api-linter/lint"
    23  	"github.com/googleapis/api-linter/rules/internal/utils"
    24  	"github.com/jhump/protoreflect/desc"
    25  )
    26  
    27  var lroResponse = &lint.MethodRule{
    28  	Name:   lint.NewRuleName(151, "lro-response-type"),
    29  	OnlyIf: isAnnotatedLRO,
    30  	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
    31  		lro := utils.GetOperationInfo(m)
    32  
    33  		// Ensure the response type is set.
    34  		if lro.GetResponseType() == "" {
    35  			return []lint.Problem{{
    36  				Message:    "Methods returning an LRO must set the response type.",
    37  				Descriptor: m,
    38  				Location:   locations.MethodOperationInfo(m),
    39  			}}
    40  		}
    41  
    42  		// Unless this is a Delete method, the response type should not be Empty.
    43  		if strings.HasPrefix(m.GetName(), "Delete") || strings.HasPrefix(m.GetName(), "BatchDelete") {
    44  			return nil
    45  		}
    46  		if t := lro.GetResponseType(); t == "Empty" || t == "google.protobuf.Empty" {
    47  			return []lint.Problem{{
    48  				Message:    "Methods returning an LRO should create a blank message rather than using Empty.",
    49  				Descriptor: m,
    50  				Location:   locations.MethodOperationInfo(m),
    51  			}}
    52  		}
    53  
    54  		return nil
    55  	},
    56  }