diff --git a/lib/puppet/pops/types/p_object_type.rb b/lib/puppet/pops/types/p_object_type.rb index 2290245344..6fcb567593 100644 --- a/lib/puppet/pops/types/p_object_type.rb +++ b/lib/puppet/pops/types/p_object_type.rb @@ -316,6 +316,8 @@ def initialize(name, container, init_hash) v = init_hash[KEY_VALUE] @value = v == :default ? v : TypeAsserter.assert_instance_of(nil, type, v) { "#{label} #{KEY_VALUE}" } + elsif @kind == ATTRIBUTE_KIND_GIVEN_OR_DERIVED + @value = nil else raise Puppet::ParseError, _("%{label} of kind 'constant' requires a value") % { label: label } if @kind == ATTRIBUTE_KIND_CONSTANT @@ -341,7 +343,7 @@ def _pcore_init_hash hash[KEY_KIND] = @kind hash.delete(KEY_FINAL) if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied end - hash[KEY_VALUE] = @value unless @value == :undef + hash[KEY_VALUE] = @value unless @value == :undef || @kind == ATTRIBUTE_KIND_GIVEN_OR_DERIVED hash end diff --git a/spec/unit/pops/types/p_object_type_spec.rb b/spec/unit/pops/types/p_object_type_spec.rb index 8d8f8254a9..d119d0de08 100644 --- a/spec/unit/pops/types/p_object_type_spec.rb +++ b/spec/unit/pops/types/p_object_type_spec.rb @@ -697,18 +697,16 @@ def parse_object(name, body_string) end context 'when producing an init_hash_type' do - it 'produces a struct of all attributes that are not derived or constant' do + it 'produces a struct of all attributes excepting derived or constant' do t = parse_object('MyObject', <<-OBJECT) attributes => { a => { type => Integer }, - b => { type => Integer, kind => given_or_derived }, - c => { type => Integer, kind => derived }, - d => { type => Integer, kind => constant, value => 4 } + b => { type => Integer, kind => derived }, + c => { type => Integer, kind => constant, value => 4 } } OBJECT expect(t.init_hash_type).to eql(factory.struct({ 'a' => factory.integer, - 'b' => factory.integer })) end @@ -716,12 +714,14 @@ def parse_object(name, body_string) t = parse_object('MyObject', <<-OBJECT) attributes => { a => { type => Integer }, - b => { type => Integer, value => 4 } + b => { type => Integer, value => 4 }, + c => { type => Integer, kind => given_or_derived }, } OBJECT expect(t.init_hash_type).to eql(factory.struct({ 'a' => factory.integer, - factory.optional('b') => factory.integer + factory.optional('b') => factory.integer, + factory.optional('c') => factory.integer, })) end