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

     1  // Copyright 2022 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  	"github.com/googleapis/api-linter/lint"
    19  	"github.com/googleapis/api-linter/locations"
    20  	"github.com/googleapis/api-linter/rules/internal/utils"
    21  	"github.com/jhump/protoreflect/desc"
    22  )
    23  
    24  // BatchCreate methods should reference the target resource via `child_type` or
    25  // the parent directly via `type`.
    26  var resourceReferenceType = &lint.MethodRule{
    27  	Name: lint.NewRuleName(233, "resource-reference-type"),
    28  	OnlyIf: func(m *desc.MethodDescriptor) bool {
    29  		// Return type of the RPC.
    30  		ot := utils.GetResponseType(m)
    31  
    32  		// First repeated message field must be annotated with google.api.resource.
    33  		repeated := utils.GetRepeatedMessageFields(ot)
    34  		if len(repeated) == 0 {
    35  			return false
    36  		}
    37  		resMsg := repeated[0].GetMessageType()
    38  		resource := utils.GetResource(resMsg)
    39  
    40  		parent := m.GetInputType().FindFieldByName("parent")
    41  		return isBatchCreateMethod(m) && parent != nil && utils.GetResourceReference(parent) != nil && resource != nil
    42  	},
    43  	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
    44  		// Return type of the RPC.
    45  		ot := utils.GetResponseType(m)
    46  		repeated := utils.GetRepeatedMessageFields(ot)
    47  		resMsg := repeated[0].GetMessageType()
    48  
    49  		resource := utils.GetResource(resMsg)
    50  		parent := m.GetInputType().FindFieldByName("parent")
    51  		ref := utils.GetResourceReference(parent)
    52  
    53  		if resource.GetType() == ref.GetType() {
    54  			return []lint.Problem{{
    55  				Message:    "BatchCreate should use a `child_type` reference to the created resource, not a `type` reference.",
    56  				Descriptor: parent,
    57  				Location:   locations.FieldResourceReference(parent),
    58  			}}
    59  		}
    60  		if ref.GetChildType() != "" && resource.GetType() != ref.GetChildType() {
    61  			return []lint.Problem{{
    62  				Message:    "BatchCreate should use a `child_type` reference to the created resource.",
    63  				Descriptor: parent,
    64  				Location:   locations.FieldResourceReference(parent),
    65  			}}
    66  		}
    67  
    68  		return nil
    69  	},
    70  }