{"record":{"id":"99fbc9ab0d7bb5e8","repo":"vectordotdev/vector","slug":"schema-reference-should-exist","errorCode":null,"errorMessage":"schema reference should exist","messagePattern":"schema reference should exist","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/vector-config/src/schema/visitors/scoped_visit.rs","lineNumber":85,"sourceCode":"pub trait ScopedVisitor: Visitor {\n    fn push_schema_scope<S: Into<SchemaReference>>(&mut self, scope: S);\n\n    fn pop_schema_scope(&mut self);\n\n    fn get_current_schema_scope(&self) -> &SchemaReference;\n}\n\npub fn visit_schema_object_scoped<SV: ScopedVisitor + ?Sized>(\n    sv: &mut SV,\n    definitions: &mut Map<String, Schema>,\n    schema: &mut SchemaObject,\n) {\n    if let Some(reference) = schema.reference.as_ref() {\n        let schema_def_key = get_cleaned_schema_reference(reference);\n        let mut referenced_schema = definitions\n            .get(schema_def_key)\n            .cloned()\n            .expect(\"schema reference should exist\");\n\n        if let Schema::Object(referenced_schema) = &mut referenced_schema {\n            sv.push_schema_scope(schema_def_key);\n\n            sv.visit_schema_object(definitions, referenced_schema);\n\n            sv.pop_schema_scope();\n        }\n\n        definitions.insert(schema_def_key.to_string(), referenced_schema);\n    }\n\n    if let Some(sub) = &mut schema.subschemas {\n        visit_vec_scoped(sv, definitions, &mut sub.all_of);\n        visit_vec_scoped(sv, definitions, &mut sub.any_of);\n        visit_vec_scoped(sv, definitions, &mut sub.one_of);\n        visit_box_scoped(sv, definitions, &mut sub.not);\n        visit_box_scoped(sv, definitions, &mut sub.if_schema);","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/vectordotdev/vector/blob/bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013/lib/vector-config/src/schema/visitors/scoped_visit.rs#L67-L103","documentation":"`visit_schema_object_scoped` resolves a schema object's `$ref` against the root definitions map, clones it, and asserts it exists before recursively visiting it under a new scope. The panic fires when the referenced definition name is not present in `definitions`, meaning a reference points to a definition that was removed or never registered.","triggerScenarios":"Visiting a schema whose `$ref` target is missing from `root.definitions` — definitions pruned by an earlier visitor, a hand-assembled `RootSchema`, or a schemars version emitting `$ref`s to external/absent definitions.","commonSituations":"Composing visitors where one deletes definitions another still needs; constructing schemas programmatically with stale `$ref` strings; upgrading schemars and relying on renamed definition keys.","solutions":["Validate that every `$ref` in the root schema resolves to an entry in `definitions` before running the visitor pipeline","Keep the visitor that removes definitions ordered after all ref-resolving visitors","Check `Cargo.lock` for a schemars version mismatch after dependency upgrades","Reproduce with a minimal schema and log the definitions map at visitor entry"],"exampleFix":"// before\nif !schema.definitions.contains_key(\"MyDef\") { schema.definitions.remove(\"Other\"); }\nvisitor.visit_root_schema(&mut schema);\n// after\nvisitor.visit_root_schema(&mut schema); // let ref-resolving visitors run before any removal","handlingStrategy":"validation","validationCode":"fn all_refs_resolve(root: &RootSchema) -> Result<(), String> {\n    fn walk(s: &Schema, defs: &Map<String, Schema>) -> Result<(), String> {\n        if let Schema::Object(o) = s {\n            if let Some(r) = &o.reference {\n                let key = r.trim_start_matches(\"#/definitions/\");\n                if !defs.contains_key(key) { return Err(format!(\"unresolved $ref: {key}\")); }\n            }\n            if let Some(sub) = &o.subschemas {\n                for child in sub.iter_children() { walk(child, defs)?; }\n            }\n            if let Some(obj) = &o.object {\n                for p in obj.properties.values() { walk(p, defs)?; }\n            }\n        }\n        Ok(())\n    }\n    if let Some(sub) = &root.schema.subschemas {\n        for child in sub.iter_children() { walk(child, &root.definitions)?; }\n    }\n    Ok(())\n}","typeGuard":"fn resolve_ref<'a>(defs: &'a Map<String, Schema>, reference: &str) -> Option<&'a Schema> {\n    defs.get(get_cleaned_schema_reference(reference))\n}","tryCatchPattern":"let referenced = definitions.get(schema_def_key).cloned().ok_or_else(||\n    anyhow::anyhow!(\"$ref {schema_def_key} does not resolve in definitions\"))?;","preventionTips":["Validate every $ref resolves before running ref-resolving visitors","Order definition-removal visitors last in the pipeline","Check schemars version consistency after dependency bumps"],"tags":["rust","schema-generation","json-schema"],"backgroundTag":"resource-not-found","analyzedSha":"bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013","analyzedAt":"2026-09-16T02:53:35.741Z","contentChangedAt":"2026-09-16T02:53:35.741Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}