-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdereference.go
More file actions
53 lines (42 loc) · 1.08 KB
/
dereference.go
File metadata and controls
53 lines (42 loc) · 1.08 KB
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
package mason
import (
"encoding/json"
"fmt"
"strings"
"github.com/swaggest/jsonschema-go"
"github.com/tailbits/mason/model"
)
func (a *API) DereferenceSchema(schema []byte) ([]byte, error) {
var sch jsonschema.Schema
if err := json.Unmarshal(schema, &sch); err != nil {
return nil, fmt.Errorf("json.Unmarshal: schema[%s] %w", string(schema), err)
}
var err error
walkRefs(&sch, func(ref *string) {
id := strings.TrimPrefix(*ref, "#/definitions/")
if _, ok := sch.Definitions[id]; !ok {
// that means we have an external reference
// we need to dereference it
e, ok := a.GetModel(id)
if !ok {
err = fmt.Errorf("entity %s not found", id)
return
}
ent, ok := e.(model.WithSchema)
if !ok {
err = fmt.Errorf("entity %s does not implement platform.WithSchema", id)
return
}
entSchBytes := ent.Schema()
var entSch jsonschema.Schema
if err = json.Unmarshal(entSchBytes, &entSch); err != nil {
return
}
sch.WithDefinitionsItem(id, entSch.ToSchemaOrBool())
}
})
if err != nil {
return nil, err
}
return json.Marshal(sch)
}