|
| 1 | +package openapi3_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + "github.com/getkin/kin-openapi/openapi3" |
| 9 | +) |
| 10 | + |
| 11 | +func TestOneOf_Warning_Errors(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + loader := openapi3.NewLoader() |
| 15 | + spec := ` |
| 16 | +components: |
| 17 | + schemas: |
| 18 | + Something: |
| 19 | + type: object |
| 20 | + properties: |
| 21 | + field: |
| 22 | + title: Some field |
| 23 | + oneOf: |
| 24 | + - title: First rule |
| 25 | + type: string |
| 26 | + minLength: 10 |
| 27 | + maxLength: 10 |
| 28 | + - title: Second rule |
| 29 | + type: string |
| 30 | + minLength: 15 |
| 31 | + maxLength: 15 |
| 32 | +`[1:] |
| 33 | + |
| 34 | + doc, err := loader.LoadFromData([]byte(spec)) |
| 35 | + require.NoError(t, err) |
| 36 | + |
| 37 | + tests := [...]struct { |
| 38 | + name string |
| 39 | + value string |
| 40 | + checkErr require.ErrorAssertionFunc |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "valid value", |
| 44 | + value: "ABCDE01234", |
| 45 | + checkErr: require.NoError, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "valid value", |
| 49 | + value: "ABCDE0123456789", |
| 50 | + checkErr: require.NoError, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "no valid value", |
| 54 | + value: "ABCDE", |
| 55 | + checkErr: func(t require.TestingT, err error, i ...interface{}) { |
| 56 | + require.Equal(t, "doesn't match schema due to: minimum string length is 10\nSchema:\n {\n \"maxLength\": 10,\n \"minLength\": 10,\n \"title\": \"First rule\",\n \"type\": \"string\"\n }\n\nValue:\n \"ABCDE\"\n Or minimum string length is 15\nSchema:\n {\n \"maxLength\": 15,\n \"minLength\": 15,\n \"title\": \"Second rule\",\n \"type\": \"string\"\n }\n\nValue:\n \"ABCDE\"\n", err.Error()) |
| 57 | + |
| 58 | + wErr := &openapi3.MultiError{} |
| 59 | + require.ErrorAs(t, err, wErr) |
| 60 | + |
| 61 | + require.Len(t, *wErr, 2) |
| 62 | + |
| 63 | + require.Equal(t, "minimum string length is 10", (*wErr)[0].(*openapi3.SchemaError).Reason) |
| 64 | + require.Equal(t, "minimum string length is 15", (*wErr)[1].(*openapi3.SchemaError).Reason) |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, test := range tests { |
| 70 | + test := test |
| 71 | + t.Run(test.name, func(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + err = doc.Components.Schemas["Something"].Value.Properties["field"].Value.VisitJSON(test.value) |
| 75 | + |
| 76 | + test.checkErr(t, err) |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
0 commit comments