Skip to content

Commit a93f09e

Browse files
committed
wip
Signed-off-by: Pierre Fenoll <[email protected]>
1 parent c84451c commit a93f09e

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

openapi3/callback.go

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openapi3
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67

78
"github.com/go-openapi/jsonpointer"
@@ -12,6 +13,16 @@ type Callbacks struct {
1213
om *orderedmap.OrderedMap[string, *CallbackRef]
1314
}
1415

16+
// MarshalJSON returns the JSON encoding of Callbacks.
17+
func (c *Callbacks) MarshalJSON() ([]byte, error) {
18+
return c.om.MarshalJSON()
19+
}
20+
21+
// UnmarshalJSON sets Callbacks to a copy of data.
22+
func (c *Callbacks) UnmarshalJSON(data []byte) error {
23+
return json.Unmarshal(data, &c.om)
24+
}
25+
1526
func (c *Callbacks) Value(key string) *CallbackRef {
1627
// if c == nil || c.om == nil {
1728
// return nil
@@ -42,7 +53,8 @@ func (c *Callbacks) Iter() *callbacksKV {
4253
type callbacksKV orderedmap.Pair[string, *CallbackRef] //FIXME: pub?
4354

4455
func (pair *callbacksKV) Next() *callbacksKV {
45-
return pair.Next()
56+
ompair := (*orderedmap.Pair[string, *CallbackRef])(pair)
57+
return (*callbacksKV)(ompair.Next())
4658
}
4759

4860
var _ jsonpointer.JSONPointable = (*Callbacks)(nil)
@@ -63,14 +75,58 @@ func (c *Callbacks) JSONLookup(token string) (interface{}, error) {
6375
// Callback is specified by OpenAPI/Swagger standard version 3.
6476
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#callbackObject
6577
type Callback struct {
66-
orderedmap.OrderedMap[string, *PathItem]
78+
om *orderedmap.OrderedMap[string, *PathItem]
79+
}
80+
81+
// MarshalJSON returns the JSON encoding of Callback.
82+
func (callback *Callback) MarshalJSON() ([]byte, error) {
83+
return callback.om.MarshalJSON()
84+
}
85+
86+
// UnmarshalJSON sets Callback to a copy of data.
87+
func (callback *Callback) UnmarshalJSON(data []byte) error {
88+
return json.Unmarshal(data, &callback.om)
89+
}
90+
91+
func (callback *Callback) Value(key string) *PathItem {
92+
// if callback == nil || callback.om == nil {
93+
// return nil
94+
// }
95+
return callback.om.Value(key)
96+
}
97+
98+
func (callback *Callback) Set(key string, value *PathItem) {
99+
// if callback != nil || callback.om != nil {
100+
_, _ = callback.om.Set(key, value)
101+
// }
102+
}
103+
104+
func (callback *Callback) Len() int {
105+
if callback == nil || callback.om == nil {
106+
return 0
107+
}
108+
return callback.om.Len()
109+
}
110+
111+
func (callback *Callback) Iter() *callbackKV {
112+
if callback == nil || callback.om == nil {
113+
return nil
114+
}
115+
return (*callbackKV)(callback.om.Oldest())
116+
}
117+
118+
type callbackKV orderedmap.Pair[string, *PathItem] //FIXME: pub?
119+
120+
func (pair *callbackKV) Next() *callbackKV {
121+
ompair := (*orderedmap.Pair[string, *PathItem])(pair)
122+
return (*callbackKV)(ompair.Next())
67123
}
68124

69125
// Validate returns an error if Callback does not comply with the OpenAPI spec.
70126
func (callback *Callback) Validate(ctx context.Context, opts ...ValidationOption) error {
71127
ctx = WithValidationOptions(ctx, opts...)
72128

73-
for pair := callback.Oldest(); pair != nil; pair = pair.Next() {
129+
for pair := callback.Iter(); pair != nil; pair = pair.Next() {
74130
if err := pair.Value.Validate(ctx); err != nil {
75131
return err
76132
}

openapi3/internalize_refs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func (doc *T) derefPaths(paths *orderedmap.OrderedMap[string, *PathItem], refNam
307307
cb := pair.Value
308308
isExternal := doc.addCallbackToSpec(cb, refNameResolver, parentIsExternal)
309309
if cb.Value != nil {
310-
cbValue := any(cb.Value).(*orderedmap.OrderedMap[string, *PathItem])
310+
cbValue := (*orderedmap.OrderedMap[string, *PathItem])(cb.Value.om)
311311
doc.derefPaths(cbValue, refNameResolver, parentIsExternal || isExternal)
312312
}
313313
}
@@ -379,7 +379,7 @@ func (doc *T) InternalizeRefs(ctx context.Context, refNameResolver func(ref stri
379379
isExternal := doc.addCallbackToSpec(cb, refNameResolver, false)
380380
if cb != nil && cb.Value != nil {
381381
cb.Ref = "" // always dereference the top level
382-
cbValue := any(cb.Value).(*orderedmap.OrderedMap[string, *PathItem])
382+
cbValue := (*orderedmap.OrderedMap[string, *PathItem])(cb.Value.om)
383383
doc.derefPaths(cbValue, refNameResolver, isExternal)
384384
}
385385
}

openapi3/loader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ func (loader *Loader) resolveCallbackRef(doc *T, component *CallbackRef, documen
934934
return nil
935935
}
936936

937-
for pair := value.Oldest(); pair != nil; pair = pair.Next() {
937+
for pair := value.Iter(); pair != nil; pair = pair.Next() {
938938
entrypoint, pathItem := pair.Key, pair.Value
939939
err = func() (err error) {
940940
key := "-"
@@ -977,7 +977,7 @@ func (loader *Loader) resolveCallbackRef(doc *T, component *CallbackRef, documen
977977
return failedToResolveRefFragmentPart(ref, id)
978978
}
979979

980-
for pair := resolved.Value.Oldest(); pair != nil; pair = pair.Next() {
980+
for pair := resolved.Value.Iter(); pair != nil; pair = pair.Next() {
981981
*pathItem = *pair.Value
982982
break
983983
}

openapi3/paths.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type pathsKV orderedmap.Pair[string, *PathItem] //FIXME: pub?
6060

6161
func (pair *pathsKV) Next() *pathsKV {
6262
ompair := (*orderedmap.Pair[string, *PathItem])(pair)
63-
return (*pathsKV)((ompair).Next())
63+
return (*pathsKV)(ompair.Next())
6464
}
6565

6666
// NewPathsWithCapacity builds a paths object of the given capacity.

0 commit comments

Comments
 (0)