github.com/djarvur/go-swagger@v0.18.0/scan/parameters_test.go (about)

     1  // Copyright 2015 go-swagger maintainers
     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  //    http://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 scan
    16  
    17  import (
    18  	goparser "go/parser"
    19  	"log"
    20  	"testing"
    21  
    22  	"github.com/go-openapi/spec"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestScanFileParam(t *testing.T) {
    27  	docFile := "../fixtures/goparsing/classification/operations/noparams.go"
    28  	fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments)
    29  	if err != nil {
    30  		log.Fatal(err)
    31  	}
    32  	sp := newParameterParser(classificationProg)
    33  	noParamOps := make(map[string]*spec.Operation)
    34  	err = sp.Parse(fileTree, noParamOps)
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  	assert.Len(t, noParamOps, 10)
    39  
    40  	of, ok := noParamOps["myOperation"]
    41  	assert.True(t, ok)
    42  	assert.Len(t, of.Parameters, 1)
    43  	fileParam := of.Parameters[0]
    44  	assert.Equal(t, "MyFormFile desc.", fileParam.Description)
    45  	assert.Equal(t, "formData", fileParam.In)
    46  	assert.Equal(t, "file", fileParam.Type)
    47  	assert.False(t, fileParam.Required)
    48  
    49  	emb, ok := noParamOps["myOtherOperation"]
    50  	assert.True(t, ok)
    51  	assert.Len(t, emb.Parameters, 2)
    52  	fileParam = emb.Parameters[0]
    53  	assert.Equal(t, "MyFormFile desc.", fileParam.Description)
    54  	assert.Equal(t, "formData", fileParam.In)
    55  	assert.Equal(t, "file", fileParam.Type)
    56  	assert.False(t, fileParam.Required)
    57  	extraParam := emb.Parameters[1]
    58  	assert.Equal(t, "ExtraParam desc.", extraParam.Description)
    59  	assert.Equal(t, "formData", extraParam.In)
    60  	assert.Equal(t, "integer", extraParam.Type)
    61  	assert.True(t, extraParam.Required)
    62  
    63  	ffp, ok := noParamOps["myFuncOperation"]
    64  	assert.True(t, ok)
    65  	assert.Len(t, ffp.Parameters, 1)
    66  	fileParam = ffp.Parameters[0]
    67  	assert.Equal(t, "MyFormFile desc.", fileParam.Description)
    68  	assert.Equal(t, "formData", fileParam.In)
    69  	assert.Equal(t, "file", fileParam.Type)
    70  	assert.False(t, fileParam.Required)
    71  }
    72  
    73  func TestParamsParser(t *testing.T) {
    74  	docFile := "../fixtures/goparsing/classification/operations/noparams.go"
    75  	fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments)
    76  	if err != nil {
    77  		log.Fatal(err)
    78  	}
    79  	sp := newParameterParser(classificationProg)
    80  	noParamOps := make(map[string]*spec.Operation)
    81  	err = sp.Parse(fileTree, noParamOps)
    82  	if err != nil {
    83  		log.Fatal(err)
    84  	}
    85  	assert.Len(t, noParamOps, 10)
    86  
    87  	cr, okParam := noParamOps["yetAnotherOperation"]
    88  	assert.True(t, okParam)
    89  	assert.Len(t, cr.Parameters, 8)
    90  	for _, param := range cr.Parameters {
    91  		switch param.Name {
    92  		case "id":
    93  			assert.Equal(t, "integer", param.Type)
    94  			assert.Equal(t, "int64", param.Format)
    95  		case "name":
    96  			assert.Equal(t, "string", param.Type)
    97  			assert.Equal(t, "", param.Format)
    98  		case "age":
    99  			assert.Equal(t, "integer", param.Type)
   100  			assert.Equal(t, "int32", param.Format)
   101  		case "notes":
   102  			assert.Equal(t, "string", param.Type)
   103  			assert.Equal(t, "", param.Format)
   104  		case "extra":
   105  			assert.Equal(t, "string", param.Type)
   106  			assert.Equal(t, "", param.Format)
   107  		case "createdAt":
   108  			assert.Equal(t, "string", param.Type)
   109  			assert.Equal(t, "date-time", param.Format)
   110  		case "informity":
   111  			assert.Equal(t, "string", param.Type)
   112  			assert.Equal(t, "formData", param.In)
   113  		case "NoTagName":
   114  			assert.Equal(t, "string", param.Type)
   115  			assert.Equal(t, "", param.Format)
   116  		default:
   117  			assert.Fail(t, "unknown property: "+param.Name)
   118  		}
   119  	}
   120  
   121  	ob, okParam := noParamOps["updateOrder"]
   122  	assert.True(t, okParam)
   123  	assert.Len(t, ob.Parameters, 1)
   124  	bodyParam := ob.Parameters[0]
   125  	assert.Equal(t, "The order to submit.", bodyParam.Description)
   126  	assert.Equal(t, "body", bodyParam.In)
   127  	assert.Equal(t, "#/definitions/order", bodyParam.Schema.Ref.String())
   128  	assert.True(t, bodyParam.Required)
   129  
   130  	mop, okParam := noParamOps["getOrders"]
   131  	assert.True(t, okParam)
   132  	assert.Len(t, mop.Parameters, 2)
   133  	ordersParam := mop.Parameters[0]
   134  	assert.Equal(t, "The orders", ordersParam.Description)
   135  	assert.True(t, ordersParam.Required)
   136  	assert.Equal(t, "array", ordersParam.Type)
   137  	otherParam := mop.Parameters[1]
   138  	assert.Equal(t, "And another thing", otherParam.Description)
   139  
   140  	op, okParam := noParamOps["someOperation"]
   141  	assert.True(t, okParam)
   142  	assert.Len(t, op.Parameters, 8)
   143  
   144  	for _, param := range op.Parameters {
   145  		switch param.Name {
   146  		case "id":
   147  			assert.Equal(t, "ID of this no model instance.\nids in this application start at 11 and are smaller than 1000", param.Description)
   148  			assert.Equal(t, "path", param.In)
   149  			assert.Equal(t, "integer", param.Type)
   150  			assert.Equal(t, "int64", param.Format)
   151  			assert.True(t, param.Required)
   152  			assert.Equal(t, "ID", param.Extensions["x-go-name"])
   153  			assert.EqualValues(t, 1000, *param.Maximum)
   154  			assert.True(t, param.ExclusiveMaximum)
   155  			assert.EqualValues(t, 10, *param.Minimum)
   156  			assert.True(t, param.ExclusiveMinimum)
   157  			assert.Equal(t, 1, param.Default, "%s default value is incorrect", param.Name)
   158  
   159  		case "score":
   160  			assert.Equal(t, "The Score of this model", param.Description)
   161  			assert.Equal(t, "query", param.In)
   162  			assert.Equal(t, "integer", param.Type)
   163  			assert.Equal(t, "int32", param.Format)
   164  			assert.True(t, param.Required)
   165  			assert.Equal(t, "Score", param.Extensions["x-go-name"])
   166  			assert.EqualValues(t, 45, *param.Maximum)
   167  			assert.False(t, param.ExclusiveMaximum)
   168  			assert.EqualValues(t, 3, *param.Minimum)
   169  			assert.False(t, param.ExclusiveMinimum)
   170  			assert.Equal(t, 2, param.Default, "%s default value is incorrect", param.Name)
   171  			assert.Equal(t, 27, param.Example)
   172  
   173  		case "x-hdr-name":
   174  			assert.Equal(t, "Name of this no model instance", param.Description)
   175  			assert.Equal(t, "header", param.In)
   176  			assert.Equal(t, "string", param.Type)
   177  			assert.True(t, param.Required)
   178  			assert.Equal(t, "Name", param.Extensions["x-go-name"])
   179  			assert.EqualValues(t, 4, *param.MinLength)
   180  			assert.EqualValues(t, 50, *param.MaxLength)
   181  			assert.Equal(t, "[A-Za-z0-9-.]*", param.Pattern)
   182  
   183  		case "created":
   184  			assert.Equal(t, "Created holds the time when this entry was created", param.Description)
   185  			assert.Equal(t, "query", param.In)
   186  			assert.Equal(t, "string", param.Type)
   187  			assert.Equal(t, "date-time", param.Format)
   188  			assert.False(t, param.Required)
   189  			assert.Equal(t, "Created", param.Extensions["x-go-name"])
   190  
   191  		case "category":
   192  			assert.Equal(t, "The Category of this model", param.Description)
   193  			assert.Equal(t, "query", param.In)
   194  			assert.Equal(t, "string", param.Type)
   195  			assert.True(t, param.Required)
   196  			assert.Equal(t, "Category", param.Extensions["x-go-name"])
   197  			assert.EqualValues(t, []interface{}{"foo", "bar", "none"}, param.Enum, "%s enum values are incorrect", param.Name)
   198  			assert.Equal(t, "bar", param.Default, "%s default value is incorrect", param.Name)
   199  
   200  		case "foo_slice":
   201  			assert.Equal(t, "a FooSlice has foos which are strings", param.Description)
   202  			assert.Equal(t, "FooSlice", param.Extensions["x-go-name"])
   203  			assert.Equal(t, "query", param.In)
   204  			assert.Equal(t, "array", param.Type)
   205  			assert.False(t, param.Required)
   206  			assert.True(t, param.UniqueItems)
   207  			assert.Equal(t, "pipe", param.CollectionFormat)
   208  			assert.EqualValues(t, 3, *param.MinItems, "'foo_slice' should have had 3 min items")
   209  			assert.EqualValues(t, 10, *param.MaxItems, "'foo_slice' should have had 10 max items")
   210  			itprop := param.Items
   211  			assert.EqualValues(t, 3, *itprop.MinLength, "'foo_slice.items.minLength' should have been 3")
   212  			assert.EqualValues(t, 10, *itprop.MaxLength, "'foo_slice.items.maxLength' should have been 10")
   213  			assert.EqualValues(t, "\\w+", itprop.Pattern, "'foo_slice.items.pattern' should have \\w+")
   214  			assert.EqualValues(t, "bar", itprop.Default, "'foo_slice.items.default' should have bar default value")
   215  
   216  		case "items":
   217  			assert.Equal(t, "Items", param.Extensions["x-go-name"])
   218  			assert.Equal(t, "body", param.In)
   219  			assert.NotNil(t, param.Schema)
   220  			aprop := param.Schema
   221  			assert.Equal(t, "array", aprop.Type[0])
   222  			assert.NotNil(t, aprop.Items)
   223  			assert.NotNil(t, aprop.Items.Schema)
   224  			itprop := aprop.Items.Schema
   225  			assert.Len(t, itprop.Properties, 4)
   226  			assert.Len(t, itprop.Required, 3)
   227  			assertProperty(t, itprop, "integer", "id", "int32", "ID")
   228  			iprop, ok := itprop.Properties["id"]
   229  			assert.True(t, ok)
   230  			assert.Equal(t, "ID of this no model instance.\nids in this application start at 11 and are smaller than 1000", iprop.Description)
   231  			assert.EqualValues(t, 1000, *iprop.Maximum)
   232  			assert.True(t, iprop.ExclusiveMaximum, "'id' should have had an exclusive maximum")
   233  			assert.NotNil(t, iprop.Minimum)
   234  			assert.EqualValues(t, 10, *iprop.Minimum)
   235  			assert.True(t, iprop.ExclusiveMinimum, "'id' should have had an exclusive minimum")
   236  			assert.Equal(t, 3, iprop.Default, "Items.ID default value is incorrect")
   237  
   238  			assertRef(t, itprop, "pet", "Pet", "#/definitions/pet")
   239  			iprop, ok = itprop.Properties["pet"]
   240  			assert.True(t, ok)
   241  			// if itprop.Ref.String() == "" {
   242  			// 	assert.Equal(t, "The Pet to add to this NoModel items bucket.\nPets can appear more than once in the bucket", iprop.Description)
   243  			// }
   244  
   245  			assertProperty(t, itprop, "integer", "quantity", "int16", "Quantity")
   246  			iprop, ok = itprop.Properties["quantity"]
   247  			assert.True(t, ok)
   248  			assert.Equal(t, "The amount of pets to add to this bucket.", iprop.Description)
   249  			assert.EqualValues(t, 1, *iprop.Minimum)
   250  			assert.EqualValues(t, 10, *iprop.Maximum)
   251  
   252  			assertProperty(t, itprop, "string", "notes", "", "Notes")
   253  			iprop, ok = itprop.Properties["notes"]
   254  			assert.True(t, ok)
   255  			assert.Equal(t, "Notes to add to this item.\nThis can be used to add special instructions.", iprop.Description)
   256  
   257  		case "bar_slice":
   258  			assert.Equal(t, "a BarSlice has bars which are strings", param.Description)
   259  			assert.Equal(t, "BarSlice", param.Extensions["x-go-name"])
   260  			assert.Equal(t, "query", param.In)
   261  			assert.Equal(t, "array", param.Type)
   262  			assert.False(t, param.Required)
   263  			assert.True(t, param.UniqueItems)
   264  			assert.Equal(t, "pipe", param.CollectionFormat)
   265  			assert.NotNil(t, param.Items, "bar_slice should have had an items property")
   266  			assert.EqualValues(t, 3, *param.MinItems, "'bar_slice' should have had 3 min items")
   267  			assert.EqualValues(t, 10, *param.MaxItems, "'bar_slice' should have had 10 max items")
   268  			itprop := param.Items
   269  			if assert.NotNil(t, itprop) {
   270  				assert.EqualValues(t, 4, *itprop.MinItems, "'bar_slice.items.minItems' should have been 4")
   271  				assert.EqualValues(t, 9, *itprop.MaxItems, "'bar_slice.items.maxItems' should have been 9")
   272  				itprop2 := itprop.Items
   273  				if assert.NotNil(t, itprop2) {
   274  					assert.EqualValues(t, 5, *itprop2.MinItems, "'bar_slice.items.items.minItems' should have been 5")
   275  					assert.EqualValues(t, 8, *itprop2.MaxItems, "'bar_slice.items.items.maxItems' should have been 8")
   276  					itprop3 := itprop2.Items
   277  					if assert.NotNil(t, itprop3) {
   278  						assert.EqualValues(t, 3, *itprop3.MinLength, "'bar_slice.items.items.items.minLength' should have been 3")
   279  						assert.EqualValues(t, 10, *itprop3.MaxLength, "'bar_slice.items.items.items.maxLength' should have been 10")
   280  						assert.EqualValues(t, "\\w+", itprop3.Pattern, "'bar_slice.items.items.items.pattern' should have \\w+")
   281  					}
   282  				}
   283  			}
   284  
   285  		default:
   286  			assert.Fail(t, "unknown property: "+param.Name)
   287  		}
   288  	}
   289  
   290  	// assert that the order of the parameters is maintained
   291  	order, ok := noParamOps["anotherOperation"]
   292  	assert.True(t, ok)
   293  	assert.Len(t, order.Parameters, 8)
   294  
   295  	for index, param := range order.Parameters {
   296  		switch param.Name {
   297  		case "id":
   298  			assert.Equal(t, 0, index, "%s index incorrect", param.Name)
   299  		case "score":
   300  			assert.Equal(t, 1, index, "%s index incorrect", param.Name)
   301  		case "x-hdr-name":
   302  			assert.Equal(t, 2, index, "%s index incorrect", param.Name)
   303  		case "created":
   304  			assert.Equal(t, 3, index, "%s index incorrect", param.Name)
   305  		case "category":
   306  			assert.Equal(t, 4, index, "%s index incorrect", param.Name)
   307  		case "foo_slice":
   308  			assert.Equal(t, 5, index, "%s index incorrect", param.Name)
   309  		case "bar_slice":
   310  			assert.Equal(t, 6, index, "%s index incorrect", param.Name)
   311  		case "items":
   312  			assert.Equal(t, 7, index, "%s index incorrect", param.Name)
   313  		default:
   314  			assert.Fail(t, "unknown property: "+param.Name)
   315  		}
   316  	}
   317  
   318  	// check that aliases work correctly
   319  	aliasOp, ok := noParamOps["someAliasOperation"]
   320  	assert.True(t, ok)
   321  	assert.Len(t, aliasOp.Parameters, 4)
   322  	for _, param := range aliasOp.Parameters {
   323  		switch param.Name {
   324  		case "intAlias":
   325  			assert.Equal(t, "query", param.In)
   326  			assert.Equal(t, "integer", param.Type)
   327  			assert.Equal(t, "int64", param.Format)
   328  			assert.True(t, param.Required)
   329  			assert.EqualValues(t, 10, *param.Maximum)
   330  			assert.EqualValues(t, 1, *param.Minimum)
   331  		case "stringAlias":
   332  			assert.Equal(t, "query", param.In)
   333  			assert.Equal(t, "string", param.Type)
   334  		case "intAliasPath":
   335  			assert.Equal(t, "path", param.In)
   336  			assert.Equal(t, "integer", param.Type)
   337  			assert.Equal(t, "int64", param.Format)
   338  		case "intAliasForm":
   339  			assert.Equal(t, "formData", param.In)
   340  			assert.Equal(t, "integer", param.Type)
   341  			assert.Equal(t, "int64", param.Format)
   342  		default:
   343  			assert.Fail(t, "unknown property: "+param.Name)
   344  		}
   345  	}
   346  }