|
| 1 | +package openapi3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// Error() text format: "invalid <section>: <inner>" — byte-identical to |
| 12 | +// the fmt.Errorf wrapper this type replaced. |
| 13 | +func TestSectionValidationError_Error(t *testing.T) { |
| 14 | + inner := errors.New("value of version must be a non-empty string") |
| 15 | + e := &SectionValidationError{Section: "info", Cause: inner} |
| 16 | + require.Equal(t, "invalid info: value of version must be a non-empty string", e.Error()) |
| 17 | +} |
| 18 | + |
| 19 | +// Unwrap returns the inner error so errors.Is / errors.As walk the chain. |
| 20 | +func TestSectionValidationError_Unwrap(t *testing.T) { |
| 21 | + sentinel := errors.New("sentinel") |
| 22 | + e := &SectionValidationError{Section: "components", Cause: sentinel} |
| 23 | + require.True(t, errors.Is(e, sentinel)) |
| 24 | +} |
| 25 | + |
| 26 | +func TestPathValidationError_Error(t *testing.T) { |
| 27 | + inner := errors.New("bad path item") |
| 28 | + e := &PathValidationError{Path: "/users/{id}", Cause: inner} |
| 29 | + require.Equal(t, "invalid path /users/{id}: bad path item", e.Error()) |
| 30 | +} |
| 31 | + |
| 32 | +func TestPathValidationError_Unwrap(t *testing.T) { |
| 33 | + sentinel := errors.New("sentinel") |
| 34 | + e := &PathValidationError{Path: "/x", Cause: sentinel} |
| 35 | + require.True(t, errors.Is(e, sentinel)) |
| 36 | +} |
| 37 | + |
| 38 | +func TestOperationValidationError_Error(t *testing.T) { |
| 39 | + inner := errors.New("bad operation") |
| 40 | + e := &OperationValidationError{Method: "GET", Cause: inner} |
| 41 | + require.Equal(t, "invalid operation GET: bad operation", e.Error()) |
| 42 | +} |
| 43 | + |
| 44 | +func TestOperationValidationError_Unwrap(t *testing.T) { |
| 45 | + sentinel := errors.New("sentinel") |
| 46 | + e := &OperationValidationError{Method: "POST", Cause: sentinel} |
| 47 | + require.True(t, errors.Is(e, sentinel)) |
| 48 | +} |
| 49 | + |
| 50 | +// Three-layer chain (section + path + operation) is the typical shape |
| 51 | +// for a schema-deep validation error inside paths. errors.As against |
| 52 | +// each cluster pulls the corresponding context independently of the |
| 53 | +// others; nothing depends on the order in which the layers were |
| 54 | +// wrapped. |
| 55 | +func TestSectionPathOperationChain_AsExtraction(t *testing.T) { |
| 56 | + leaf := errors.New("field const is for OpenAPI >=3.1") |
| 57 | + chain := &SectionValidationError{ |
| 58 | + Section: "paths", |
| 59 | + Cause: &PathValidationError{ |
| 60 | + Path: "/thing", |
| 61 | + Cause: &OperationValidationError{ |
| 62 | + Method: "GET", |
| 63 | + Cause: leaf, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + var sve *SectionValidationError |
| 69 | + require.True(t, errors.As(chain, &sve)) |
| 70 | + require.Equal(t, "paths", sve.Section) |
| 71 | + |
| 72 | + var pve *PathValidationError |
| 73 | + require.True(t, errors.As(chain, &pve)) |
| 74 | + require.Equal(t, "/thing", pve.Path) |
| 75 | + |
| 76 | + var ove *OperationValidationError |
| 77 | + require.True(t, errors.As(chain, &ove)) |
| 78 | + require.Equal(t, "GET", ove.Method) |
| 79 | + |
| 80 | + // And the leaf is still reachable via errors.Is. |
| 81 | + require.True(t, errors.Is(chain, leaf)) |
| 82 | + |
| 83 | + // And the rendered string concatenates the layers exactly as the |
| 84 | + // previous fmt.Errorf chain did. |
| 85 | + require.Equal(t, |
| 86 | + "invalid paths: invalid path /thing: invalid operation GET: field const is for OpenAPI >=3.1", |
| 87 | + chain.Error(), |
| 88 | + ) |
| 89 | +} |
| 90 | + |
| 91 | +// Mixed: a SectionValidationError wrapping an arbitrary non-typed error |
| 92 | +// still renders correctly. Guards against the typed wrapper changing |
| 93 | +// behaviour when the underlying error is not one of our cluster types. |
| 94 | +func TestSectionValidationError_WrappingArbitrary(t *testing.T) { |
| 95 | + inner := fmt.Errorf("third-party error: %w", errors.New("x")) |
| 96 | + e := &SectionValidationError{Section: "webhooks", Cause: inner} |
| 97 | + require.Equal(t, "invalid webhooks: third-party error: x", e.Error()) |
| 98 | +} |
0 commit comments