Skip to content

Commit 34f4e38

Browse files
fix: fixed potential nil reference error and improve resolution interface (#41)
1 parent acc11cd commit 34f4e38

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

json/json.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func YAMLToJSON(node *yaml.Node, indentation int, buffer io.Writer) error {
2727
}
2828

2929
func handleYAMLNode(node *yaml.Node) (any, error) {
30+
if node == nil {
31+
return nil, nil
32+
}
33+
3034
switch node.Kind {
3135
case yaml.DocumentNode:
3236
return handleYAMLNode(node.Content[0])

jsonschema/oas3/jsonschema.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type JSONSchema[T Referenceable | Concrete] struct {
4040
topLevelParent *JSONSchema[Referenceable] // Top-level parent (root of the reference chain)
4141
}
4242

43+
var _ references.Resolvable[JSONSchema[Concrete]] = (*JSONSchema[Referenceable])(nil)
44+
4345
func NewJSONSchemaFromSchema[T Referenceable | Concrete](value *Schema) *JSONSchema[T] {
4446
return &JSONSchema[T]{
4547
EitherValue: values.EitherValue[Schema, core.Schema, bool, bool]{

jsonschema/oas3/resolution.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,18 @@ func (s *JSONSchema[Referenceable]) Resolve(ctx context.Context, opts ResolveOpt
7474
}, []string{})
7575
}
7676

77+
// GetResolvedObject will return either this schema or the referenced schema if previously resolved.
78+
// This methods is identical to GetResolvedSchema but was added to support the Resolvable interface
79+
func (s *JSONSchema[Referenceable]) GetResolvedObject() *JSONSchema[Concrete] {
80+
if s == nil {
81+
return nil
82+
}
83+
84+
return s.GetResolvedSchema()
85+
}
86+
7787
// GetResolvedSchema will return either this schema or the referenced schema if previously resolved.
88+
// This methods is identical to GetResolvedObject but was kept for backwards compatibility
7889
func (s *JSONSchema[Referenceable]) GetResolvedSchema() *JSONSchema[Concrete] {
7990
if s == nil || !s.IsResolved() {
8091
return nil

openapi/reference.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ type Reference[T any, V interfaces.Validator[T], C marshaller.CoreModeler] struc
205205
topLevelParent *Reference[T, V, C] // Top-level parent (root of the reference chain)
206206
}
207207

208+
var _ references.Resolvable[Info] = (*Reference[Info, *Info, *core.Info])(nil)
209+
208210
var _ interfaces.Model[core.Reference[*core.Info]] = (*Reference[Info, *Info, *core.Info])(nil)
209211

210212
// ResolveOptions represent the options available when resolving a reference.
@@ -265,7 +267,18 @@ func (r *Reference[T, V, C]) GetReference() references.Reference {
265267
return *r.Reference
266268
}
267269

270+
// GetResolvedObject will return the referenced object. If this is a reference and its unresolved, this will return nil.
271+
// This methods is identical to GetObject but was added to support the Resolvable interface
272+
func (r *Reference[T, V, C]) GetResolvedObject() *T {
273+
if r == nil {
274+
return nil
275+
}
276+
277+
return r.GetObject()
278+
}
279+
268280
// GetObject returns the referenced object. If this is a reference and its unresolved, this will return nil.
281+
// This methods is identical to GetResolvedObject but was kept for backwards compatibility
269282
func (r *Reference[T, V, C]) GetObject() *T {
270283
if r == nil {
271284
return nil

references/resolution.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ type ResolutionTarget interface {
2424
StoreReferenceDocumentInCache(key string, doc []byte)
2525
}
2626

27+
type Resolvable[T any] interface {
28+
Resolve(ctx context.Context, opts ResolveOptions) ([]error, error)
29+
GetResolvedObject() *T
30+
}
31+
2732
// AbsoluteReferenceResult contains the result of resolving an absolute reference
2833
type AbsoluteReferenceResult struct {
2934
// AbsoluteReference is the resolved absolute reference string

0 commit comments

Comments
 (0)