MCPcopy Index your code
hub / github.com/getkin/kin-openapi / resolveSchemaRef

Method resolveSchemaRef

openapi3/loader.go:931–1110  ·  view source on GitHub ↗
(doc *T, component *SchemaRef, documentPath *url.URL, visited []string)

Source from the content-addressed store, hash-verified

929}
930
931func (loader *Loader) resolveSchemaRef(doc *T, component *SchemaRef, documentPath *url.URL, visited []string) (err error) {
932 if component.isEmpty() {
933 return errMUSTSchema
934 }
935
936 if ref := component.Ref; ref != "" {
937 if component.Value != nil {
938 return nil
939 }
940 if !loader.shouldVisitRef(ref, func(value any) {
941 component.Value = value.(*Schema)
942 refPath, _ := loader.resolveRefPath(ref, documentPath)
943 component.setRefPath(refPath)
944 }) {
945 return nil
946 }
947 loader.visitRef(ref)
948 if isSingleRefElement(ref) {
949 var schema Schema
950 if documentPath, err = loader.loadSingleElementFromURI(ref, documentPath, &schema); err != nil {
951 return err
952 }
953 component.Value = &schema
954 component.setRefPath(documentPath)
955 } else {
956 var resolved SchemaRef
957 doc, componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
958 if err != nil {
959 return err
960 }
961 if err := loader.resolveSchemaRef(doc, &resolved, componentPath, visited); err != nil {
962 if err == errMUSTSchema {
963 return nil
964 }
965 return err
966 }
967 component.Value = resolved.Value
968 component.setRefPath(resolved.RefPath())
969 }
970 defer loader.unvisitRef(ref, component.Value)
971
972 // OAS 3.1 / JSON Schema 2020-12: apply sibling keywords from the original schema
973 // object on top of the resolved $ref value. In 3.1, siblings are not ignored —
974 // they augment the referenced schema (e.g. deprecated:true alongside $ref).
975 // Only apply for OAS 3.1+ — in 3.0 $ref replaces its entire object and siblings
976 // are (validly) ignored.
977 if doc.IsOpenAPI31OrLater() && component.sibling != nil && component.Value != nil {
978 // Work on a copy so we don't mutate a schema shared by other references.
979 schemaCopy := *component.Value
980 applySiblingSchemaFields(&schemaCopy, component.sibling, component.extra)
981 component.Value = &schemaCopy
982 }
983 }
984 value := component.Value
985 if value == nil {
986 return nil
987 }
988

Callers 5

ResolveRefsInMethod · 0.95
resolveHeaderRefMethod · 0.95
resolveParameterRefMethod · 0.95
resolveRequestBodyRefMethod · 0.95
resolveResponseRefMethod · 0.95

Calls 13

shouldVisitRefMethod · 0.95
resolveRefPathMethod · 0.95
visitRefMethod · 0.95
resolveComponentMethod · 0.95
RefPathMethod · 0.95
unvisitRefMethod · 0.95
isSingleRefElementFunction · 0.85
applySiblingSchemaFieldsFunction · 0.85
componentNamesFunction · 0.85
IsOpenAPI31OrLaterMethod · 0.80
isEmptyMethod · 0.45

Tested by

no test coverage detected