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

do not override parent mapping when inheriting #98

Merged
merged 2 commits into from
Oct 4, 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
2 changes: 1 addition & 1 deletion lib/lutaml/model/serialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def attribute(name, type, options = {})
Lutaml::Model::Config::AVAILABLE_FORMATS.each do |format|
define_method(format) do |&block|
klass = format == :xml ? XmlMapping : KeyValueMapping
mappings[format] = klass.new
mappings[format] ||= klass.new
mappings[format].instance_eval(&block)

if format == :xml && !mappings[format].root_element
Expand Down
102 changes: 51 additions & 51 deletions spec/lutaml/model/delegation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,87 +145,100 @@ class Ceramic < Lutaml::Model::Serializable
expect(xml_data).to include('<?xml version="1.0" encoding="ASCII"?>')
end

it "sets the default namespace of <delegation>" do
it "sets the namespace of a particular element inside Ceramic" do
Delegation::Ceramic.class_eval do
xml do
root "delegation"
namespace "https://example.com/delegation/1.2"
map_element "type", to: :type
map_element "type",
to: :type,
namespace: "https://example.com/type/1.2",
prefix: "type"
map_element "color", to: :color, delegate: :glaze
map_element "finish", to: :finish, delegate: :glaze
end
end

delegation_class = Delegation::Ceramic

delegation = delegation_class.from_yaml(yaml_data)
xml_data = delegation.to_xml(
pretty: true,
declaration: true,
encoding: "UTF-8",
)
expect(xml_data).to(
include('<delegation xmlns="https://example.com/delegation/1.2">'),
)
expect(xml_data).to include('<delegation xmlns:type="https://example.com/type/1.2">')
expect(xml_data).to include("<type:type>Vase</type:type>")
end

it "sets the namespace of <delegation> with a prefix" do
it "sets the namespace of a particular attribute inside <delegation>" do
Delegation::Ceramic.class_eval do
attribute :date, Lutaml::Model::Type::Date

xml do
root "delegation"
namespace "https://example.com/delegation/1.2", "del"
map_attribute "date",
to: :date,
namespace: "https://example.com/delegation/1.2",
prefix: "del"
map_element "type", to: :type
map_element "color", to: :color, delegate: :glaze
map_element "finish", to: :finish, delegate: :glaze
end
end

delegation_class = Delegation::Ceramic
delegation = delegation_class.from_yaml(yaml_data)
delegation = delegation_class.new(
type: "Vase",
glaze: Delegation::Glaze.new(
color: "Blue",
finish: "Glossy",
),
date: "2024-06-08",
)

xml_data = delegation.to_xml(
pretty: true,
declaration: true,
encoding: "UTF-8",
)

expect(xml_data).to(
include(
'<del:delegation xmlns:del="https://example.com/delegation/1.2">',
),
)
delegation_attributes = [
'xmlns:del="https://example.com/delegation/1.2"',
'del:date="2024-06-08"',
]

expect(xml_data).to include("<delegation #{delegation_attributes.join(' ')}>")
end

it "sets the namespace of a particular element inside Ceramic" do
it "sets the default namespace of <delegation>" do
Delegation::Ceramic.class_eval do
xml do
root "delegation"
map_element "type",
to: :type,
namespace: "https://example.com/type/1.2",
prefix: "type"
namespace "https://example.com/delegation/1.2"
map_element "type", to: :type
map_element "color", to: :color, delegate: :glaze
map_element "finish", to: :finish, delegate: :glaze
end
end

delegation_class = Delegation::Ceramic

delegation = delegation_class.from_yaml(yaml_data)
xml_data = delegation.to_xml(
pretty: true,
declaration: true,
encoding: "UTF-8",
)
expect(xml_data).to include('<delegation xmlns:type="https://example.com/type/1.2">')
expect(xml_data).to include("<type:type>Vase</type:type>")
expect(xml_data).to(
include('<delegation xmlns="https://example.com/delegation/1.2" xmlns:del="https://example.com/delegation/1.2">'),
)
end

it "sets the namespace of <delegation> and also" \
"a particular element inside using :inherit" do
it "sets the namespace of <delegation> with a prefix" do
Delegation::Ceramic.class_eval do
xml do
root "delegation"
namespace "https://example.com/delegation/1.2", "del"
map_element "type", to: :type # , namespace: :inherit
map_element "type", to: :type
map_element "color", to: :color, delegate: :glaze
map_element "finish", to: :finish, delegate: :glaze
end
Expand All @@ -239,50 +252,37 @@ class Ceramic < Lutaml::Model::Serializable
encoding: "UTF-8",
)

delegation_attribute = 'xmlns:del="https://example.com/delegation/1.2">'

expect(xml_data).to include("<del:delegation #{delegation_attribute}")
expect(xml_data).to include("<del:type>Vase</del:type>")
expect(xml_data).to(
include(
'<del:delegation xmlns:del="https://example.com/delegation/1.2">',
),
)
end

it "sets the namespace of a particular attribute inside <delegation>" do
it "sets the namespace of <delegation> and also" \
"a particular element inside using :inherit" do
Delegation::Ceramic.class_eval do
attribute :date, Lutaml::Model::Type::Date

xml do
root "delegation"
map_attribute "date",
to: :date,
namespace: "https://example.com/delegation/1.2",
prefix: "del"
map_element "type", to: :type
namespace "https://example.com/delegation/1.2", "del"
map_element "type", to: :type # , namespace: :inherit
map_element "color", to: :color, delegate: :glaze
map_element "finish", to: :finish, delegate: :glaze
end
end

delegation_class = Delegation::Ceramic
delegation = delegation_class.new(
type: "Vase",
glaze: Delegation::Glaze.new(
color: "Blue",
finish: "Glossy",
),
date: "2024-06-08",
)

delegation = delegation_class.from_yaml(yaml_data)
xml_data = delegation.to_xml(
pretty: true,
declaration: true,
encoding: "UTF-8",
)

delegation_attributes = [
'xmlns:del="https://example.com/delegation/1.2"',
'del:date="2024-06-08"',
]
delegation_attribute = 'xmlns:del="https://example.com/delegation/1.2">'

expect(xml_data).to include("<delegation #{delegation_attributes.join(' ')}>")
expect(xml_data).to include("<del:delegation #{delegation_attribute}")
expect(xml_data).to include("<del:type>Vase</del:type>")
end

it "sets the namespace of <delegation> and also" \
Expand Down
38 changes: 21 additions & 17 deletions spec/lutaml/model/inheritance_spec.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
require "spec_helper"
require "lutaml/model"

class Parent < Lutaml::Model::Serializable
attribute :text, Lutaml::Model::Type::String
attribute :id, Lutaml::Model::Type::String
attribute :name, Lutaml::Model::Type::String
end

class Child < Parent
attribute :age, Lutaml::Model::Type::Integer

xml do
root "child"
module InheritanceSpec
class Parent < Lutaml::Model::Serializable
attribute :text, Lutaml::Model::Type::String
attribute :id, Lutaml::Model::Type::String
attribute :name, Lutaml::Model::Type::String

xml do
map_content to: :text

map_attribute "id", to: :id
map_element "name", to: :name
end
end

map_content to: :text
class Child < Parent
attribute :age, Lutaml::Model::Type::Integer

map_attribute "id", to: :id
xml do
root "child"

map_element "age", to: :age
map_element "name", to: :name
map_element "age", to: :age
end
end
end

RSpec.describe "Inheritance" do
subject(:child_object) do
Child.new(
InheritanceSpec::Child.new(
{
text: "Some text",
name: "John Doe",
Expand All @@ -35,7 +39,7 @@ class Child < Parent
end

let(:expected_xml) do
'<child id="foobar"><age>30</age><name>John Doe</name>Some text</child>'
'<child id="foobar"><name>John Doe</name><age>30</age>Some text</child>'
end

it "uses parent attributes" do
Expand Down
Loading
Loading