You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, if an attribute does not define collection: true, lutaml-model accepts all 3 cases:
0 occurrences
1 occurrence
N occurrences
However, collection: true means 0..*, and collection: false (default) means 0..1. Right now, the default of 0..1 does not raise any error if there are more than 1 occurrence.
When the XML/JSON does not fit this model definition, it should raise an error.
The text was updated successfully, but these errors were encountered:
@ronaldtse I was looking into this issue and I tried to raise an error on invalid cardinality such that the input has multiple elements but we don’t have collection: true defined in our attribute.
So, in this case, map_content raises an error since multiple content lines are mapped to a single content attribute in an Array, which raises a cardinality error. For example:
xml = <<~XML <RootOrderedContent id="123">
<bold>bell</bold>
<italic>384,400 km</italic>
<underline>craters</underline>
<bold>cool</bold>
The Earth's Moon rings like a when struck by
meteroids. Distanced from the Earth by ,
its surface is covered in . Ain't that ?
</RootOrderedContent>
XML
When mapped to:
class RootOrderedContent < Lutaml::Model::Serializable
attribute :id, :string
attribute :bold, :string, collection: true
attribute :italic, :string, collection: true
attribute :underline, :string
attribute :content, :string
xml do
root "RootOrderedContent", ordered: true
map_attribute :id, to: :id
map_element :bold, to: :bold
map_element :italic, to: :italic
map_element :underline, to: :underline
map_content to: :content
end
end
serialized = OrderedContentSpec::RootOrderedContent.from_xml(xml)
serialized.content #=> ["\n The Earth's Moon rings like a ", " when struck by\n meteroids. Distanced from the Earth by ", ",\n its surface is covered in ", ".\n Ain't that ", "?\n"]
So this content, when mapped to :content attribute, would raise cardinality error if we implement this strictness.
A solution that I can think of is to bypass the :content attribute (from my example) from this error check so that no error is raised.
Let me know what do you suggest here please?
From:
Currently, if an attribute does not define
collection: true
, lutaml-model accepts all 3 cases:However,
collection: true
means0..*
, andcollection: false
(default) means0..1
. Right now, the default of0..1
does not raise any error if there are more than 1 occurrence.When the XML/JSON does not fit this model definition, it should raise an error.
The text was updated successfully, but these errors were encountered: