github.com/googleapis/api-linter@v1.65.2/rules/aip0131/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 aip0131
    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  			"ValidOptionalReadMask",
    39  			"google.protobuf.FieldMask read_mask = 2 [(google.api.field_behavior) = OPTIONAL];",
    40  			"read_mask",
    41  			nil,
    42  		},
    43  		{
    44  			"InvalidRequiredReadMask",
    45  			"google.protobuf.FieldMask read_mask = 2 [(google.api.field_behavior) = REQUIRED];",
    46  			"read_mask",
    47  			testutils.Problems{
    48  				{Message: `Get RPCs must only require fields explicitly described in AIPs, not "read_mask"`},
    49  			},
    50  		},
    51  		{
    52  			"InvalidRequiredUnknownField",
    53  			"bool create_iam = 3 [(google.api.field_behavior) = REQUIRED];",
    54  			"create_iam",
    55  			testutils.Problems{
    56  				{Message: `Get RPCs must only require fields explicitly described in AIPs, not "create_iam"`},
    57  			},
    58  		},
    59  	} {
    60  		t.Run(test.name, func(t *testing.T) {
    61  			f := testutils.ParseProto3Tmpl(t, `
    62  				import "google/api/annotations.proto";
    63  				import "google/api/field_behavior.proto";
    64  				import "google/api/resource.proto";
    65  				import "google/protobuf/field_mask.proto";
    66  
    67  				service Library {
    68  					rpc GetBook(GetBookRequest) returns (Book) {
    69  						option (google.api.http) = {
    70  							get: "/v1/{name=publishers/*/books/*}"
    71  						};
    72  					}
    73  				}
    74  
    75  				message GetBookRequest {
    76  					// The name of the book to retrieve.
    77  					// Format: publishers/{publisher}/books/{book}
    78  					string name = 1 [
    79  					    (google.api.field_behavior) = REQUIRED,
    80  						(google.api.resource_reference) = {
    81  							type: "library.googleapis.com/Book"
    82  						}
    83  					];
    84  					{{.Fields}}
    85  				}
    86  
    87  				message Book {
    88  					option (google.api.resource) = {
    89  						type: "library.googleapis.com/Book"
    90  						pattern: "publishers/{publisher}/books/{book}"
    91  					};
    92  					string name = 1;
    93  				}
    94  			`, test)
    95  			var dbr desc.Descriptor = f.FindMessage("GetBookRequest")
    96  			if test.problematicFieldName != "" {
    97  				dbr = f.FindMessage("GetBookRequest").FindFieldByName(test.problematicFieldName)
    98  			}
    99  			if diff := test.problems.SetDescriptor(dbr).Diff(requestRequiredFields.Lint(f)); diff != "" {
   100  				t.Errorf(diff)
   101  			}
   102  		})
   103  	}
   104  }