Skip to content

Commit 1392e0d

Browse files
authored
vendor JSON schema to avoid network calls in tests (#1434)
1 parent 6cc3416 commit 1392e0d

File tree

2 files changed

+178
-6
lines changed

2 files changed

+178
-6
lines changed

experimental/schemabuilder/reflector_test.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package schemabuilder
22

33
import (
4+
_ "embed"
45
"encoding/json"
56
"os"
67
"reflect"
@@ -9,13 +10,18 @@ import (
910
"github.com/go-openapi/loads"
1011
"github.com/go-openapi/spec"
1112
"github.com/go-openapi/strfmt"
13+
"github.com/go-openapi/swag/loading"
1214
"github.com/go-openapi/validate"
13-
"github.com/grafana/grafana-plugin-sdk-go/data"
14-
apisdata "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
1515
"github.com/invopop/jsonschema"
1616
"github.com/stretchr/testify/require"
17+
18+
"github.com/grafana/grafana-plugin-sdk-go/data"
19+
apisdata "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
1720
)
1821

22+
//go:embed testdata/draft-04-schema.json
23+
var draft04SchemaJSON []byte
24+
1925
func TestWriteQuerySchema(t *testing.T) {
2026
builder, err := NewSchemaBuilder(BuilderOptions{
2127
PluginID: []string{"dummy"},
@@ -118,11 +124,28 @@ func validateOpenAPIv2Schema(t *testing.T, data []byte, file string) {
118124
})
119125
require.NoError(t, err, file)
120126

121-
// 2c. Load the spec structure using loads.Analyzed.
122-
doc, err := loads.Analyzed(swaggerBytes, "2.0")
123-
require.NoError(t, err)
127+
localDocLoader := func(path string, opts ...loading.Option) (json.RawMessage, error) {
128+
// Use vendored draft-04 schema to avoid network calls
129+
if path == "https://json-schema.org/draft-04/schema#" || path == "https://json-schema.org/draft-04/schema" {
130+
return draft04SchemaJSON, nil
131+
}
132+
133+
// For other paths (local files or HTTP URLs), use the default loader
134+
return loading.JSONDoc(path, opts...)
135+
}
136+
137+
tmpFile, err := os.CreateTemp("", "swagger-*.json")
138+
require.NoError(t, err, file)
139+
defer os.Remove(tmpFile.Name())
140+
141+
_, err = tmpFile.Write(swaggerBytes)
142+
require.NoError(t, err, file)
143+
err = tmpFile.Close()
144+
require.NoError(t, err, file)
145+
146+
doc, err := loads.Spec(tmpFile.Name(), loads.WithDocLoader(localDocLoader))
147+
require.NoError(t, err, file)
124148

125-
// 2d. Validate the loaded document against the official OpenAPI 2.0 meta-schema.
126149
err = validate.Spec(doc, strfmt.Default)
127150
require.NoError(t, err, file)
128151
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
{
2+
"id": "http://json-schema.org/draft-04/schema#",
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"description": "Core schema meta-schema",
5+
"definitions": {
6+
"schemaArray": {
7+
"type": "array",
8+
"minItems": 1,
9+
"items": { "$ref": "#" }
10+
},
11+
"positiveInteger": {
12+
"type": "integer",
13+
"minimum": 0
14+
},
15+
"positiveIntegerDefault0": {
16+
"allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
17+
},
18+
"simpleTypes": {
19+
"enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
20+
},
21+
"stringArray": {
22+
"type": "array",
23+
"items": { "type": "string" },
24+
"minItems": 1,
25+
"uniqueItems": true
26+
}
27+
},
28+
"type": "object",
29+
"properties": {
30+
"id": {
31+
"type": "string"
32+
},
33+
"$schema": {
34+
"type": "string"
35+
},
36+
"title": {
37+
"type": "string"
38+
},
39+
"description": {
40+
"type": "string"
41+
},
42+
"default": {},
43+
"multipleOf": {
44+
"type": "number",
45+
"minimum": 0,
46+
"exclusiveMinimum": true
47+
},
48+
"maximum": {
49+
"type": "number"
50+
},
51+
"exclusiveMaximum": {
52+
"type": "boolean",
53+
"default": false
54+
},
55+
"minimum": {
56+
"type": "number"
57+
},
58+
"exclusiveMinimum": {
59+
"type": "boolean",
60+
"default": false
61+
},
62+
"maxLength": { "$ref": "#/definitions/positiveInteger" },
63+
"minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
64+
"pattern": {
65+
"type": "string",
66+
"format": "regex"
67+
},
68+
"additionalItems": {
69+
"anyOf": [
70+
{ "type": "boolean" },
71+
{ "$ref": "#" }
72+
],
73+
"default": {}
74+
},
75+
"items": {
76+
"anyOf": [
77+
{ "$ref": "#" },
78+
{ "$ref": "#/definitions/schemaArray" }
79+
],
80+
"default": {}
81+
},
82+
"maxItems": { "$ref": "#/definitions/positiveInteger" },
83+
"minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
84+
"uniqueItems": {
85+
"type": "boolean",
86+
"default": false
87+
},
88+
"maxProperties": { "$ref": "#/definitions/positiveInteger" },
89+
"minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
90+
"required": { "$ref": "#/definitions/stringArray" },
91+
"additionalProperties": {
92+
"anyOf": [
93+
{ "type": "boolean" },
94+
{ "$ref": "#" }
95+
],
96+
"default": {}
97+
},
98+
"definitions": {
99+
"type": "object",
100+
"additionalProperties": { "$ref": "#" },
101+
"default": {}
102+
},
103+
"properties": {
104+
"type": "object",
105+
"additionalProperties": { "$ref": "#" },
106+
"default": {}
107+
},
108+
"patternProperties": {
109+
"type": "object",
110+
"additionalProperties": { "$ref": "#" },
111+
"default": {}
112+
},
113+
"dependencies": {
114+
"type": "object",
115+
"additionalProperties": {
116+
"anyOf": [
117+
{ "$ref": "#" },
118+
{ "$ref": "#/definitions/stringArray" }
119+
]
120+
}
121+
},
122+
"enum": {
123+
"type": "array",
124+
"minItems": 1,
125+
"uniqueItems": true
126+
},
127+
"type": {
128+
"anyOf": [
129+
{ "$ref": "#/definitions/simpleTypes" },
130+
{
131+
"type": "array",
132+
"items": { "$ref": "#/definitions/simpleTypes" },
133+
"minItems": 1,
134+
"uniqueItems": true
135+
}
136+
]
137+
},
138+
"format": { "type": "string" },
139+
"allOf": { "$ref": "#/definitions/schemaArray" },
140+
"anyOf": { "$ref": "#/definitions/schemaArray" },
141+
"oneOf": { "$ref": "#/definitions/schemaArray" },
142+
"not": { "$ref": "#" }
143+
},
144+
"dependencies": {
145+
"exclusiveMaximum": [ "maximum" ],
146+
"exclusiveMinimum": [ "minimum" ]
147+
},
148+
"default": {}
149+
}

0 commit comments

Comments
 (0)