MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / ensureExternalRefsInSchema

Function ensureExternalRefsInSchema

pkg/codegen/externalref.go:80–107  ·  view source on GitHub ↗

ensureExternalRefsInSchema ensures that when an externalRef (`$ref` that points to a file that isn't the current spec) is encountered, we make sure we update our underlying `RefType` to make sure that we point to that type. This only happens if we have a non-empty `ref` passed in, and that `ref` is

(schema *Schema, ref string)

Source from the content-addressed store, hash-verified

78//
79// NOTE that the pointer here allows us to pass in a reference and edit in-place
80func ensureExternalRefsInSchema(schema *Schema, ref string) {
81 if ref == "" {
82 return
83 }
84
85 parts := strings.SplitN(ref, "#", 2)
86 pack, ok := globalState.importMapping[parts[0]]
87 if !ok {
88 return
89 }
90
91 // if this is already defined as the start of a struct, we shouldn't inject **??**
92 if !strings.HasPrefix(schema.GoType, "struct {") {
93 schema.RefType = fmt.Sprintf("%s.%s", pack.Name, schema.GoType)
94 }
95
96 // Qualify union branch types. Each UnionElement was resolved as a
97 // local reference during generateUnion (e.g. "Bionicle"); when the
98 // enclosing schema came from an externally-ref'd file the branches
99 // actually live in the imported package, so the As/From/Merge
100 // methods generated by union.tmpl must use "<pkg>.Bionicle" instead.
101 for i, elem := range schema.UnionElements {
102 s := string(elem)
103 if !strings.Contains(s, ".") {
104 schema.UnionElements[i] = UnionElement(fmt.Sprintf("%s.%s", pack.Name, s))
105 }
106 }
107}

Calls 1

UnionElementTypeAlias · 0.85

Tested by

no test coverage detected