|
| 1 | +package openapi3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func TestIssue495(t *testing.T) { |
| 10 | + { |
| 11 | + spec := []byte(` |
| 12 | +openapi: 3.0.1 |
| 13 | +info: |
| 14 | + version: v1 |
| 15 | + title: Products api |
| 16 | +components: |
| 17 | + schemas: |
| 18 | + someSchema: |
| 19 | + type: object |
| 20 | + schemaArray: |
| 21 | + type: array |
| 22 | + minItems: 1 |
| 23 | + items: |
| 24 | + $ref: '#' |
| 25 | +paths: |
| 26 | + /categories: |
| 27 | + get: |
| 28 | + responses: |
| 29 | + '200': |
| 30 | + description: '' |
| 31 | + content: |
| 32 | + application/json: |
| 33 | + schema: |
| 34 | + properties: |
| 35 | + allOf: |
| 36 | + $ref: '#/components/schemas/schemaArray' |
| 37 | +`[1:]) |
| 38 | + |
| 39 | + sl := NewLoader() |
| 40 | + |
| 41 | + doc, err := sl.LoadFromData(spec) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + err = doc.Validate(sl.Context) |
| 45 | + require.EqualError(t, err, `invalid components: schema "schemaArray": found unresolved ref: "#"`) |
| 46 | + } |
| 47 | + |
| 48 | + spec := []byte(` |
| 49 | +openapi: 3.0.1 |
| 50 | +info: |
| 51 | + version: v1 |
| 52 | + title: Products api |
| 53 | +components: |
| 54 | + schemas: |
| 55 | + someSchema: |
| 56 | + type: object |
| 57 | + schemaArray: |
| 58 | + type: array |
| 59 | + minItems: 1 |
| 60 | + items: |
| 61 | + $ref: '#/components/schemas/someSchema' |
| 62 | +paths: |
| 63 | + /categories: |
| 64 | + get: |
| 65 | + responses: |
| 66 | + '200': |
| 67 | + description: '' |
| 68 | + content: |
| 69 | + application/json: |
| 70 | + schema: |
| 71 | + properties: |
| 72 | + allOf: |
| 73 | + $ref: '#/components/schemas/schemaArray' |
| 74 | +`[1:]) |
| 75 | + |
| 76 | + sl := NewLoader() |
| 77 | + |
| 78 | + doc, err := sl.LoadFromData(spec) |
| 79 | + require.NoError(t, err) |
| 80 | + |
| 81 | + err = doc.Validate(sl.Context) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + require.Equal(t, &Schema{Type: "object"}, doc.Components.Schemas["schemaArray"].Value.Items.Value) |
| 85 | +} |
| 86 | + |
| 87 | +func TestIssue495WithDraft04(t *testing.T) { |
| 88 | + spec := []byte(` |
| 89 | +openapi: 3.0.1 |
| 90 | +servers: |
| 91 | +- url: http://localhost:5000 |
| 92 | +info: |
| 93 | + version: v1 |
| 94 | + title: Products api |
| 95 | + contact: |
| 96 | + name: me |
| 97 | + |
| 98 | + description: This is a sample |
| 99 | +paths: |
| 100 | + /categories: |
| 101 | + get: |
| 102 | + summary: Provides the available categories for the store |
| 103 | + operationId: list-categories |
| 104 | + responses: |
| 105 | + '200': |
| 106 | + description: this is a desc |
| 107 | + content: |
| 108 | + application/json: |
| 109 | + schema: |
| 110 | + $ref: http://json-schema.org/draft-04/schema |
| 111 | +`[1:]) |
| 112 | + |
| 113 | + sl := NewLoader() |
| 114 | + sl.IsExternalRefsAllowed = true |
| 115 | + |
| 116 | + doc, err := sl.LoadFromData(spec) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + err = doc.Validate(sl.Context) |
| 120 | + require.ErrorContains(t, err, `found unresolved ref: "#"`) |
| 121 | +} |
| 122 | + |
| 123 | +func TestIssue495WithDraft04Bis(t *testing.T) { |
| 124 | + spec := []byte(` |
| 125 | +openapi: 3.0.1 |
| 126 | +servers: |
| 127 | +- url: http://localhost:5000 |
| 128 | +info: |
| 129 | + version: v1 |
| 130 | + title: Products api |
| 131 | + contact: |
| 132 | + name: me |
| 133 | + |
| 134 | + description: This is a sample |
| 135 | +paths: |
| 136 | + /categories: |
| 137 | + get: |
| 138 | + summary: Provides the available categories for the store |
| 139 | + operationId: list-categories |
| 140 | + responses: |
| 141 | + '200': |
| 142 | + description: this is a desc |
| 143 | + content: |
| 144 | + application/json: |
| 145 | + schema: |
| 146 | + $ref: testdata/draft04.yml |
| 147 | +`[1:]) |
| 148 | + |
| 149 | + sl := NewLoader() |
| 150 | + sl.IsExternalRefsAllowed = true |
| 151 | + |
| 152 | + doc, err := sl.LoadFromData(spec) |
| 153 | + require.NoError(t, err) |
| 154 | + |
| 155 | + err = doc.Validate(sl.Context) |
| 156 | + require.ErrorContains(t, err, `found unresolved ref: "#"`) |
| 157 | +} |
0 commit comments