github.com/googleapis/api-linter@v1.65.2/rules/aip0132/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 aip0132
    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  			"ValidOptionalPageSize",
    39  			"int32 page_size = 2 [(google.api.field_behavior) = OPTIONAL];",
    40  			"page_size",
    41  			nil,
    42  		},
    43  		{
    44  			"InvalidRequiredPageSize",
    45  			"int32 page_size = 2 [(google.api.field_behavior) = REQUIRED];",
    46  			"page_size",
    47  			testutils.Problems{
    48  				{Message: `List RPCs must only require fields explicitly described in AIPs, not "page_size"`},
    49  			},
    50  		},
    51  		{
    52  			"InvalidRequiredUnknownField",
    53  			"bool create_iam = 3 [(google.api.field_behavior) = REQUIRED];",
    54  			"create_iam",
    55  			testutils.Problems{
    56  				{Message: `List 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  
    66  				service Library {
    67  					rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) {
    68  						option (google.api.http) = {
    69  							get: "/v1/{parent=publishers/*}/books"
    70  						};
    71  					}
    72  				}
    73  
    74  				message ListBooksRequest {
    75  					// The parent, which owns this collection of books.
    76  					// Format: publishers/{publisher}
    77  					string parent = 1 [
    78  					    (google.api.field_behavior) = REQUIRED,
    79  					    (google.api.resource_reference) = {
    80  					  		child_type: "library.googleapis.com/Book"
    81  					    }];
    82  
    83  					{{.Fields}}
    84  				}
    85  
    86  				message ListBooksResponse {
    87  					repeated Book books = 1;
    88  					string next_page_token = 2;
    89  				}
    90  
    91  				message Book {
    92  					option (google.api.resource) = {
    93  						type: "library.googleapis.com/Book"
    94  						pattern: "publishers/{publisher}/books/{book}"
    95  					};
    96  					string name = 1;
    97  				}
    98  			`, test)
    99  			var dbr desc.Descriptor = f.FindMessage("ListBooksRequest")
   100  			if test.problematicFieldName != "" {
   101  				dbr = f.FindMessage("ListBooksRequest").FindFieldByName(test.problematicFieldName)
   102  			}
   103  			if diff := test.problems.SetDescriptor(dbr).Diff(requestRequiredFields.Lint(f)); diff != "" {
   104  				t.Errorf(diff)
   105  			}
   106  		})
   107  	}
   108  }