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

fix: namespace nil when explicitly given nil in mapping #93

Merged
merged 3 commits into from
Sep 24, 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
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-09-18 12:28:37 UTC using RuboCop version 1.65.1.
# on 2024-09-23 15:00:23 UTC using RuboCop version 1.65.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 98
# Offense count: 100
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Expand Down Expand Up @@ -63,15 +63,15 @@ Metrics/CyclomaticComplexity:
- 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
- 'lib/lutaml/model/xml_adapter/xml_document.rb'

# Offense count: 43
# Offense count: 45
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 51

# Offense count: 4
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Metrics/ParameterLists:
Max: 9
Max: 10

# Offense count: 22
# Configuration parameters: AllowedMethods, AllowedPatterns, Max.
Expand Down Expand Up @@ -123,7 +123,7 @@ RSpec/MultipleDescribes:
- 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
- 'spec/lutaml/model/xml_adapter_spec.rb'

# Offense count: 97
# Offense count: 98
RSpec/MultipleExpectations:
Max: 14

Expand Down
6 changes: 6 additions & 0 deletions lib/lutaml/model/mapping_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(
delegate: nil,
mixed_content: false,
namespace_set: false,
prefix_set: false,
child_mappings: nil
)
@name = name
Expand All @@ -26,6 +27,7 @@ def initialize(
@delegate = delegate
@mixed_content = mixed_content
@namespace_set = namespace_set
@prefix_set = prefix_set
@child_mappings = child_mappings
end

Expand Down Expand Up @@ -74,6 +76,10 @@ def namespace_set?
@namespace_set
end

def prefix_set?
@prefix_set
end

def content_mapping?
name.nil?
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lutaml/model/serialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def attribute(name, type, options = {})
define_method(:"of_#{format}") do |doc|
if doc.is_a?(Array)
return doc.map do |item|
apply_mappings(item.to_h, format)
send(:"of_#{format}", item)
end
end

Expand Down
10 changes: 8 additions & 2 deletions lib/lutaml/model/xml_adapter/builder/nokogiri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ def add_attribute(element, name, value)
element[name] = value
end

def create_and_add_element(element_name, prefix: nil, attributes: {})
add_namespace_prefix(prefix) if prefix
def create_and_add_element(
element_name,
prefix: (prefix_unset = true
nil),
attributes: {}
)
add_namespace_prefix(prefix)

if block_given?
public_send(element_name, attributes) do
xml.parent.namespace = nil if prefix.nil? && !prefix_unset
yield(self)
end
else
Expand Down
32 changes: 20 additions & 12 deletions lib/lutaml/model/xml_adapter/xml_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,29 @@ def add_to_xml(xml, element, prefix, value, options = {})
value,
options.merge({ rule: rule, attribute: attribute }),
)
else
elsif rule.prefix_set?
xml.create_and_add_element(rule.name, prefix: prefix) do
if !value.nil?
serialized_value = attribute.type.serialize(value)

if attribute.type == Lutaml::Model::Type::Hash
serialized_value.each do |key, val|
xml.create_and_add_element(key) do |element|
element.text(val)
end
end
else
xml.add_text(xml, serialized_value)
add_value(xml, value, attribute)
end
else
xml.create_and_add_element(rule.name) do
add_value(xml, value, attribute)
end
end
end

def add_value(xml, value, attribute)
if !value.nil?
serialized_value = attribute.type.serialize(value)

if attribute.type == Lutaml::Model::Type::Hash
serialized_value.each do |key, val|
xml.create_and_add_element(key) do |element|
element.text(val)
end
end
else
xml.add_text(xml, serialized_value)
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/lutaml/model/xml_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def map_element(
delegate: nil,
namespace: (namespace_set = false
nil),
prefix: nil
prefix: (prefix_set = false
nil)
)
validate!(name, to, with)

Expand All @@ -57,6 +58,7 @@ def map_element(
namespace: namespace,
prefix: prefix,
namespace_set: namespace_set != false,
prefix_set: prefix_set != false,
)
end

Expand All @@ -68,7 +70,8 @@ def map_attribute(
delegate: nil,
namespace: (namespace_set = false
nil),
prefix: nil
prefix: (prefix_set = false
nil)
)
validate!(name, to, with)

Expand All @@ -81,6 +84,7 @@ def map_attribute(
namespace: namespace,
prefix: prefix,
namespace_set: namespace_set != false,
prefix_set: prefix_set != false,
)
end

Expand Down
4 changes: 3 additions & 1 deletion lib/lutaml/model/xml_mapping_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def initialize(
namespace: nil,
prefix: nil,
mixed_content: false,
namespace_set: false
namespace_set: false,
prefix_set: false
)
super(
name,
Expand All @@ -24,6 +25,7 @@ def initialize(
delegate: delegate,
mixed_content: mixed_content,
namespace_set: namespace_set,
prefix_set: prefix_set,
)

@namespace = if namespace.to_s == "inherit"
Expand Down
52 changes: 52 additions & 0 deletions spec/lutaml/model/xml_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ class Paragraph < Lutaml::Model::Serializable
end
end

module XmlMapping
class ChildNamespaceNil < Lutaml::Model::Serializable
attribute :element_default_namespace, :string
attribute :element_nil_namespace, :string
attribute :element_new_namespace, :string

xml do
root "ChildNamespaceNil"
namespace "http://www.omg.org/spec/XMI/20131001", "xmi"

# this will inherit the namespace from the parent i.e <xmi:ElementDefaultNamespace>
map_element "ElementDefaultNamespace", to: :element_default_namespace

# this will have nil namesapce applied i.e <ElementNilNamespace>
map_element "ElementNilNamespace", to: :element_nil_namespace,
prefix: nil,
namespace: nil

# this will have new namespace i.e <new:ElementNewNamespace>
map_element "ElementNewNamespace", to: :element_new_namespace,
prefix: "new",
namespace: "http://www.omg.org/spec/XMI/20161001"
end
end
end

RSpec.describe Lutaml::Model::XmlMapping do
let(:mapping) { described_class.new }

Expand Down Expand Up @@ -112,6 +138,32 @@ class Paragraph < Lutaml::Model::Serializable
end
end

context "with nil element-level namespace" do
let(:expected_xml) do
<<~XML
<xmi:ChildNamespaceNil xmlns:xmi="http://www.omg.org/spec/XMI/20131001" xmlns:new="http://www.omg.org/spec/XMI/20161001">
<xmi:ElementDefaultNamespace>Default namespace</xmi:ElementDefaultNamespace>
<ElementNilNamespace>No namespace</ElementNilNamespace>
<new:ElementNewNamespace>New namespace</new:ElementNewNamespace>
</xmi:ChildNamespaceNil>
XML
end

let(:model) do
XmlMapping::ChildNamespaceNil.new(
{
element_default_namespace: "Default namespace",
element_nil_namespace: "No namespace",
element_new_namespace: "New namespace",
},
)
end

it "expect to apply correct namespaces" do
expect(model.to_xml).to be_equivalent_to(expected_xml)
end
end

context "with schemaLocation" do
let(:xml) do
<<~XML
Expand Down
Loading