-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
221 lines (198 loc) · 6.01 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package rest
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"path"
"strings"
"github.com/hasura/ndc-rest-schema/openapi"
rest "github.com/hasura/ndc-rest-schema/schema"
"github.com/hasura/ndc-rest-schema/utils"
"github.com/hasura/ndc-sdk-go/schema"
"gopkg.in/yaml.v3"
)
// GetSchema gets the connector's schema.
func (c *RESTConnector) GetSchema(ctx context.Context, configuration *Configuration, _ *State) (schema.SchemaResponseMarshaler, error) {
return c.rawSchema, nil
}
// build NDC REST schema from file list
func buildSchemaFiles(configDir string, files []SchemaFile, logger *slog.Logger) ([]ndcRestSchemaWithName, map[string][]string) {
schemas := make([]ndcRestSchemaWithName, len(files))
errors := make(map[string][]string)
for i, file := range files {
var errs []string
schemaOutput, err := buildSchemaFile(configDir, &file, logger)
if err != nil {
errs = append(errs, err.Error())
}
if schemaOutput != nil {
schemas[i] = ndcRestSchemaWithName{
name: file.Path,
schema: schemaOutput,
}
}
if len(errs) > 0 {
errors[file.Path] = errs
}
}
return schemas, errors
}
func buildSchemaFile(configDir string, conf *SchemaFile, logger *slog.Logger) (*rest.NDCRestSchema, error) {
if conf.Path == "" {
return nil, errors.New("file path is empty")
}
filePath := conf.Path
if !strings.HasPrefix(conf.Path, "/") && !strings.HasPrefix(conf.Path, "http") {
filePath = path.Join(configDir, conf.Path)
}
rawBytes, err := utils.ReadFileFromPath(filePath)
if err != nil {
return nil, err
}
switch conf.Spec {
case rest.NDCSpec:
var result rest.NDCRestSchema
fileFormat, err := rest.ParseSchemaFileFormat(strings.Trim(path.Ext(conf.Path), "."))
if err != nil {
return nil, err
}
switch fileFormat {
case rest.SchemaFileJSON:
if err := json.Unmarshal(rawBytes, &result); err != nil {
return nil, err
}
case rest.SchemaFileYAML:
if err := yaml.Unmarshal(rawBytes, &result); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("invalid file format: %s", fileFormat)
}
return &result, nil
case rest.OpenAPIv2Spec, rest.OAS2Spec:
result, errs := openapi.OpenAPIv2ToNDCSchema(rawBytes, &openapi.ConvertOptions{
MethodAlias: conf.MethodAlias,
TrimPrefix: conf.TrimPrefix,
})
if result != nil {
if len(errs) > 0 {
logger.Warn("some errors happened when parsing OpenAPI", slog.Any("errors", errs))
}
return result, nil
}
return nil, errors.Join(errs...)
case rest.OpenAPIv3Spec, rest.OAS3Spec:
result, errs := openapi.OpenAPIv3ToNDCSchema(rawBytes, &openapi.ConvertOptions{
MethodAlias: conf.MethodAlias,
TrimPrefix: conf.TrimPrefix,
})
if result != nil {
if len(errs) > 0 {
logger.Warn("some errors happened when parsing OpenAPI", slog.Any("errors", errs))
}
return result, nil
}
return nil, errors.Join(errs...)
default:
return nil, fmt.Errorf("invalid configuration spec: %s", conf.Spec)
}
}
func (c *RESTConnector) applyNDCRestSchemas(schemas []ndcRestSchemaWithName) map[string][]string {
ndcSchema := &schema.SchemaResponse{
Collections: []schema.CollectionInfo{},
ScalarTypes: make(schema.SchemaResponseScalarTypes),
ObjectTypes: make(schema.SchemaResponseObjectTypes),
}
functions := map[string]rest.RESTFunctionInfo{}
procedures := map[string]rest.RESTProcedureInfo{}
errors := make(map[string][]string)
for _, item := range schemas {
settings := item.schema.Settings
if settings == nil {
settings = &rest.NDCRestSettings{}
}
meta := RESTMetadata{
settings: settings,
}
var errs []string
for name, scalar := range item.schema.ScalarTypes {
ndcSchema.ScalarTypes[name] = scalar
}
for name, object := range item.schema.ObjectTypes {
ndcSchema.ObjectTypes[name] = object
}
ndcSchema.Collections = append(ndcSchema.Collections, item.schema.Collections...)
var functionSchemas []schema.FunctionInfo
var procedureSchemas []schema.ProcedureInfo
for _, fnItem := range item.schema.Functions {
if fnItem.Request == nil || fnItem.Request.URL == "" {
continue
}
req, err := validateRequestSchema(fnItem.Request, "get")
if err != nil {
errs = append(errs, fmt.Sprintf("function %s: %s", fnItem.Name, err))
continue
}
fn := rest.RESTFunctionInfo{
Request: req,
FunctionInfo: fnItem.FunctionInfo,
}
functions[fnItem.Name] = fn
functionSchemas = append(functionSchemas, fn.FunctionInfo)
}
for _, procItem := range item.schema.Procedures {
if procItem.Request == nil || procItem.Request.URL == "" {
continue
}
req, err := validateRequestSchema(procItem.Request, "")
if err != nil {
errs = append(errs, fmt.Sprintf("procedure %s: %s", procItem.Name, err))
continue
}
procedures[procItem.Name] = rest.RESTProcedureInfo{
Request: req,
ProcedureInfo: procItem.ProcedureInfo,
}
procedureSchemas = append(procedureSchemas, procItem.ProcedureInfo)
}
if len(errs) > 0 {
errors[item.name] = errs
continue
}
ndcSchema.Functions = append(ndcSchema.Functions, functionSchemas...)
ndcSchema.Procedures = append(ndcSchema.Procedures, procedureSchemas...)
meta.functions = functions
meta.procedures = procedures
c.metadata = append(c.metadata, meta)
}
schemaBytes, err := json.Marshal(ndcSchema)
if err != nil {
errors["schema"] = []string{err.Error()}
}
if len(errors) > 0 {
return errors
}
c.schema = &schema.SchemaResponse{
ScalarTypes: ndcSchema.ScalarTypes,
ObjectTypes: ndcSchema.ObjectTypes,
}
c.rawSchema = schema.NewRawSchemaResponseUnsafe(schemaBytes)
return nil
}
func validateRequestSchema(req *rest.Request, defaultMethod string) (*rest.Request, error) {
if req.Method == "" {
if defaultMethod == "" {
return nil, fmt.Errorf("the HTTP method is required")
}
req.Method = defaultMethod
}
if req.Type == "" {
req.Type = rest.RequestTypeREST
}
return req, nil
}
func printSchemaValidationError(logger *slog.Logger, errors map[string][]string) {
logger.Error("errors happen when validating NDC REST schemas", slog.Any("errors", errors))
}