github.com/googleapis/api-linter@v1.65.2/rules/aip0133/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 aip0133
    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  // Create methods should reference the target resource via `child_type` or the
    25  // parent directly via `type`.
    26  var resourceReferenceType = &lint.MethodRule{
    27  	Name: lint.NewRuleName(133, "resource-reference-type"),
    28  	OnlyIf: func(m *desc.MethodDescriptor) bool {
    29  		ot := utils.GetResponseType(m)
    30  		// Unresolvable response_type for an Operation results in nil here.
    31  		resource := utils.GetResource(ot)
    32  		p := m.GetInputType().FindFieldByName("parent")
    33  		return utils.IsCreateMethod(m) && p != nil && utils.GetResourceReference(p) != nil && resource != nil
    34  	},
    35  	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
    36  		// Return type of the RPC.
    37  		ot := utils.GetResponseType(m)
    38  		resource := utils.GetResource(ot)
    39  		parent := m.GetInputType().FindFieldByName("parent")
    40  		ref := utils.GetResourceReference(parent)
    41  
    42  		if resource.GetType() == ref.GetType() {
    43  			return []lint.Problem{{
    44  				Message:    "Create should use a `child_type` reference to the created resource, not a `type` reference.",
    45  				Descriptor: parent,
    46  				Location:   locations.FieldResourceReference(parent),
    47  			}}
    48  		}
    49  		if ref.GetChildType() != "" && resource.GetType() != ref.GetChildType() {
    50  			return []lint.Problem{{
    51  				Message:    "Create should use a `child_type` reference to the created resource.",
    52  				Descriptor: parent,
    53  				Location:   locations.FieldResourceReference(parent),
    54  			}}
    55  		}
    56  
    57  		return nil
    58  	},
    59  }