parameters_test.go 4.6 KB

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