|
| 1 | +// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package validate |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/go-openapi/analysis" |
| 12 | + "github.com/go-openapi/loads" |
| 13 | + "github.com/go-openapi/spec" |
| 14 | + "github.com/go-openapi/strfmt" |
| 15 | + "github.com/go-openapi/testify/v2/assert" |
| 16 | + "github.com/go-openapi/testify/v2/require" |
| 17 | +) |
| 18 | + |
| 19 | +// collectionFormatSpec builds a specification whose only operation declares the given parameters |
| 20 | +// and the given headers on its 200 response. |
| 21 | +func collectionFormatSpec(parameters, headers string) string { |
| 22 | + return fmt.Sprintf(`{ |
| 23 | + "swagger": "2.0", |
| 24 | + "info": {"title": "t", "version": "1"}, |
| 25 | + "paths": { |
| 26 | + "/x": { |
| 27 | + "get": { |
| 28 | + "parameters": [%s], |
| 29 | + "responses": {"200": {"description": "ok", "headers": {%s}}} |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + }`, parameters, headers) |
| 34 | +} |
| 35 | + |
| 36 | +// collectionFormatValidatorFromJSON builds a SpecValidator wired the way Validate does, up to the |
| 37 | +// point validateCollectionFormats needs. |
| 38 | +func collectionFormatValidatorFromJSON(t *testing.T, doc string) *SpecValidator { |
| 39 | + t.Helper() |
| 40 | + |
| 41 | + d, err := loads.Analyzed(json.RawMessage(doc), "") |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + s := NewSpecValidator(d.Schema(), strfmt.Default) |
| 45 | + s.spec = d |
| 46 | + s.analyzer = analysis.New(d.Spec()) |
| 47 | + s.paramLocations = newParamLocations(d.Spec()) |
| 48 | + |
| 49 | + return s |
| 50 | +} |
| 51 | + |
| 52 | +func TestValidateCollectionFormats(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + tests := []struct { |
| 56 | + name string |
| 57 | + parameters string |
| 58 | + headers string |
| 59 | + wantWarnings []string |
| 60 | + }{ |
| 61 | + { |
| 62 | + name: "collectionFormat on an array", |
| 63 | + parameters: `{"name":"q","in":"query","type":"array","items":{"type":"string"},"collectionFormat":"pipes"}`, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "array without a collectionFormat", |
| 67 | + parameters: `{"name":"q","in":"query","type":"array","items":{"type":"string"}}`, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "no collectionFormat anywhere", |
| 71 | + parameters: `{"name":"q","in":"query","type":"string"}`, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "collectionFormat on a string parameter", |
| 75 | + parameters: `{"name":"q","in":"query","type":"string","collectionFormat":"csv"}`, |
| 76 | + wantWarnings: []string{ |
| 77 | + `collectionFormat "csv" is ignored in parameter "q": it joins the members of an array, and the type is "string"`, |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "collectionFormat on an integer parameter", |
| 82 | + parameters: `{"name":"q","in":"query","type":"integer","collectionFormat":"pipes"}`, |
| 83 | + wantWarnings: []string{ |
| 84 | + `collectionFormat "pipes" is ignored in parameter "q": it joins the members of an array, and the type is "integer"`, |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "collectionFormat on a file parameter", |
| 89 | + parameters: `{"name":"f","in":"formData","type":"file","collectionFormat":"csv"}`, |
| 90 | + wantWarnings: []string{ |
| 91 | + `collectionFormat "csv" is ignored in parameter "f": it joins the members of an array, and the type is "file"`, |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "collectionFormat on the items of an array", |
| 96 | + parameters: `{"name":"q","in":"query","type":"array","items":{"type":"string","collectionFormat":"ssv"}}`, |
| 97 | + wantWarnings: []string{ |
| 98 | + `collectionFormat "ssv" is ignored in items of parameter "q": it joins the members of an array, and the type is "string"`, |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "collectionFormat on the items of an array of arrays", |
| 103 | + parameters: `{"name":"q","in":"query","type":"array","items":{"type":"array","items":{"type":"string"},"collectionFormat":"tsv"}}`, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "collectionFormat on a string response header", |
| 107 | + headers: `"X":{"type":"string","collectionFormat":"csv"}`, |
| 108 | + wantWarnings: []string{ |
| 109 | + `collectionFormat "csv" is ignored in header "X": it joins the members of an array, and the type is "string"`, |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "collectionFormat on an array response header", |
| 114 | + headers: `"X":{"type":"array","items":{"type":"string"},"collectionFormat":"csv"}`, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "collectionFormat on the items of a response header", |
| 118 | + headers: `"X":{"type":"array","items":{"type":"integer","collectionFormat":"pipes"}}`, |
| 119 | + wantWarnings: []string{ |
| 120 | + `collectionFormat "pipes" is ignored in items of header "X": it joins the members of an array, and the type is "integer"`, |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + for _, tc := range tests { |
| 126 | + t.Run(tc.name, func(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + doc := collectionFormatSpec(tc.parameters, tc.headers) |
| 130 | + res := collectionFormatValidatorFromJSON(t, doc).validateCollectionFormats() |
| 131 | + |
| 132 | + assert.Equal(t, tc.wantWarnings, nonEmpty(warningMessages(res))) |
| 133 | + assert.Empty(t, errorMessages(res), "the rule reports warnings only") |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// TestCollectionFormatKeepsSpecValid holds the rule to a warning: Swagger 2.0 says where |
| 139 | +// collectionFormat applies without forbidding it elsewhere, so a specification that writes one on a |
| 140 | +// string stays valid. |
| 141 | +func TestCollectionFormatKeepsSpecValid(t *testing.T) { |
| 142 | + t.Parallel() |
| 143 | + |
| 144 | + doc := collectionFormatSpec(`{"name":"q","in":"query","type":"string","collectionFormat":"csv"}`, "") |
| 145 | + |
| 146 | + d, err := loads.Analyzed(json.RawMessage(doc), "") |
| 147 | + require.NoError(t, err) |
| 148 | + |
| 149 | + require.NoError(t, Spec(d, strfmt.Default)) |
| 150 | + |
| 151 | + _, warns := NewSpecValidator(d.Schema(), strfmt.Default).Validate(d) |
| 152 | + assert.SliceContainsT(t, errorMessages(warns), |
| 153 | + `collectionFormat "csv" is ignored in parameter "q": it joins the members of an array, and the type is "string"`) |
| 154 | +} |
| 155 | + |
| 156 | +// TestCollectionFormatLocations pins down where the warning says it happened. |
| 157 | +func TestCollectionFormatLocations(t *testing.T) { |
| 158 | + t.Parallel() |
| 159 | + |
| 160 | + doc := collectionFormatSpec( |
| 161 | + `{"name":"q","in":"query","type":"array","items":{"type":"string","collectionFormat":"ssv"}}`, |
| 162 | + `"X":{"type":"string","collectionFormat":"csv"}`, |
| 163 | + ) |
| 164 | + |
| 165 | + d, err := loads.Analyzed(json.RawMessage(doc), "") |
| 166 | + require.NoError(t, err) |
| 167 | + |
| 168 | + _, warns := NewSpecValidator(d.Schema(), strfmt.Default).Validate(d) |
| 169 | + |
| 170 | + pointers := pointersOf(warns.LocatedErrors()) |
| 171 | + assert.SliceContainsT(t, pointers, "/paths/~1x/get/parameters/0/items/collectionFormat") |
| 172 | + assert.SliceContainsT(t, pointers, "/paths/~1x/get/responses/200/headers/X/collectionFormat") |
| 173 | +} |
| 174 | + |
| 175 | +// TestCollectionFormatThroughRef covers a parameter reached through a $ref: the walk sees it |
| 176 | +// expanded, so the warning is reported just as it is for one written in place. |
| 177 | +func TestCollectionFormatThroughRef(t *testing.T) { |
| 178 | + t.Parallel() |
| 179 | + |
| 180 | + doc := `{ |
| 181 | + "swagger": "2.0", |
| 182 | + "info": {"title": "t", "version": "1"}, |
| 183 | + "parameters": { |
| 184 | + "Q": {"name": "q", "in": "query", "type": "string", "collectionFormat": "csv"} |
| 185 | + }, |
| 186 | + "paths": { |
| 187 | + "/x": { |
| 188 | + "get": { |
| 189 | + "parameters": [{"$ref": "#/parameters/Q"}], |
| 190 | + "responses": {"200": {"description": "ok"}} |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + }` |
| 195 | + |
| 196 | + res := collectionFormatValidatorFromJSON(t, doc).validateCollectionFormats() |
| 197 | + assert.Equal(t, []string{ |
| 198 | + `collectionFormat "csv" is ignored in parameter "q": it joins the members of an array, and the type is "string"`, |
| 199 | + }, warningMessages(res)) |
| 200 | +} |
| 201 | + |
| 202 | +// TestCollectionFormatSkipsBodyParameters guards the one location the meta-schema handles on its |
| 203 | +// own: a body parameter may not carry a collectionFormat at all, and that is an error rather than |
| 204 | +// this warning. |
| 205 | +func TestCollectionFormatSkipsBodyParameters(t *testing.T) { |
| 206 | + t.Parallel() |
| 207 | + |
| 208 | + doc := collectionFormatSpec(`{"name":"b","in":"body","schema":{"type":"string"}}`, "") |
| 209 | + |
| 210 | + res := collectionFormatValidatorFromJSON(t, doc).validateCollectionFormats() |
| 211 | + assert.Empty(t, warningMessages(res)) |
| 212 | +} |
| 213 | + |
| 214 | +// TestCollectionFormatIsNotASchemaKeyword holds the rule to what it walks. collectionFormat is a |
| 215 | +// member of [spec.SimpleSchema], which a parameter, a header and an items carry; [spec.Schema] has |
| 216 | +// none, so validating an ordinary JSON schema can never reach this rule. |
| 217 | +// |
| 218 | +// The guard is on the type, not on the walk: a compile-time field access is what would break if a |
| 219 | +// later version of go-openapi/spec moved the member onto Schema. |
| 220 | +func TestCollectionFormatIsNotASchemaKeyword(t *testing.T) { |
| 221 | + t.Parallel() |
| 222 | + |
| 223 | + var ( |
| 224 | + _ = spec.Items{}.CollectionFormat |
| 225 | + _ = spec.Header{}.CollectionFormat |
| 226 | + _ = spec.Parameter{}.CollectionFormat |
| 227 | + ) |
| 228 | + |
| 229 | + raw, err := json.Marshal(spec.Schema{ |
| 230 | + SchemaProps: spec.SchemaProps{Type: spec.StringOrArray{stringType}}, |
| 231 | + }) |
| 232 | + require.NoError(t, err) |
| 233 | + |
| 234 | + var members map[string]any |
| 235 | + require.NoError(t, json.Unmarshal(raw, &members)) |
| 236 | + assert.MapNotContainsT(t, members, swaggerCollectionFormat) |
| 237 | + |
| 238 | + // and validating data against a schema reports nothing of the sort |
| 239 | + schema := new(spec.Schema) |
| 240 | + require.NoError(t, json.Unmarshal([]byte(`{"type": "string"}`), schema)) |
| 241 | + require.NoError(t, AgainstSchema(schema, "a,b,c", strfmt.Default)) |
| 242 | +} |
0 commit comments