lutaml-model 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dependent-repos.json +2 -1
  3. data/.rubocop_todo.yml +10 -80
  4. data/lib/lutaml/model/attribute.rb +6 -1
  5. data/lib/lutaml/model/schema/xml_compiler.rb +3 -1
  6. data/lib/lutaml/model/type/float.rb +0 -4
  7. data/lib/lutaml/model/version.rb +1 -1
  8. data/lib/lutaml/model/xml_adapter/xml_document.rb +0 -4
  9. data/lib/lutaml/model/xml_adapter/xml_element.rb +1 -3
  10. data/spec/address_spec.rb +3 -3
  11. data/spec/benchmarks/xml_parsing_benchmark_spec.rb +3 -3
  12. data/spec/lutaml/model/collection_spec.rb +0 -16
  13. data/spec/lutaml/model/json_adapter_spec.rb +26 -24
  14. data/spec/lutaml/model/key_value_mapping_spec.rb +1 -13
  15. data/spec/lutaml/model/schema/json_schema_spec.rb +12 -12
  16. data/spec/lutaml/model/schema/relaxng_schema_spec.rb +12 -12
  17. data/spec/lutaml/model/schema/xml_compiler_spec.rb +447 -147
  18. data/spec/lutaml/model/schema/xsd_schema_spec.rb +12 -12
  19. data/spec/lutaml/model/schema/yaml_schema_spec.rb +12 -12
  20. data/spec/lutaml/model/sequence_spec.rb +129 -113
  21. data/spec/lutaml/model/toml_adapter_spec.rb +27 -25
  22. data/spec/lutaml/model/transformation_spec.rb +80 -80
  23. data/spec/lutaml/model/type/boolean_spec.rb +4 -10
  24. data/spec/lutaml/model/type/decimal_spec.rb +3 -3
  25. data/spec/lutaml/model/type/hash_spec.rb +39 -26
  26. data/spec/lutaml/model/type/integer_spec.rb +2 -2
  27. data/spec/lutaml/model/type/time_without_date_spec.rb +0 -6
  28. data/spec/lutaml/model/type_spec.rb +6 -26
  29. data/spec/lutaml/model/utils_spec.rb +25 -30
  30. data/spec/lutaml/model/validation_spec.rb +9 -6
  31. data/spec/lutaml/model/xml_adapter/oga_adapter_spec.rb +2 -2
  32. data/spec/lutaml/model/xml_adapter/ox_adapter_spec.rb +2 -2
  33. data/spec/lutaml/model/xml_adapter/xml_namespace_spec.rb +280 -281
  34. data/spec/lutaml/model/xml_adapter_spec.rb +128 -120
  35. data/spec/lutaml/model/xml_mapping_spec.rb +23 -16
  36. data/spec/person_spec.rb +6 -16
  37. metadata +2 -2
@@ -4,366 +4,365 @@ require "lutaml/model/xml_adapter/ox_adapter"
4
4
  require "lutaml/model/xml_adapter/oga_adapter"
5
5
  require "lutaml/model"
6
6
 
7
- RSpec.shared_context "XML namespace models" do
8
- class TestModelNoPrefix < Lutaml::Model::Serializable
9
- attribute :name, :string
10
-
11
- xml do
12
- root "test"
13
- namespace "http://example.com/test", "test"
14
- map_element "name", to: :name
7
+ RSpec.describe "XmlNamespace" do
8
+ shared_context "with XML namespace models" do
9
+ class TestModelNoPrefix < Lutaml::Model::Serializable
10
+ attribute :name, :string
11
+
12
+ xml do
13
+ root "test"
14
+ namespace "http://example.com/test", "test"
15
+ map_element "name", to: :name
16
+ end
15
17
  end
16
- end
17
18
 
18
- class TestModelWithPrefix < Lutaml::Model::Serializable
19
- attribute :name, :string
19
+ class TestModelWithPrefix < Lutaml::Model::Serializable
20
+ attribute :name, :string
20
21
 
21
- xml do
22
- root "test"
23
- namespace "http://example.com/test", "test"
24
- map_element "name", to: :name
22
+ xml do
23
+ root "test"
24
+ namespace "http://example.com/test", "test"
25
+ map_element "name", to: :name
26
+ end
25
27
  end
26
- end
27
28
 
28
- class SamplePrefixedNamespacedModel < Lutaml::Model::Serializable
29
- attribute :id, :string
30
- attribute :lang, :string
31
- attribute :name, :string, default: -> { "Anonymous" }
32
- attribute :age, :integer, default: -> { 18 }
29
+ class SamplePrefixedNamespacedModel < Lutaml::Model::Serializable
30
+ attribute :id, :string
31
+ attribute :lang, :string
32
+ attribute :name, :string, default: -> { "Anonymous" }
33
+ attribute :age, :integer, default: -> { 18 }
33
34
 
34
- xml do
35
- root "SamplePrefixedNamespacedModel"
36
- namespace "http://example.com/foo", "foo"
35
+ xml do
36
+ root "SamplePrefixedNamespacedModel"
37
+ namespace "http://example.com/foo", "foo"
37
38
 
38
- map_attribute "id", to: :id
39
- map_attribute "lang", to: :lang,
40
- prefix: "xml",
41
- namespace: "http://example.com/xml"
39
+ map_attribute "id", to: :id
40
+ map_attribute "lang", to: :lang,
41
+ prefix: "xml",
42
+ namespace: "http://example.com/xml"
42
43
 
43
- map_element "Name", to: :name, prefix: "bar", namespace: "http://example.com/bar"
44
- map_element "Age", to: :age, prefix: "baz", namespace: "http://example.com/baz"
44
+ map_element "Name", to: :name, prefix: "bar", namespace: "http://example.com/bar"
45
+ map_element "Age", to: :age, prefix: "baz", namespace: "http://example.com/baz"
46
+ end
45
47
  end
46
- end
47
48
 
48
- class NamespaceNilPrefixedNamespaced < Lutaml::Model::Serializable
49
- attribute :namespace_model, SamplePrefixedNamespacedModel
49
+ class NamespaceNilPrefixedNamespaced < Lutaml::Model::Serializable
50
+ attribute :namespace_model, SamplePrefixedNamespacedModel
50
51
 
51
- xml do
52
- root "NamespaceNil"
53
- map_element "SamplePrefixedNamespacedModel", to: :namespace_model,
54
- namespace: nil,
55
- prefix: nil
52
+ xml do
53
+ root "NamespaceNil"
54
+ map_element "SamplePrefixedNamespacedModel", to: :namespace_model,
55
+ namespace: nil,
56
+ prefix: nil
57
+ end
56
58
  end
57
- end
58
59
 
59
- class SampleDefaultNamespacedModel < Lutaml::Model::Serializable
60
- attribute :id, :string
61
- attribute :lang, :string
62
- attribute :name, :string, default: -> { "Anonymous" }
63
- attribute :age, :integer, default: -> { 18 }
60
+ class SampleDefaultNamespacedModel < Lutaml::Model::Serializable
61
+ attribute :id, :string
62
+ attribute :lang, :string
63
+ attribute :name, :string, default: -> { "Anonymous" }
64
+ attribute :age, :integer, default: -> { 18 }
64
65
 
65
- xml do
66
- root "SampleDefaultNamespacedModel"
67
- namespace "http://example.com/foo"
66
+ xml do
67
+ root "SampleDefaultNamespacedModel"
68
+ namespace "http://example.com/foo"
68
69
 
69
- map_attribute "id", to: :id
70
- map_attribute "lang", to: :lang,
71
- prefix: "xml",
72
- namespace: "http://example.com/xml"
70
+ map_attribute "id", to: :id
71
+ map_attribute "lang", to: :lang,
72
+ prefix: "xml",
73
+ namespace: "http://example.com/xml"
73
74
 
74
- map_element "Name", to: :name, prefix: "bar", namespace: "http://example.com/bar"
75
- map_element "Age", to: :age, prefix: "baz", namespace: "http://example.com/baz"
75
+ map_element "Name", to: :name, prefix: "bar", namespace: "http://example.com/bar"
76
+ map_element "Age", to: :age, prefix: "baz", namespace: "http://example.com/baz"
77
+ end
76
78
  end
77
- end
78
79
 
79
- class NamespaceNilDefaultNamespaced < Lutaml::Model::Serializable
80
- attribute :namespace_model, SampleDefaultNamespacedModel
80
+ class NamespaceNilDefaultNamespaced < Lutaml::Model::Serializable
81
+ attribute :namespace_model, SampleDefaultNamespacedModel
81
82
 
82
- xml do
83
- root "NamespaceNil"
84
- map_element "SampleDefaultNamespacedModel", to: :namespace_model,
85
- namespace: nil,
86
- prefix: nil
83
+ xml do
84
+ root "NamespaceNil"
85
+ map_element "SampleDefaultNamespacedModel", to: :namespace_model,
86
+ namespace: nil,
87
+ prefix: nil
88
+ end
87
89
  end
88
- end
89
90
 
90
- class Body < Lutaml::Model::Serializable
91
- attribute :paragraph, :string
91
+ class Body < Lutaml::Model::Serializable
92
+ attribute :paragraph, :string
92
93
 
93
- xml do
94
- map_element "p", to: :paragraph
94
+ xml do
95
+ map_element "p", to: :paragraph
96
+ end
95
97
  end
96
- end
97
98
 
98
- class Element < Lutaml::Model::Serializable
99
- attribute :text, :string
100
- xml do
101
- root "test-element"
102
- namespace "http://www.test.com/schemas/test/1.0/", "test"
103
- map_content to: :text
99
+ class Element < Lutaml::Model::Serializable
100
+ attribute :text, :string
101
+ xml do
102
+ root "test-element"
103
+ namespace "http://www.test.com/schemas/test/1.0/", "test"
104
+ map_content to: :text
105
+ end
104
106
  end
105
- end
106
107
 
107
- class Front < Lutaml::Model::Serializable
108
- attribute :test_element, Element
108
+ class Front < Lutaml::Model::Serializable
109
+ attribute :test_element, Element
109
110
 
110
- xml do
111
- namespace "http://www.test.com/schemas/test/1.0/", "test"
112
- map_element "test-element", to: :test_element
111
+ xml do
112
+ namespace "http://www.test.com/schemas/test/1.0/", "test"
113
+ map_element "test-element", to: :test_element
114
+ end
113
115
  end
114
- end
115
116
 
116
- class Article < Lutaml::Model::Serializable
117
- attribute :front, Front
118
- attribute :body, Body
117
+ class Article < Lutaml::Model::Serializable
118
+ attribute :front, Front
119
+ attribute :body, Body
119
120
 
120
- xml do
121
- root "article"
122
- map_element "front", to: :front, prefix: "test", namespace: "http://www.test.com/schemas/test/1.0/"
123
- map_element "body", to: :body
121
+ xml do
122
+ root "article"
123
+ map_element "front", to: :front, prefix: "test", namespace: "http://www.test.com/schemas/test/1.0/"
124
+ map_element "body", to: :body
125
+ end
124
126
  end
125
- end
126
127
 
127
- class OwnedEnd < Lutaml::Model::Serializable
128
- attribute :id, :string
129
- attribute :type, :string
130
- attribute :uml_type, :string
128
+ class OwnedEnd < Lutaml::Model::Serializable
129
+ attribute :id, :string
130
+ attribute :type, :string
131
+ attribute :uml_type, :string
131
132
 
132
- xml do
133
- root "ownedEnd"
133
+ xml do
134
+ root "ownedEnd"
134
135
 
135
- map_attribute "id", to: :id, namespace: "http://www.omg.org/spec/XMI/20131001", prefix: "xmi"
136
- map_attribute "type", to: :type, namespace: "http://www.omg.org/spec/XMI/20131001", prefix: "xmi"
137
- map_attribute "type", to: :uml_type
136
+ map_attribute "id", to: :id, namespace: "http://www.omg.org/spec/XMI/20131001", prefix: "xmi"
137
+ map_attribute "type", to: :type, namespace: "http://www.omg.org/spec/XMI/20131001", prefix: "xmi"
138
+ map_attribute "type", to: :uml_type
139
+ end
138
140
  end
139
141
  end
140
- end
141
-
142
- RSpec.shared_examples "XML serialization with namespace" do |model_class, xml_string|
143
- it "serializes to XML" do
144
- model = model_class.new(name: "Test Name")
145
- expect(model.to_xml).to be_equivalent_to(xml_string)
146
- end
147
-
148
- it "deserializes from XML" do
149
- model = model_class.from_xml(xml_string)
150
- expect(model.name).to eq("Test Name")
151
- end
152
- end
153
-
154
- RSpec.shared_examples "an XML namespace parser" do |adapter_class|
155
- include_context "XML namespace models"
156
142
 
157
- around do |example|
158
- old_adapter = Lutaml::Model::Config.xml_adapter
159
- Lutaml::Model::Config.xml_adapter = adapter_class
160
- example.run
161
- ensure
162
- Lutaml::Model::Config.xml_adapter = old_adapter
163
- end
164
-
165
- context "with no prefix" do
166
- include_examples "XML serialization with namespace",
167
- TestModelNoPrefix,
168
- '<test xmlns="http://example.com/test"><name>Test Name</name></test>'
169
- end
143
+ shared_examples "XML serialization with namespace" do |model_class, xml_string|
144
+ it "serializes to XML" do
145
+ model = model_class.new(name: "Test Name")
146
+ expect(model.to_xml).to be_equivalent_to(xml_string)
147
+ end
170
148
 
171
- context "with prefix" do
172
- include_examples "XML serialization with namespace",
173
- TestModelWithPrefix,
174
- '<test:test xmlns:test="http://example.com/test"><test:name>Test Name</test:name></test:test>'
149
+ it "deserializes from XML" do
150
+ model = model_class.from_xml(xml_string)
151
+ expect(model.name).to eq("Test Name")
152
+ end
175
153
  end
176
154
 
177
- context "with prefixed namespace" do
178
- let(:attributes) { { name: "John Doe", age: 30 } }
179
- let(:model) { SamplePrefixedNamespacedModel.new(attributes) }
155
+ shared_examples "an XML namespace parser" do |adapter_class|
156
+ include_context "with XML namespace models"
180
157
 
181
- it "serializes to XML" do
182
- expected_xml = <<~XML
183
- <foo:SamplePrefixedNamespacedModel xmlns:foo="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
184
- <bar:Name>John Doe</bar:Name>
185
- <baz:Age>30</baz:Age>
186
- </foo:SamplePrefixedNamespacedModel>
187
- XML
188
-
189
- expect(model.to_xml).to be_equivalent_to(expected_xml)
158
+ around do |example|
159
+ old_adapter = Lutaml::Model::Config.xml_adapter
160
+ Lutaml::Model::Config.xml_adapter = adapter_class
161
+ example.run
162
+ ensure
163
+ Lutaml::Model::Config.xml_adapter = old_adapter
190
164
  end
191
165
 
192
- it "deserializes from XML" do
193
- xml = <<~XML
194
- <foo:SamplePrefixedNamespacedModel xmlns:foo="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
195
- <bar:Name>John Doe</bar:Name>
196
- <baz:Age>30</baz:Age>
197
- </foo:SamplePrefixedNamespacedModel>
198
- XML
199
-
200
- new_model = SamplePrefixedNamespacedModel.from_xml(xml)
201
- expect(new_model.name).to eq("John Doe")
202
- expect(new_model.age).to eq(30)
166
+ context "with no prefix" do
167
+ include_examples "XML serialization with namespace",
168
+ TestModelNoPrefix,
169
+ '<test xmlns="http://example.com/test"><name>Test Name</name></test>'
203
170
  end
204
171
 
205
- it "round-trips if namespace is set" do
206
- xml = <<~XML
207
- <foo:SamplePrefixedNamespacedModel xml:lang="en" xmlns:foo="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
208
- <bar:Name>John Doe</bar:Name>
209
- <baz:Age>30</baz:Age>
210
- </foo:SamplePrefixedNamespacedModel>
211
- XML
212
-
213
- doc = SamplePrefixedNamespacedModel.from_xml(xml)
214
- generated_xml = doc.to_xml
215
- expect(generated_xml).to be_equivalent_to(xml)
172
+ context "with prefix" do
173
+ include_examples "XML serialization with namespace",
174
+ TestModelWithPrefix,
175
+ '<test:test xmlns:test="http://example.com/test"><test:name>Test Name</test:name></test:test>'
216
176
  end
217
177
 
218
- it "round-trips if namespace is set to nil in parent" do
219
- xml = <<~XML
220
- <NamespaceNil xmlns:foo="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
221
- <SamplePrefixedNamespacedModel xml:lang="en">
178
+ context "with prefixed namespace" do
179
+ let(:attributes) { { name: "John Doe", age: 30 } }
180
+ let(:model) { SamplePrefixedNamespacedModel.new(attributes) }
181
+
182
+ let(:xml) do
183
+ <<~XML
184
+ <foo:SamplePrefixedNamespacedModel xmlns:foo="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
222
185
  <bar:Name>John Doe</bar:Name>
223
186
  <baz:Age>30</baz:Age>
224
- </SamplePrefixedNamespacedModel>
225
- </NamespaceNil>
226
- XML
187
+ </foo:SamplePrefixedNamespacedModel>
188
+ XML
189
+ end
227
190
 
228
- doc = NamespaceNilPrefixedNamespaced.from_xml(xml)
229
- generated_xml = doc.to_xml
230
- expect(generated_xml).to be_equivalent_to(xml)
231
- end
232
- end
191
+ let(:xml_with_lang) do
192
+ <<~XML
193
+ <foo:SamplePrefixedNamespacedModel xml:lang="en" xmlns:foo="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
194
+ <bar:Name>John Doe</bar:Name>
195
+ <baz:Age>30</baz:Age>
196
+ </foo:SamplePrefixedNamespacedModel>
197
+ XML
198
+ end
233
199
 
234
- context "with default namespace" do
235
- let(:attributes) { { name: "Jane Smith", age: 25 } }
236
- let(:model) { SampleDefaultNamespacedModel.new(attributes) }
200
+ it "serializes to XML" do
201
+ expect(model.to_xml).to be_equivalent_to(xml)
202
+ end
237
203
 
238
- it "serializes to XML" do
239
- expected_xml = <<~XML
240
- <SampleDefaultNamespacedModel xmlns="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
241
- <bar:Name>Jane Smith</bar:Name>
242
- <baz:Age>25</baz:Age>
243
- </SampleDefaultNamespacedModel>
244
- XML
245
-
246
- expect(model.to_xml).to be_equivalent_to(expected_xml)
247
- end
204
+ it "deserializes from XML" do
205
+ new_model = SamplePrefixedNamespacedModel.from_xml(xml)
206
+ expect(new_model.name).to eq("John Doe")
207
+ expect(new_model.age).to eq(30)
208
+ end
248
209
 
249
- it "deserializes from XML" do
250
- xml = <<~XML
251
- <SampleDefaultNamespacedModel xmlns="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
252
- <bar:Name>Jane Smith</bar:Name>
253
- <baz:Age>25</baz:Age>
254
- </SampleDefaultNamespacedModel>
255
- XML
256
-
257
- new_model = SampleDefaultNamespacedModel.from_xml(xml)
258
- expect(new_model.name).to eq("Jane Smith")
259
- expect(new_model.age).to eq(25)
260
- end
210
+ it "round-trips if namespace is set" do
211
+ doc = SamplePrefixedNamespacedModel.from_xml(xml_with_lang)
212
+ generated_xml = doc.to_xml
213
+ expect(generated_xml).to be_equivalent_to(xml_with_lang)
214
+ end
261
215
 
262
- it "round-trips if namespace is set" do
263
- xml = <<~XML
264
- <SampleDefaultNamespacedModel xml:lang="en" xmlns="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
265
- <bar:Name>Jane Smith</bar:Name>
266
- <baz:Age>25</baz:Age>
267
- </SampleDefaultNamespacedModel>
268
- XML
269
-
270
- doc = SampleDefaultNamespacedModel.from_xml(xml)
271
- generated_xml = doc.to_xml
272
- expect(generated_xml).to be_equivalent_to(xml)
216
+ it "round-trips if namespace is set to nil in parent" do
217
+ xml = <<~XML
218
+ <NamespaceNil xmlns:foo="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
219
+ <SamplePrefixedNamespacedModel xml:lang="en">
220
+ <bar:Name>John Doe</bar:Name>
221
+ <baz:Age>30</baz:Age>
222
+ </SamplePrefixedNamespacedModel>
223
+ </NamespaceNil>
224
+ XML
225
+
226
+ doc = NamespaceNilPrefixedNamespaced.from_xml(xml)
227
+ generated_xml = doc.to_xml
228
+ expect(generated_xml).to be_equivalent_to(xml)
229
+ end
273
230
  end
274
231
 
275
- it "round-trips if namespace is set to nil in parent" do
276
- xml = <<~XML
277
- <NamespaceNil xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
278
- <SampleDefaultNamespacedModel xml:lang="en">
232
+ context "with default namespace" do
233
+ let(:attributes) { { name: "Jane Smith", age: 25 } }
234
+ let(:model) { SampleDefaultNamespacedModel.new(attributes) }
235
+
236
+ it "serializes to XML" do
237
+ expected_xml = <<~XML
238
+ <SampleDefaultNamespacedModel xmlns="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
279
239
  <bar:Name>Jane Smith</bar:Name>
280
240
  <baz:Age>25</baz:Age>
281
241
  </SampleDefaultNamespacedModel>
282
- </NamespaceNil>
283
- XML
242
+ XML
284
243
 
285
- doc = NamespaceNilDefaultNamespaced.from_xml(xml)
286
- generated_xml = doc.to_xml
287
- expect(generated_xml).to be_equivalent_to(xml)
288
- end
289
- end
244
+ expect(model.to_xml).to be_equivalent_to(expected_xml)
245
+ end
290
246
 
291
- context "when custom namespace is used" do
292
- let(:xml_input) do
293
- <<~XML
294
- <article xmlns:test="http://www.test.com/schemas/test/1.0/">
295
- <test:front>
296
- <test:test-element>Text Here</test:test-element>
297
- </test:front>
298
- <body>
299
- <p>This is a paragraph</p>
300
- </body>
301
- </article>
302
- XML
303
- end
247
+ it "deserializes from XML" do
248
+ xml = <<~XML
249
+ <SampleDefaultNamespacedModel xmlns="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
250
+ <bar:Name>Jane Smith</bar:Name>
251
+ <baz:Age>25</baz:Age>
252
+ </SampleDefaultNamespacedModel>
253
+ XML
304
254
 
305
- describe "XML serialization" do
306
- it "correctly deserializes from XML" do
307
- article = Article.from_xml(xml_input)
308
- expect(article.body.paragraph).to eq("This is a paragraph")
255
+ new_model = SampleDefaultNamespacedModel.from_xml(xml)
256
+ expect(new_model.name).to eq("Jane Smith")
257
+ expect(new_model.age).to eq(25)
309
258
  end
310
259
 
311
- it "round-trips XML" do
312
- article = Article.from_xml(xml_input)
313
- output_xml = article.to_xml(pretty: true)
260
+ it "round-trips if namespace is set" do
261
+ xml = <<~XML
262
+ <SampleDefaultNamespacedModel xml:lang="en" xmlns="http://example.com/foo" xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
263
+ <bar:Name>Jane Smith</bar:Name>
264
+ <baz:Age>25</baz:Age>
265
+ </SampleDefaultNamespacedModel>
266
+ XML
267
+
268
+ doc = SampleDefaultNamespacedModel.from_xml(xml)
269
+ generated_xml = doc.to_xml
270
+ expect(generated_xml).to be_equivalent_to(xml)
271
+ end
314
272
 
315
- expect(output_xml).to be_equivalent_to(xml_input)
273
+ it "round-trips if namespace is set to nil in parent" do
274
+ xml = <<~XML
275
+ <NamespaceNil xmlns:bar="http://example.com/bar" xmlns:baz="http://example.com/baz">
276
+ <SampleDefaultNamespacedModel xml:lang="en">
277
+ <bar:Name>Jane Smith</bar:Name>
278
+ <baz:Age>25</baz:Age>
279
+ </SampleDefaultNamespacedModel>
280
+ </NamespaceNil>
281
+ XML
282
+
283
+ doc = NamespaceNilDefaultNamespaced.from_xml(xml)
284
+ generated_xml = doc.to_xml
285
+ expect(generated_xml).to be_equivalent_to(xml)
316
286
  end
317
287
  end
318
- end
319
288
 
320
- context "when two attributes have same name but different prefix" do
321
- let(:xml_input) do
322
- <<~XML
323
- <ownedEnd xmlns:xmi="http://www.omg.org/spec/XMI/20131001"
324
- xmi:type="xmi_type"
325
- xmi:id="my_id"
326
- type="test" />
327
- XML
328
- end
289
+ context "when custom namespace is used" do
290
+ let(:xml_input) do
291
+ <<~XML
292
+ <article xmlns:test="http://www.test.com/schemas/test/1.0/">
293
+ <test:front>
294
+ <test:test-element>Text Here</test:test-element>
295
+ </test:front>
296
+ <body>
297
+ <p>This is a paragraph</p>
298
+ </body>
299
+ </article>
300
+ XML
301
+ end
329
302
 
330
- describe "XML serialization" do
331
- it "correctly deserializes from XML" do
332
- owned_end = OwnedEnd.from_xml(xml_input)
303
+ describe "XML serialization" do
304
+ it "correctly deserializes from XML" do
305
+ article = Article.from_xml(xml_input)
306
+ expect(article.body.paragraph).to eq("This is a paragraph")
307
+ end
333
308
 
334
- expect(owned_end.id).to eq("my_id")
335
- expect(owned_end.type).to eq("xmi_type")
336
- expect(owned_end.uml_type).to eq("test")
337
- end
309
+ it "round-trips XML" do
310
+ article = Article.from_xml(xml_input)
311
+ output_xml = article.to_xml(pretty: true)
338
312
 
339
- it "correctly serializes to XML" do
340
- owned_end = OwnedEnd.new(
341
- id: "my_id",
342
- type: "xmi_type",
343
- uml_type: "test",
344
- )
313
+ expect(output_xml).to be_equivalent_to(xml_input)
314
+ end
315
+ end
316
+ end
345
317
 
346
- expect(owned_end.to_xml).to be_equivalent_to(xml_input)
318
+ context "when two attributes have same name but different prefix" do
319
+ let(:xml_input) do
320
+ <<~XML
321
+ <ownedEnd xmlns:xmi="http://www.omg.org/spec/XMI/20131001"
322
+ xmi:type="xmi_type"
323
+ xmi:id="my_id"
324
+ type="test" />
325
+ XML
347
326
  end
348
327
 
349
- it "round-trips XML" do
350
- owned_end = OwnedEnd.from_xml(xml_input)
351
- output_xml = owned_end.to_xml
328
+ describe "XML serialization" do
329
+ it "correctly deserializes from XML" do
330
+ owned_end = OwnedEnd.from_xml(xml_input)
352
331
 
353
- expect(output_xml).to be_equivalent_to(xml_input)
332
+ expect(owned_end.id).to eq("my_id")
333
+ expect(owned_end.type).to eq("xmi_type")
334
+ expect(owned_end.uml_type).to eq("test")
335
+ end
336
+
337
+ it "correctly serializes to XML" do
338
+ owned_end = OwnedEnd.new(
339
+ id: "my_id",
340
+ type: "xmi_type",
341
+ uml_type: "test",
342
+ )
343
+
344
+ expect(owned_end.to_xml).to be_equivalent_to(xml_input)
345
+ end
346
+
347
+ it "round-trips XML" do
348
+ owned_end = OwnedEnd.from_xml(xml_input)
349
+ output_xml = owned_end.to_xml
350
+
351
+ expect(output_xml).to be_equivalent_to(xml_input)
352
+ end
354
353
  end
355
354
  end
356
355
  end
357
- end
358
356
 
359
- RSpec.describe Lutaml::Model::XmlAdapter::NokogiriAdapter do
360
- it_behaves_like "an XML namespace parser", described_class
361
- end
357
+ describe Lutaml::Model::XmlAdapter::NokogiriAdapter do
358
+ it_behaves_like "an XML namespace parser", described_class
359
+ end
362
360
 
363
- RSpec.describe Lutaml::Model::XmlAdapter::OxAdapter do
364
- it_behaves_like "an XML namespace parser", described_class
365
- end
361
+ describe Lutaml::Model::XmlAdapter::OxAdapter do
362
+ it_behaves_like "an XML namespace parser", described_class
363
+ end
366
364
 
367
- RSpec.describe Lutaml::Model::XmlAdapter::OgaAdapter do
368
- it_behaves_like "an XML namespace parser", described_class
365
+ describe Lutaml::Model::XmlAdapter::OgaAdapter do
366
+ it_behaves_like "an XML namespace parser", described_class
367
+ end
369
368
  end