MCPcopy
hub / github.com/dosco/graphjin / Load

Function Load

core/openapi/loader.go:39–126  ·  view source on GitHub ↗

Load discovers OpenAPI specs in opts.SpecsDir, parses each one, applies per-spec user configuration, and classifies every operation. It is designed to be tolerant: a bad spec produces a warning and is dropped from the registry, but never aborts loading other specs. Per-operation classification failu

(opts LoaderOptions, configs map[string]SpecConfig, logger *log.Logger)

Source from the content-addressed store, hash-verified

37// operations are still queryable, but anything requiring auth will fail
38// at request time with a clear error.
39func Load(opts LoaderOptions, configs map[string]SpecConfig, logger *log.Logger) (*LoadResult, error) {
40 dir := opts.SpecsDir
41 if dir == "" {
42 dir = defaultSpecsDir
43 }
44
45 res := &LoadResult{Registry: &Registry{}}
46
47 entries, err := os.ReadDir(dir)
48 if err != nil {
49 if os.IsNotExist(err) {
50 // Missing dir is fine — OpenAPI integration is just dormant.
51 return res, nil
52 }
53 return nil, fmt.Errorf("openapi: read specs dir %q: %w", dir, err)
54 }
55
56 // Process in deterministic order so registry iteration and log output
57 // don't depend on filesystem enumeration order.
58 files := make([]string, 0, len(entries))
59 for _, e := range entries {
60 if e.IsDir() {
61 continue
62 }
63 name := e.Name()
64 if !strings.HasSuffix(name, ".yaml") && !strings.HasSuffix(name, ".yml") {
65 continue
66 }
67 files = append(files, name)
68 }
69 sort.Strings(files)
70
71 loader := openapi3.NewLoader()
72 loader.IsExternalRefsAllowed = false
73
74 for _, name := range files {
75 path := filepath.Join(dir, name)
76 key := strings.TrimSuffix(strings.TrimSuffix(name, ".yaml"), ".yml")
77
78 doc, err := loader.LoadFromFile(path)
79 if err != nil {
80 res.Warnings = append(res.Warnings, fmt.Sprintf("openapi: skip %s: parse error: %v", name, err))
81 continue
82 }
83
84 // kin-openapi's Validate is strict; we want to be lenient because
85 // real-world specs frequently have minor non-conformances we can
86 // still extract operations from. Validation failures become
87 // warnings, not skips.
88 if vErr := doc.Validate(loader.Context); vErr != nil {
89 res.Warnings = append(res.Warnings, fmt.Sprintf("openapi: %s: validation: %v (continuing)", name, vErr))
90 }
91
92 cfg := expandSpecConfig(configs[key])
93 cfg = canonicaliseOpKeys(cfg, doc)
94
95 baseURL := cfg.BaseURL
96 if baseURL == "" && len(doc.Servers) > 0 {

Calls 6

expandSpecConfigFunction · 0.85
canonicaliseOpKeysFunction · 0.85
classifyAllFunction · 0.85
ValidateMethod · 0.80
NameMethod · 0.65
addMethod · 0.45