MCPcopy Create free account
hub / github.com/modelcontextprotocol/registry / buildResolvedReference

Function buildResolvedReference

internal/validators/schema.go:353–427  ·  view source on GitHub ↗

buildResolvedReference extracts the resolved reference path by resolving $ref segments

(keywordLocation, absoluteKeywordLocation string, schema map[string]any)

Source from the content-addressed store, hash-verified

351
352// buildResolvedReference extracts the resolved reference path by resolving $ref segments
353func buildResolvedReference(keywordLocation, absoluteKeywordLocation string, schema map[string]any) string {
354 if keywordLocation == "" || absoluteKeywordLocation == "" {
355 return ""
356 }
357
358 // Clean up the absolute location by removing file:// prefix
359 absolute := absoluteKeywordLocation
360 if strings.HasPrefix(absolute, "file://") {
361 absolute = strings.TrimPrefix(absolute, "file://")
362 if idx := strings.Index(absolute, "#"); idx != -1 {
363 absolute = absolute[idx:] // Keep only the #/path part
364 }
365 }
366
367 // Parse the keyword location to understand the $ref chain
368 keyword := strings.TrimPrefix(keywordLocation, "/")
369 keywordParts := strings.Split(keyword, "/")
370
371 // Build the path showing $ref resolution
372 pathSegments := make([]string, 0)
373
374 // Track the resolved path so far (starts empty, gets built up as we resolve $refs)
375 resolvedPath := ""
376
377 // Process each part of the keyword path
378 for i, part := range keywordParts {
379 if part == "" {
380 continue // Skip empty parts
381 }
382
383 if part == "$ref" {
384 // This is a $ref - we need to look up what it resolves to
385 // For the first $ref, use the path from the root
386 // For subsequent $refs, use the resolved path from the previous $ref plus the current segment
387 var refPath string
388 if resolvedPath == "" {
389 // First $ref - use the path from the root
390 refPath = strings.Join(keywordParts[:i+1], "/")
391 refPath = "/" + refPath
392 } else {
393 // Subsequent $ref - use the resolved path plus the current segment
394 refPath = resolvedPath + "/" + part
395 }
396
397 // Look up the $ref value in the schema
398 refValue := resolveRefInSchema(schema, refPath)
399
400 if refValue != "" {
401 pathSegments = append(pathSegments, fmt.Sprintf("[%s]", refValue))
402 // Update the resolved path for the next $ref
403 resolvedPath = refValue
404 } else {
405 pathSegments = append(pathSegments, "[$ref]")
406 }
407 } else {
408 // Regular path segment
409 pathSegments = append(pathSegments, part)
410 // Add this segment to the resolved path for the next $ref

Callers 1

addDetailedErrorsFunction · 0.85

Calls 2

resolveRefInSchemaFunction · 0.85
IndexMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…