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

Lutaml::Model fails when a parent element is namespaced but its children are not #164

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

Comments

@ronaldtse
Copy link
Contributor

From:

The default Termium XML output looks like this:

<ns2:termium_extract xmlns:ns2="http://termium.tpsgc-pwgsc.gc.ca/schemas/2012/06/Termium"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="EN"
  xsi:schemaLocation="http://termium.tpsgc-pwgsc.gc.ca/schemas/2012/06/Termium http://termium.tpsgc-pwgsc.gc.ca/schemas/2012/06/Termium.xsd">
  <extractLanguage language="EN" order="0" />
  <extractLanguage language="FR" order="1" />
...

Here, the termium_extract element has a namespace marked as ns2, but its children have no namespaces.

Technically, it should be parsed like this:

module Termium
  # For <extract>
  class Extract < Lutaml::Model::Serializable
    attribute :language, :string
    attribute :extract_language, ExtractLanguage, collection: true
    attribute :core, Core, collection: true

    xml do
      root "termium_extract"
      namespace "http://termium.tpsgc-pwgsc.gc.ca/schemas/2012/06/Termium", "ns2"

      map_attribute "language", to: :language, namespace: nil
      map_element "extractLanguage", to: :extract_language, namespace: nil
      map_element "core", to: :core, namespace: nil
    end

  end
end

However, this fails because it treats all its children with the same ns2 namespace. Since the children elements have no namespace, they are all ignored.

Instead, this would work:

module Termium
  class Extract < Lutaml::Model::Serializable
    attribute :language, :string
    attribute :extract_language, ExtractLanguage, collection: true
    attribute :core, Core, collection: true

    xml do
      root "termium_extract"

      map_attribute "language", to: :language
      map_element "extractLanguage", to: :extract_language
      map_element "core", to: :core
    end
  end
end

Which is treating the element itself with no namespace, and the children have no namespace. This generates proper XML.

This is a bug in lutaml-model.

This can be found reproducible in the PR:

@ronaldtse ronaldtse added the bug Something isn't working label Nov 15, 2024
@HassanAkbar
Copy link
Member

@ronaldtse I have a question related to namespace behavior. If we explicitly set the namespace of the child to nil in a parent mapping will that be forwarded to all the child mappings as well? i.e

What will be the namespace for the Subject class if we have the following classes? We are explicitly passing namespace: nil to Core in Extract mappings, will the namespace for the Subject class also be nil?

class Extract < Lutaml::Model::Serializable
  attribute :core, Core, collection: true

  xml do
    root "termium_extract"
    namespace "http://termium.tpsgc-pwgsc.gc.ca/schemas/2012/06/Termium", "ns2"

    map_element "core", to: :core, namespace: nil
  end
end

class Core < Lutaml::Model::Serializable
  attribute :subject, Subject

  xml do
    root "core"

    map_element "subject", to: : subject
  end
end

class Subject < Lutaml::Model::Serializable
  attribute :abbreviation, :string
  attribute :details, :string

  xml do
    root "subject"
    namespace "https://subject-namespace", "sn"

    map_attribute "abbreviation", to: :abbreviation
    map_attribute "details", to: :details
  end
end

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

2 participants