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

     1  // Copyright 2023 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 aip0134
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  	"github.com/jhump/protoreflect/desc"
    22  )
    23  
    24  func TestRequiredFieldTests(t *testing.T) {
    25  	for _, test := range []struct {
    26  		name                 string
    27  		Fields               string
    28  		problematicFieldName string
    29  		problems             testutils.Problems
    30  	}{
    31  		{
    32  			"ValidNoExtraFields",
    33  			"",
    34  			"",
    35  			nil,
    36  		},
    37  		{
    38  			"ValidOptionalUpdateMask",
    39  			"google.protobuf.FieldMask update_mask = 2 [(google.api.field_behavior) = OPTIONAL];",
    40  			"update_mask",
    41  			nil,
    42  		},
    43  		{
    44  			"ValidRequiredUpdateMask",
    45  			"google.protobuf.FieldMask update_mask = 2 [(google.api.field_behavior) = REQUIRED];",
    46  			"update_mask",
    47  			nil,
    48  		},
    49  		{
    50  			"ValidOptionalValidateOnly",
    51  			"bool validate_only = 3 [(google.api.field_behavior) = OPTIONAL];",
    52  			"validate_only",
    53  			nil,
    54  		},
    55  		{
    56  			"InvalidRequiredValidateOnly",
    57  			"bool validate_only = 3 [(google.api.field_behavior) = REQUIRED];",
    58  			"validate_only",
    59  			testutils.Problems{
    60  				{Message: `Update RPCs must only require fields explicitly described in AIPs, not "validate_only"`},
    61  			},
    62  		},
    63  		{
    64  			"InvalidRequiredUnknownField",
    65  			"bool create_iam = 3 [(google.api.field_behavior) = REQUIRED];",
    66  			"create_iam",
    67  			testutils.Problems{
    68  				{Message: `Update RPCs must only require fields explicitly described in AIPs, not "create_iam"`},
    69  			},
    70  		},
    71  		{
    72  			"InvalidRequiredUnknownMessageField",
    73  			"Foo foo = 3 [(google.api.field_behavior) = REQUIRED];",
    74  			"foo",
    75  			testutils.Problems{
    76  				{Message: `Update RPCs must only require fields explicitly described in AIPs, not "foo"`},
    77  			},
    78  		},
    79  	} {
    80  		t.Run(test.name, func(t *testing.T) {
    81  			f := testutils.ParseProto3Tmpl(t, `
    82  				import "google/api/annotations.proto";
    83  				import "google/api/field_behavior.proto";
    84  				import "google/api/resource.proto";
    85  				import "google/protobuf/field_mask.proto";
    86  
    87  				service Library {
    88  					rpc UpdateBookShelf(UpdateBookShelfRequest) returns (BookShelf) {
    89  						option (google.api.http) = {
    90  							patch: "/v1/{name=publishers/*/bookShelves/*}"
    91  							body: "book"
    92  						};
    93  					}
    94  				}
    95  
    96  				message BookShelf {
    97  					option (google.api.resource) = {
    98  						type: "library.googleapis.com/BookShelf"
    99  						pattern: "publishers/{publisher}/bookShelves/{book_shelf}"
   100  					};
   101  					string name = 1;
   102  				}
   103  
   104  				message UpdateBookShelfRequest {
   105  					BookShelf book_shelf = 1 [
   106  						(google.api.field_behavior) = REQUIRED
   107  					];
   108  					{{.Fields}}
   109  				}
   110  
   111  				message Foo {}
   112  			`, test)
   113  			var dbr desc.Descriptor = f.FindMessage("UpdateBookShelfRequest")
   114  			if test.problematicFieldName != "" {
   115  				dbr = f.FindMessage("UpdateBookShelfRequest").FindFieldByName(test.problematicFieldName)
   116  			}
   117  			if diff := test.problems.SetDescriptor(dbr).Diff(requestRequiredFields.Lint(f)); diff != "" {
   118  				t.Errorf(diff)
   119  			}
   120  		})
   121  	}
   122  }