github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/scan/responses_test.go (about)

     1  // +build !go1.11
     2  
     3  // Copyright 2015 go-swagger maintainers
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //    http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package scan
    18  
    19  import (
    20  	goparser "go/parser"
    21  	"log"
    22  	"testing"
    23  
    24  	"github.com/go-openapi/spec"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestParseResponses(t *testing.T) {
    29  	docFile := "../fixtures/goparsing/classification/operations/responses.go"
    30  	fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments)
    31  	if err != nil {
    32  		log.Fatal(err)
    33  	}
    34  
    35  	rp := newResponseParser(classificationProg)
    36  	responses := make(map[string]spec.Response)
    37  	err = rp.Parse(fileTree, responses)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	assert.Len(t, responses, 9)
    42  	cr, ok := responses["complexerOne"]
    43  	assert.True(t, ok)
    44  	assert.Len(t, cr.Headers, 7)
    45  	for k, header := range cr.Headers {
    46  		switch k {
    47  		case "id":
    48  			assert.Equal(t, "integer", header.Type)
    49  			assert.Equal(t, "int64", header.Format)
    50  		case "name":
    51  			assert.Equal(t, "string", header.Type)
    52  			assert.Equal(t, "", header.Format)
    53  		case "age":
    54  			assert.Equal(t, "integer", header.Type)
    55  			assert.Equal(t, "int32", header.Format)
    56  		case "notes":
    57  			assert.Equal(t, "string", header.Type)
    58  			assert.Equal(t, "", header.Format)
    59  		case "extra":
    60  			assert.Equal(t, "string", header.Type)
    61  			assert.Equal(t, "", header.Format)
    62  		case "createdAt":
    63  			assert.Equal(t, "string", header.Type)
    64  			assert.Equal(t, "date-time", header.Format)
    65  		case "NoTagName":
    66  			assert.Equal(t, "string", header.Type)
    67  			assert.Equal(t, "", header.Format)
    68  		default:
    69  			assert.Fail(t, "unknown header: "+k)
    70  		}
    71  	}
    72  
    73  	cpr, ok := responses["complexerPointerOne"]
    74  	assert.True(t, ok)
    75  	assert.Len(t, cpr.Headers, 4)
    76  	for k, header := range cpr.Headers {
    77  		switch k {
    78  		case "id":
    79  			assert.Equal(t, "integer", header.Type)
    80  			assert.Equal(t, "int64", header.Format)
    81  		case "name":
    82  			assert.Equal(t, "string", header.Type)
    83  			assert.Equal(t, "", header.Format)
    84  		case "age":
    85  			assert.Equal(t, "integer", header.Type)
    86  			assert.Equal(t, "int32", header.Format)
    87  		case "extra":
    88  			assert.Equal(t, "integer", header.Type)
    89  			assert.Equal(t, "int64", header.Format)
    90  		default:
    91  			assert.Fail(t, "unknown header: "+k)
    92  		}
    93  	}
    94  
    95  	sos, ok := responses["simpleOnes"]
    96  	assert.True(t, ok)
    97  	assert.Len(t, sos.Headers, 1)
    98  
    99  	sosf, ok := responses["simpleOnesFunc"]
   100  	assert.True(t, ok)
   101  	assert.Len(t, sosf.Headers, 1)
   102  
   103  	res, ok := responses["someResponse"]
   104  	assert.True(t, ok)
   105  	assert.Len(t, res.Headers, 7)
   106  
   107  	for k, header := range res.Headers {
   108  		switch k {
   109  		case "id":
   110  			assert.Equal(t, "ID of this some response instance.\nids in this application start at 11 and are smaller than 1000", header.Description)
   111  			assert.Equal(t, "integer", header.Type)
   112  			assert.Equal(t, "int64", header.Format)
   113  			//assert.Equal(t, "ID", header.Extensions["x-go-name"])
   114  			assert.EqualValues(t, 1000, *header.Maximum)
   115  			assert.True(t, header.ExclusiveMaximum)
   116  			assert.EqualValues(t, 10, *header.Minimum)
   117  			assert.True(t, header.ExclusiveMinimum)
   118  			assert.Equal(t, 11, header.Default, "ID default value is incorrect")
   119  
   120  		case "score":
   121  			assert.Equal(t, "The Score of this model", header.Description)
   122  			assert.Equal(t, "integer", header.Type)
   123  			assert.Equal(t, "int32", header.Format)
   124  			//assert.Equal(t, "Score", header.Extensions["x-go-name"])
   125  			assert.EqualValues(t, 45, *header.Maximum)
   126  			assert.False(t, header.ExclusiveMaximum)
   127  			assert.EqualValues(t, 3, *header.Minimum)
   128  			assert.False(t, header.ExclusiveMinimum)
   129  			assert.Equal(t, 27, header.Example)
   130  
   131  		case "x-hdr-name":
   132  			assert.Equal(t, "Name of this some response instance", header.Description)
   133  			assert.Equal(t, "string", header.Type)
   134  			//assert.Equal(t, "Name", header.Extensions["x-go-name"])
   135  			assert.EqualValues(t, 4, *header.MinLength)
   136  			assert.EqualValues(t, 50, *header.MaxLength)
   137  			assert.Equal(t, "[A-Za-z0-9-.]*", header.Pattern)
   138  
   139  		case "active":
   140  			assert.Equal(t, "Active state of the record", header.Description)
   141  			assert.Equal(t, "boolean", header.Type)
   142  			assert.Equal(t, true, header.Default)
   143  
   144  		case "created":
   145  			assert.Equal(t, "Created holds the time when this entry was created", header.Description)
   146  			assert.Equal(t, "string", header.Type)
   147  			assert.Equal(t, "date-time", header.Format)
   148  			//assert.Equal(t, "Created", header.Extensions["x-go-name"])
   149  
   150  		case "foo_slice":
   151  			assert.Equal(t, "a FooSlice has foos which are strings", header.Description)
   152  			//assert.Equal(t, "FooSlice", header.Extensions["x-go-name"])
   153  			assert.Equal(t, "array", header.Type)
   154  			assert.True(t, header.UniqueItems)
   155  			assert.Equal(t, "pipe", header.CollectionFormat)
   156  			assert.NotNil(t, header.Items, "foo_slice should have had an items property")
   157  			assert.EqualValues(t, 3, *header.MinItems, "'foo_slice' should have had 3 min items")
   158  			assert.EqualValues(t, 10, *header.MaxItems, "'foo_slice' should have had 10 max items")
   159  			itprop := header.Items
   160  			assert.EqualValues(t, 3, *itprop.MinLength, "'foo_slice.items.minLength' should have been 3")
   161  			assert.EqualValues(t, 10, *itprop.MaxLength, "'foo_slice.items.maxLength' should have been 10")
   162  			assert.EqualValues(t, "\\w+", itprop.Pattern, "'foo_slice.items.pattern' should have \\w+")
   163  			assert.Equal(t, "foo", itprop.Example)
   164  
   165  		case "bar_slice":
   166  			assert.Equal(t, "a BarSlice has bars which are strings", header.Description)
   167  			assert.Equal(t, "array", header.Type)
   168  			assert.True(t, header.UniqueItems)
   169  			assert.Equal(t, "pipe", header.CollectionFormat)
   170  			assert.NotNil(t, header.Items, "bar_slice should have had an items property")
   171  			assert.EqualValues(t, 3, *header.MinItems, "'bar_slice' should have had 3 min items")
   172  			assert.EqualValues(t, 10, *header.MaxItems, "'bar_slice' should have had 10 max items")
   173  			itprop := header.Items
   174  			if assert.NotNil(t, itprop) {
   175  				assert.EqualValues(t, 4, *itprop.MinItems, "'bar_slice.items.minItems' should have been 4")
   176  				assert.EqualValues(t, 9, *itprop.MaxItems, "'bar_slice.items.maxItems' should have been 9")
   177  				itprop2 := itprop.Items
   178  				if assert.NotNil(t, itprop2) {
   179  					assert.EqualValues(t, 5, *itprop2.MinItems, "'bar_slice.items.items.minItems' should have been 5")
   180  					assert.EqualValues(t, 8, *itprop2.MaxItems, "'bar_slice.items.items.maxItems' should have been 8")
   181  					itprop3 := itprop2.Items
   182  					if assert.NotNil(t, itprop3) {
   183  						assert.EqualValues(t, 3, *itprop3.MinLength, "'bar_slice.items.items.items.minLength' should have been 3")
   184  						assert.EqualValues(t, 10, *itprop3.MaxLength, "'bar_slice.items.items.items.maxLength' should have been 10")
   185  						assert.EqualValues(t, "\\w+", itprop3.Pattern, "'bar_slice.items.items.items.pattern' should have \\w+")
   186  					}
   187  				}
   188  			}
   189  
   190  		default:
   191  			assert.Fail(t, "unknown property: "+k)
   192  		}
   193  	}
   194  
   195  	assert.NotNil(t, res.Schema)
   196  	aprop := res.Schema
   197  	assert.Equal(t, "array", aprop.Type[0])
   198  	assert.NotNil(t, aprop.Items)
   199  	assert.NotNil(t, aprop.Items.Schema)
   200  	itprop := aprop.Items.Schema
   201  	assert.Len(t, itprop.Properties, 4)
   202  	assert.Len(t, itprop.Required, 3)
   203  	assertProperty(t, itprop, "integer", "id", "int32", "ID")
   204  	iprop, ok := itprop.Properties["id"]
   205  	assert.True(t, ok)
   206  	assert.Equal(t, "ID of this some response instance.\nids in this application start at 11 and are smaller than 1000", iprop.Description)
   207  	assert.EqualValues(t, 1000, *iprop.Maximum)
   208  	assert.True(t, iprop.ExclusiveMaximum, "'id' should have had an exclusive maximum")
   209  	assert.NotNil(t, iprop.Minimum)
   210  	assert.EqualValues(t, 10, *iprop.Minimum)
   211  	assert.True(t, iprop.ExclusiveMinimum, "'id' should have had an exclusive minimum")
   212  
   213  	assertRef(t, itprop, "pet", "Pet", "#/definitions/pet")
   214  	iprop, ok = itprop.Properties["pet"]
   215  	assert.True(t, ok)
   216  	// if itprop.Ref.String() == "" {
   217  	// 	assert.Equal(t, "The Pet to add to this NoModel items bucket.\nPets can appear more than once in the bucket", iprop.Description)
   218  	// }
   219  
   220  	assertProperty(t, itprop, "integer", "quantity", "int16", "Quantity")
   221  	iprop, ok = itprop.Properties["quantity"]
   222  	assert.True(t, ok)
   223  	assert.Equal(t, "The amount of pets to add to this bucket.", iprop.Description)
   224  	assert.EqualValues(t, 1, *iprop.Minimum)
   225  	assert.EqualValues(t, 10, *iprop.Maximum)
   226  
   227  	assertProperty(t, itprop, "string", "notes", "", "Notes")
   228  	iprop, ok = itprop.Properties["notes"]
   229  	assert.True(t, ok)
   230  	assert.Equal(t, "Notes to add to this item.\nThis can be used to add special instructions.", iprop.Description)
   231  
   232  	res, ok = responses["resp"]
   233  	assert.True(t, ok)
   234  	assert.NotNil(t, res.Schema)
   235  	assert.Equal(t, "#/definitions/user", res.Schema.Ref.String())
   236  }