{"record":{"id":"212774bc08207f34","repo":"puppetlabs/puppet","slug":"you-can-only-save-objects-that-respond-to-name","errorCode":null,"errorMessage":"You can only save objects that respond to :name","messagePattern":"You can only save objects that respond to :name","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/indirector/yaml.rb","lineNumber":22,"sourceCode":"require_relative '../../puppet/util/yaml'\n\n# The base class for YAML indirection termini.\nclass Puppet::Indirector::Yaml < Puppet::Indirector::Terminus\n  # Read a given name's file in and convert it from YAML.\n  def find(request)\n    file = path(request.key)\n    return nil unless Puppet::FileSystem.exist?(file)\n\n    begin\n      load_file(file)\n    rescue Puppet::Util::Yaml::YamlLoadError => detail\n      raise Puppet::Error, _(\"Could not parse YAML data for %{indirection} %{request}: %{detail}\") % { indirection: indirection.name, request: request.key, detail: detail }, detail.backtrace\n    end\n  end\n\n  # Convert our object to YAML and store it to the disk.\n  def save(request)\n    raise ArgumentError, _(\"You can only save objects that respond to :name\") unless request.instance.respond_to?(:name)\n\n    file = path(request.key)\n\n    basedir = File.dirname(file)\n\n    # This is quite likely a bad idea, since we're not managing ownership or modes.\n    Dir.mkdir(basedir) unless Puppet::FileSystem.exist?(basedir)\n\n    begin\n      Puppet::Util::Yaml.dump(request.instance, file)\n    rescue TypeError => detail\n      Puppet.err _(\"Could not save %{indirection} %{request}: %{detail}\") % { indirection: name, request: request.key, detail: detail }\n    end\n  end\n\n  # Return the path to a given node's file.\n  def path(name, ext = '.yaml')\n    if name =~ Puppet::Indirector::BadNameRegexp then","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/indirector/yaml.rb#L4-L40","documentation":"The YAML terminus's save() derives the storage path from the object's name, so it requires request.instance to respond to :name; anything else raises ArgumentError immediately, before any file I/O. This is a duck-type contract check: only name-addressed model objects (nodes, facts, reports) can be stored in the yaml indirection store.","triggerScenarios":"Calling Puppet::Node.indirection.save (or another yaml-backed indirection) with a Hash, Array, String, Struct, or any object lacking a name method; passing a wrapper/decorator that forwards most calls but not name.","commonSituations":"Custom tooling that tries to persist arbitrary configuration hashes through the yaml terminus instead of using its own storage; refactors wrapping model objects; test code saving raw data structures for later inspection.","solutions":["Save proper model instances: build the object with Puppet::Node.new(name, ...) / Puppet::Node::Facts.new(name, values) and save that","For arbitrary data, use Puppet::Util::Yaml.dump(data, path) directly instead of the indirection","If wrapping a model, delegate name (def name; @target.name; end) so the contract holds","Check respond_to?(:name) at the call site for clearer failure context"],"exampleFix":"# before\nPuppet::Node.indirection.save({ 'hostname' => 'web01' }, 'web01')\n# => ArgumentError: You can only save objects that respond to :name\n\n# after\nnode = Puppet::Node.new('web01', parameters: { 'hostname' => 'web01' })\nPuppet::Node.indirection.save(node, 'web01')","handlingStrategy":"type-guard","validationCode":"unless request.instance.respond_to?(:name)\n  raise ArgumentError, 'only name-bearing model objects can be saved; wrap raw data or use Puppet::Util::Yaml.dump'\nend","typeGuard":"def name_bearing?(obj)\n  obj.respond_to?(:name)\nend","tryCatchPattern":null,"preventionTips":["Route arbitrary data dumps to Puppet::Util::Yaml.dump, not the indirection","Expose save paths only through model constructors so the contract is enforced by types","Wrap third-party objects with a name delegator before storing"],"tags":["puppet","yaml","indirector","duck-typing","argument-error"],"backgroundTag":"interface-method-missing","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}