Skip to content

Commit b5bcd71

Browse files
openapi3: export ComponentRef for usage in RefNameResolver (#998)
* Export ComponentRef for usage in RefNameResolver * run ./docs.sh Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com> --------- Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com> Co-authored-by: chris.smith <chris.smith@zocdoc.com>
1 parent f7ebd9c commit b5bcd71

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

.github/docs/openapi3.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ FUNCTIONS
9696
func BoolPtr(value bool) *bool
9797
BoolPtr is a helper for defining OpenAPI schemas.
9898

99-
func DefaultRefNameResolver(doc *T, ref componentRef) string
99+
func DefaultRefNameResolver(doc *T, ref ComponentRef) string
100100
DefaultRefResolver is a default implementation of refNameResolver for the
101101
InternalizeRefs function.
102102

@@ -150,7 +150,7 @@ func Int64Ptr(value int64) *int64
150150
func ReadFromFile(loader *Loader, location *url.URL) ([]byte, error)
151151
ReadFromFile is a ReadFromURIFunc which reads local file URIs.
152152

153-
func ReferencesComponentInRootDocument(doc *T, ref componentRef) (string, bool)
153+
func ReferencesComponentInRootDocument(doc *T, ref ComponentRef) (string, bool)
154154
ReferencesComponentInRootDocument returns if the given component reference
155155
references the same document or element as another component reference in
156156
the root document's '#/components/<type>'. If it does, it returns the name
@@ -316,6 +316,12 @@ func (m Callbacks) JSONLookup(token string) (any, error)
316316
JSONLookup implements
317317
https://pkg.go.dev/github.com/go-openapi/jsonpointer#JSONPointable
318318

319+
type ComponentRef interface {
320+
RefString() string
321+
RefPath() *url.URL
322+
CollectionName() string
323+
}
324+
319325
type Components struct {
320326
Extensions map[string]any `json:"-" yaml:"-"`
321327

@@ -1227,7 +1233,7 @@ type Ref struct {
12271233
Ref is specified by OpenAPI/Swagger 3.0 standard. See
12281234
https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#reference-object
12291235

1230-
type RefNameResolver func(*T, componentRef) string
1236+
type RefNameResolver func(*T, ComponentRef) string
12311237
RefNameResolver maps a component to an name that is used as it's
12321238
internalized name.
12331239

@@ -1990,7 +1996,7 @@ func (doc *T) AddServer(server *Server)
19901996

19911997
func (doc *T) AddServers(servers ...*Server)
19921998

1993-
func (doc *T) InternalizeRefs(ctx context.Context, refNameResolver func(*T, componentRef) string)
1999+
func (doc *T) InternalizeRefs(ctx context.Context, refNameResolver func(*T, ComponentRef) string)
19942000
InternalizeRefs removes all references to external files from the spec and
19952001
moves them to the components section.
19962002

openapi3/helpers.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func copyURI(u *url.URL) *url.URL {
7171
return &c
7272
}
7373

74-
type componentRef interface {
74+
type ComponentRef interface {
7575
RefString() string
7676
RefPath() *url.URL
7777
CollectionName() string
@@ -88,7 +88,7 @@ type componentRef interface {
8888
// /schema/other.yaml $ref: ../records.yaml
8989
//
9090
// The records.yaml reference in the 2 latter refers to the same document.
91-
func refersToSameDocument(o1 componentRef, o2 componentRef) bool {
91+
func refersToSameDocument(o1 ComponentRef, o2 ComponentRef) bool {
9292
if o1 == nil || o2 == nil {
9393
return false
9494
}
@@ -107,7 +107,7 @@ func refersToSameDocument(o1 componentRef, o2 componentRef) bool {
107107
// referencesRootDocument returns if the $ref points to the root document of the OpenAPI spec.
108108
//
109109
// If the document has no location, perhaps loaded from data in memory, it always returns false.
110-
func referencesRootDocument(doc *T, ref componentRef) bool {
110+
func referencesRootDocument(doc *T, ref ComponentRef) bool {
111111
if doc.url == nil || ref == nil || ref.RefPath() == nil {
112112
return false
113113
}
@@ -171,7 +171,7 @@ func referenceURIMatch(u1 *url.URL, u2 *url.URL) bool {
171171
// This would also return...
172172
//
173173
// #/components/schemas/Record
174-
func ReferencesComponentInRootDocument(doc *T, ref componentRef) (string, bool) {
174+
func ReferencesComponentInRootDocument(doc *T, ref ComponentRef) (string, bool) {
175175
if ref == nil || ref.RefString() == "" {
176176
return "", false
177177
}
@@ -197,19 +197,19 @@ func ReferencesComponentInRootDocument(doc *T, ref componentRef) (string, bool)
197197
panic(err) // unreachable
198198
}
199199

200-
var components map[string]componentRef
200+
var components map[string]ComponentRef
201201

202-
componentRefType := reflect.TypeOf(new(componentRef)).Elem()
202+
componentRefType := reflect.TypeOf(new(ComponentRef)).Elem()
203203
if t := reflect.TypeOf(collection); t.Kind() == reflect.Map &&
204204
t.Key().Kind() == reflect.String &&
205205
t.Elem().AssignableTo(componentRefType) {
206206
v := reflect.ValueOf(collection)
207207

208-
components = make(map[string]componentRef, v.Len())
208+
components = make(map[string]ComponentRef, v.Len())
209209
for _, key := range v.MapKeys() {
210210
strct := v.MapIndex(key)
211211
// Type assertion safe, already checked via reflection above.
212-
components[key.Interface().(string)] = strct.Interface().(componentRef)
212+
components[key.Interface().(string)] = strct.Interface().(ComponentRef)
213213
}
214214
} else {
215215
return "", false

openapi3/internalize_refs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
//
1111
// The function should avoid name collisions (i.e. be a injective mapping).
1212
// It must only contain characters valid for fixed field names: [IdentifierRegExp].
13-
type RefNameResolver func(*T, componentRef) string
13+
type RefNameResolver func(*T, ComponentRef) string
1414

1515
// DefaultRefResolver is a default implementation of refNameResolver for the
1616
// InternalizeRefs function.
@@ -27,7 +27,7 @@ type RefNameResolver func(*T, componentRef) string
2727
//
2828
// This is an injective mapping over a "reasonable" amount of the possible openapi
2929
// spec domain space but is not perfect. There might be edge cases.
30-
func DefaultRefNameResolver(doc *T, ref componentRef) string {
30+
func DefaultRefNameResolver(doc *T, ref ComponentRef) string {
3131
if ref.RefString() == "" || ref.RefPath() == nil {
3232
panic("unable to resolve reference to name")
3333
}
@@ -490,7 +490,7 @@ func (doc *T) derefPaths(paths map[string]*PathItem, refNameResolver RefNameReso
490490
// Example:
491491
//
492492
// doc.InternalizeRefs(context.Background(), nil)
493-
func (doc *T) InternalizeRefs(ctx context.Context, refNameResolver func(*T, componentRef) string) {
493+
func (doc *T) InternalizeRefs(ctx context.Context, refNameResolver func(*T, ComponentRef) string) {
494494
doc.resetVisited()
495495

496496
if refNameResolver == nil {

0 commit comments

Comments
 (0)