Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be strict about adherence to cardinality via collection: true #185

Open
ronaldtse opened this issue Nov 21, 2024 · 1 comment
Open

Be strict about adherence to cardinality via collection: true #185

ronaldtse opened this issue Nov 21, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@ronaldtse
Copy link
Contributor

From:

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.

@0arm0
Copy link
Contributor

0arm0 commented Jan 7, 2025

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants