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

Parent namespace for XML mapping #150

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/lutaml/model/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,10 @@ def serialize(value, format, options = {})

def cast(value, format, options = {})
value ||= [] if collection?
instance = options[:instance]

if value.is_a?(Array)
value.map do |v|
cast(v, format, instance: instance)
cast(v, format, options)
end
elsif type <= Serialize && value.is_a?(Hash)
type.apply_mappings(value, format, options)
Expand Down
15 changes: 8 additions & 7 deletions lib/lutaml/model/serialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def inherited(subclass)
subclass.instance_variable_set(:@attributes,
Utils.deep_dup(@attributes))
subclass.instance_variable_set(:@mappings, Utils.deep_dup(@mappings))
subclass.instance_variable_set(:@model, subclass)
subclass.instance_variable_set(:@model, @model || subclass)
end

def model(klass = nil)
Expand Down Expand Up @@ -310,6 +310,7 @@ def apply_mappings(doc, format, options = {})
def apply_xml_mapping(doc, instance, options = {})
return instance unless doc

options[:default_namespace] = mappings_for(:xml)&.namespace_uri if options[:default_namespace].nil?
mappings = mappings_for(:xml).mappings

if doc.is_a?(Array)
Expand Down Expand Up @@ -340,14 +341,14 @@ def apply_xml_mapping(doc, instance, options = {})
doc.node.inner_xml
elsif rule.content_mapping?
doc["text"]
elsif doc.key_exist?(rule.namespaced_name)
doc.fetch(rule.namespaced_name)
elsif doc.key_exist?(rule.namespaced_name(options[:default_namespace]))
doc.fetch(rule.namespaced_name(options[:default_namespace]))
else
defaults_used << rule.to
rule.to_value_for(instance)
end

value = normalize_xml_value(value, rule)
value = normalize_xml_value(value, rule, options)
rule.deserialize(instance, value, attributes, self)
end

Expand Down Expand Up @@ -388,7 +389,7 @@ def apply_hash_mapping(doc, instance, format, _options = {})
instance
end

def normalize_xml_value(value, rule)
def normalize_xml_value(value, rule, options = {})
attr = attribute_for_rule(rule)

value = [value].compact if attr&.collection? && !value.is_a?(Array)
Expand All @@ -405,11 +406,11 @@ def normalize_xml_value(value, rule)

return value unless cast_value?(attr, rule)

options.merge(caller_class: self, mixed_content: rule.mixed_content)
attr.cast(
value,
:xml,
caller_class: self,
mixed_content: rule.mixed_content,
options,
)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/lutaml/model/xml_mapping_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ def prefixed_name
end
end

def namespaced_name
def namespaced_name(parent_namespace = nil)
if name == "lang"
"#{prefix}:#{name}"
elsif namespace_set? || @attribute
[namespace, name].compact.join(":")
elsif default_namespace
"#{default_namespace}:#{name}"
else
name
[parent_namespace, name].compact.join(":")
end
end

Expand Down
57 changes: 57 additions & 0 deletions spec/lutaml/model/namespace_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "spec_helper"

module NamespaceSpec
class NestedChild < Lutaml::Model::Serializable
attribute :name, :string

xml do
root "NestedChild"

map_element :name, to: :name
end
end

class Child < Lutaml::Model::Serializable
attribute :nested_child, NestedChild

xml do
root "NestedChild"

map_element :NestedChild, to: :nested_child
end
end

class Parent < Lutaml::Model::Serializable
attribute :child, Child

xml do
root "Parent"
namespace "https://abc.com"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the test we should test additional nesting levels and when the namespaces change in between. If the parent and child both have the same namespace, what happens? Need to update documentation. Going to approve and merge this first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the test we should test additional nesting levels and when the namespaces change in between. If the parent and child both have the same namespace, what happens? Need to update documentation. Going to approve and merge this first.


map_element :Child, to: :child
end
end
end

RSpec.describe "NamespaceSpec" do
let(:parsed) { NamespaceSpec::Parent.from_xml(xml) }
let(:xml) do
<<~XML
<Parent xmlns="https://abc.com">
<Child>
<NestedChild>
<name>Rogger moore</name>
</NestedChild>
</Child>
</Parent>
XML
end

it "parses nested child using root namespace" do
expect(parsed.child.nested_child.name).to eq("Rogger moore")
end

it "round-trips xml" do
expect(parsed.to_xml).to be_equivalent_to(xml)
end
end
Loading