...

Source file src/github.com/go-openapi/spec/parameters_test.go

Documentation: github.com/go-openapi/spec

     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 spec
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  
    21  	"github.com/go-openapi/swag"
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  var parameter = Parameter{
    27  	VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{
    28  		"x-framework": "swagger-go",
    29  	}},
    30  	Refable: Refable{Ref: MustCreateRef("Dog")},
    31  	CommonValidations: CommonValidations{
    32  		Maximum:          float64Ptr(100),
    33  		ExclusiveMaximum: true,
    34  		ExclusiveMinimum: true,
    35  		Minimum:          float64Ptr(5),
    36  		MaxLength:        int64Ptr(100),
    37  		MinLength:        int64Ptr(5),
    38  		Pattern:          "\\w{1,5}\\w+",
    39  		MaxItems:         int64Ptr(100),
    40  		MinItems:         int64Ptr(5),
    41  		UniqueItems:      true,
    42  		MultipleOf:       float64Ptr(5),
    43  		Enum:             []interface{}{"hello", "world"},
    44  	},
    45  	SimpleSchema: SimpleSchema{
    46  		Type:             "string",
    47  		Format:           "date",
    48  		CollectionFormat: "csv",
    49  		Items: &Items{
    50  			Refable: Refable{Ref: MustCreateRef("Cat")},
    51  		},
    52  		Default: "8",
    53  	},
    54  	ParamProps: ParamProps{
    55  		Name:        "param-name",
    56  		In:          "header",
    57  		Required:    true,
    58  		Schema:      &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}},
    59  		Description: "the description of this parameter",
    60  	},
    61  }
    62  
    63  var parameterJSON = `{
    64  	"items": {
    65  		"$ref": "Cat"
    66  	},
    67  	"x-framework": "swagger-go",
    68    "$ref": "Dog",
    69    "description": "the description of this parameter",
    70    "maximum": 100,
    71    "minimum": 5,
    72    "exclusiveMaximum": true,
    73    "exclusiveMinimum": true,
    74    "maxLength": 100,
    75    "minLength": 5,
    76    "pattern": "\\w{1,5}\\w+",
    77    "maxItems": 100,
    78    "minItems": 5,
    79    "uniqueItems": true,
    80    "multipleOf": 5,
    81    "enum": ["hello", "world"],
    82    "type": "string",
    83    "format": "date",
    84  	"name": "param-name",
    85  	"in": "header",
    86  	"required": true,
    87  	"schema": {
    88  		"type": "string"
    89  	},
    90  	"collectionFormat": "csv",
    91  	"default": "8"
    92  }`
    93  
    94  func TestIntegrationParameter(t *testing.T) {
    95  	var actual Parameter
    96  	require.NoError(t, json.Unmarshal([]byte(parameterJSON), &actual))
    97  	assert.EqualValues(t, actual, parameter)
    98  
    99  	assertParsesJSON(t, parameterJSON, parameter)
   100  }
   101  
   102  func TestParameterSerialization(t *testing.T) {
   103  	items := &Items{
   104  		SimpleSchema: SimpleSchema{Type: "string"},
   105  	}
   106  
   107  	intItems := &Items{
   108  		SimpleSchema: SimpleSchema{Type: "int", Format: "int32"},
   109  	}
   110  
   111  	assertSerializeJSON(t, QueryParam("").Typed("string", ""), `{"type":"string","in":"query"}`)
   112  
   113  	assertSerializeJSON(t,
   114  		QueryParam("").CollectionOf(items, "multi"),
   115  		`{"type":"array","items":{"type":"string"},"collectionFormat":"multi","in":"query"}`)
   116  
   117  	assertSerializeJSON(t, PathParam("").Typed("string", ""), `{"type":"string","in":"path","required":true}`)
   118  
   119  	assertSerializeJSON(t,
   120  		PathParam("").CollectionOf(items, "multi"),
   121  		`{"type":"array","items":{"type":"string"},"collectionFormat":"multi","in":"path","required":true}`)
   122  
   123  	assertSerializeJSON(t,
   124  		PathParam("").CollectionOf(intItems, "multi"),
   125  		`{"type":"array","items":{"type":"int","format":"int32"},"collectionFormat":"multi","in":"path","required":true}`)
   126  
   127  	assertSerializeJSON(t, HeaderParam("").Typed("string", ""), `{"type":"string","in":"header","required":true}`)
   128  
   129  	assertSerializeJSON(t,
   130  		HeaderParam("").CollectionOf(items, "multi"),
   131  		`{"type":"array","items":{"type":"string"},"collectionFormat":"multi","in":"header","required":true}`)
   132  	schema := &Schema{SchemaProps: SchemaProps{
   133  		Properties: map[string]Schema{
   134  			"name": {SchemaProps: SchemaProps{
   135  				Type: []string{"string"},
   136  			}},
   137  		},
   138  	}}
   139  
   140  	refSchema := &Schema{
   141  		SchemaProps: SchemaProps{Ref: MustCreateRef("Cat")},
   142  	}
   143  
   144  	assertSerializeJSON(t,
   145  		BodyParam("", schema),
   146  		`{"in":"body","schema":{"properties":{"name":{"type":"string"}}}}`)
   147  
   148  	assertSerializeJSON(t,
   149  		BodyParam("", refSchema),
   150  		`{"in":"body","schema":{"$ref":"Cat"}}`)
   151  
   152  	// array body param
   153  	assertSerializeJSON(t,
   154  		BodyParam("", ArrayProperty(RefProperty("Cat"))),
   155  		`{"in":"body","schema":{"type":"array","items":{"$ref":"Cat"}}}`)
   156  
   157  }
   158  
   159  func TestParameterGobEncoding(t *testing.T) {
   160  	var src, dst Parameter
   161  	require.NoError(t, json.Unmarshal([]byte(parameterJSON), &src))
   162  	doTestAnyGobEncoding(t, &src, &dst)
   163  }
   164  
   165  func TestParametersWithValidation(t *testing.T) {
   166  	p := new(Parameter).WithValidations(CommonValidations{MaxLength: swag.Int64(15)})
   167  	assert.EqualValues(t, swag.Int64(15), p.MaxLength)
   168  }
   169  

View as plain text