lutaml-model 0.5.3 → 0.5.4

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-tests.yml +2 -0
  3. data/.rubocop_todo.yml +39 -13
  4. data/Gemfile +1 -0
  5. data/README.adoc +396 -23
  6. data/lib/lutaml/model/constants.rb +7 -0
  7. data/lib/lutaml/model/error/type/invalid_value_error.rb +19 -0
  8. data/lib/lutaml/model/error.rb +1 -0
  9. data/lib/lutaml/model/key_value_mapping.rb +31 -2
  10. data/lib/lutaml/model/mapping_hash.rb +8 -0
  11. data/lib/lutaml/model/mapping_rule.rb +4 -0
  12. data/lib/lutaml/model/schema/templates/simple_type.rb +247 -0
  13. data/lib/lutaml/model/schema/xml_compiler.rb +720 -0
  14. data/lib/lutaml/model/schema.rb +5 -0
  15. data/lib/lutaml/model/serialize.rb +24 -8
  16. data/lib/lutaml/model/toml_adapter/toml_rb_adapter.rb +1 -2
  17. data/lib/lutaml/model/type/hash.rb +11 -11
  18. data/lib/lutaml/model/version.rb +1 -1
  19. data/lib/lutaml/model/xml_adapter/nokogiri_adapter.rb +5 -1
  20. data/lib/lutaml/model/xml_adapter/xml_document.rb +11 -15
  21. data/lib/lutaml/model/xml_mapping.rb +4 -2
  22. data/lib/lutaml/model/xml_mapping_rule.rb +1 -4
  23. data/lib/lutaml/model.rb +1 -0
  24. data/spec/fixtures/xml/invalid_math_document.xml +4 -0
  25. data/spec/fixtures/xml/math_document_schema.xsd +56 -0
  26. data/spec/fixtures/xml/test_schema.xsd +53 -0
  27. data/spec/fixtures/xml/valid_math_document.xml +4 -0
  28. data/spec/lutaml/model/cdata_spec.rb +2 -2
  29. data/spec/lutaml/model/custom_model_spec.rb +7 -20
  30. data/spec/lutaml/model/key_value_mapping_spec.rb +27 -0
  31. data/spec/lutaml/model/map_all_spec.rb +188 -0
  32. data/spec/lutaml/model/mixed_content_spec.rb +15 -15
  33. data/spec/lutaml/model/schema/xml_compiler_spec.rb +1431 -0
  34. data/spec/lutaml/model/with_child_mapping_spec.rb +2 -2
  35. data/spec/lutaml/model/xml_adapter/xml_namespace_spec.rb +52 -0
  36. data/spec/lutaml/model/xml_mapping_spec.rb +108 -1
  37. metadata +12 -2
@@ -341,7 +341,7 @@ RSpec.describe ChildMapping do
341
341
  actual = Lutaml::Model::Config.toml_adapter.parse(instance.to_toml)
342
342
  expected = Lutaml::Model::Config.toml_adapter.parse(toml)
343
343
 
344
- expect(actual.attributes).to eq(expected.attributes)
344
+ expect(actual).to eq(expected)
345
345
  end
346
346
 
347
347
  it "converts object to toml with nesting values" do
@@ -349,7 +349,7 @@ RSpec.describe ChildMapping do
349
349
  actual = Lutaml::Model::Config.toml_adapter.parse(instance.to_toml)
350
350
  expected = Lutaml::Model::Config.toml_adapter.parse(prefixes_toml)
351
351
 
352
- expect(actual.attributes).to eq(expected.attributes)
352
+ expect(actual).to eq(expected)
353
353
  end
354
354
  end
355
355
  end
@@ -123,6 +123,20 @@ RSpec.shared_context "XML namespace models" do
123
123
  map_element "body", to: :body
124
124
  end
125
125
  end
126
+
127
+ class OwnedEnd < Lutaml::Model::Serializable
128
+ attribute :id, :string
129
+ attribute :type, :string
130
+ attribute :uml_type, :string
131
+
132
+ xml do
133
+ root "ownedEnd"
134
+
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
138
+ end
139
+ end
126
140
  end
127
141
 
128
142
  RSpec.shared_examples "XML serialization with namespace" do |model_class, xml_string|
@@ -302,6 +316,44 @@ RSpec.shared_examples "an XML namespace parser" do |adapter_class|
302
316
  end
303
317
  end
304
318
  end
319
+
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
329
+
330
+ describe "XML serialization" do
331
+ it "correctly deserializes from XML" do
332
+ owned_end = OwnedEnd.from_xml(xml_input)
333
+
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
338
+
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
+ )
345
+
346
+ expect(owned_end.to_xml).to be_equivalent_to(xml_input)
347
+ end
348
+
349
+ it "round-trips XML" do
350
+ owned_end = OwnedEnd.from_xml(xml_input)
351
+ output_xml = owned_end.to_xml
352
+
353
+ expect(output_xml).to be_equivalent_to(xml_input)
354
+ end
355
+ end
356
+ end
305
357
  end
306
358
 
307
359
  RSpec.describe Lutaml::Model::XmlAdapter::NokogiriAdapter do
@@ -131,6 +131,45 @@ module XmlMapping
131
131
  end
132
132
  end
133
133
 
134
+ class AnnotatedElement < Lutaml::Model::Serializable
135
+ attribute :idref, :string
136
+
137
+ xml do
138
+ root "annotatedElement"
139
+ map_attribute "idref", to: :idref, namespace: "http://www.omg.org/spec/XMI/20131001", prefix: "xmi"
140
+ end
141
+ end
142
+
143
+ class OwnedComment < Lutaml::Model::Serializable
144
+ attribute :annotated_attribute, :string
145
+ attribute :annotated_element, AnnotatedElement
146
+
147
+ xml do
148
+ root "ownedComment"
149
+ map_attribute "annotatedElement", to: :annotated_attribute
150
+ map_element "annotatedElement", to: :annotated_element, prefix: nil, namespace: nil
151
+ end
152
+ end
153
+
154
+ class Date < Lutaml::Model::Serializable
155
+ attribute :type, :string
156
+ attribute :text, :string
157
+ attribute :content, :string
158
+ attribute :from, :string
159
+ attribute :to, :string
160
+ attribute :on, :string
161
+
162
+ xml do
163
+ root "date", mixed: true
164
+ map_attribute "type", to: :type
165
+ map_attribute "text", to: :text
166
+ map_content to: :content
167
+ map_element "from", to: :from
168
+ map_element "to", to: :to
169
+ map_element "on", to: :on
170
+ end
171
+ end
172
+
134
173
  class OverrideDefaultNamespacePrefix < Lutaml::Model::Serializable
135
174
  attribute :same_element_name, SameNameDifferentNamespace
136
175
 
@@ -187,7 +226,7 @@ module XmlMapping
187
226
  xml do
188
227
  root "MapAllWithCustomMethod"
189
228
 
190
- map_all to: :all_content, with: { to: :content_to_xml, from: :content_from_xml }
229
+ map_all_content to: :all_content, with: { to: :content_to_xml, from: :content_from_xml }
191
230
  end
192
231
 
193
232
  def content_to_xml(model, parent, doc)
@@ -359,6 +398,69 @@ RSpec.describe Lutaml::Model::XmlMapping do
359
398
  end
360
399
  end
361
400
 
401
+ context "with same element and attribute name" do
402
+ let(:xml_with_element) do
403
+ <<~XML
404
+ <ownedComment xmlns:xmi="http://www.omg.org/spec/XMI/20131001">
405
+ <annotatedElement xmi:idref="ABC"/>
406
+ </ownedComment>
407
+ XML
408
+ end
409
+
410
+ let(:xml_with_attribute) do
411
+ <<~XML
412
+ <ownedComment annotatedElement="test2">
413
+ </ownedComment>
414
+ XML
415
+ end
416
+
417
+ let(:xml_with_same_name_attribute_and_element) do
418
+ <<~XML
419
+ <ownedComment xmlns:xmi="http://www.omg.org/spec/XMI/20131001" annotatedElement="test2">
420
+ <annotatedElement xmi:idref="ABC"/>
421
+ </ownedComment>
422
+ XML
423
+ end
424
+
425
+ let(:xml) do
426
+ <<~XML
427
+ <date type="published">
428
+ End of December
429
+ <on>2020-01</on>
430
+ Start of January
431
+ </date>
432
+ XML
433
+ end
434
+
435
+ it "parse and serializes the input xml correctly # lutaml/issues/217" do
436
+ parsed = XmlMapping::OwnedComment.from_xml(xml_with_element)
437
+ serialized = parsed.to_xml
438
+
439
+ expect(serialized).to be_equivalent_to(xml_with_element.strip)
440
+ end
441
+
442
+ it "parse and serialize model correctly" do
443
+ parsed = XmlMapping::OwnedComment.from_xml(xml_with_attribute)
444
+ serialized = parsed.to_xml
445
+
446
+ expect(serialized).to be_equivalent_to(xml_with_attribute)
447
+ end
448
+
449
+ it "parse and serialize model correctly with both attribute and element" do
450
+ parsed = XmlMapping::OwnedComment.from_xml(xml_with_same_name_attribute_and_element)
451
+ serialized = parsed.to_xml
452
+
453
+ expect(serialized).to be_equivalent_to(xml_with_same_name_attribute_and_element)
454
+ end
455
+
456
+ it "testing parse element" do
457
+ parsed = XmlMapping::Date.from_xml(xml)
458
+ serialized = parsed.to_xml
459
+
460
+ expect(serialized).to be_equivalent_to(xml)
461
+ end
462
+ end
463
+
362
464
  context "with same name elements" do
363
465
  let(:input_xml) do
364
466
  <<~XML
@@ -886,6 +988,11 @@ RSpec.describe Lutaml::Model::XmlMapping do
886
988
  # `to` is symbol which are constant so object_id will be same
887
989
  expect(orig_mapping.to).to eq(dup_mapping.to)
888
990
  end
991
+
992
+ it "duplicates attribute" do
993
+ # boolean value is constant so object_id will be same
994
+ expect(orig_mappings.attributes.first.attribute?).to eq(dup_mappings.attributes.first.attribute?)
995
+ end
889
996
  end
890
997
  end
891
998
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-11 00:00:00.000000000 Z
11
+ date: 2025-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -57,6 +57,7 @@ files:
57
57
  - lib/lutaml/model/comparable_nil.rb
58
58
  - lib/lutaml/model/comparison.rb
59
59
  - lib/lutaml/model/config.rb
60
+ - lib/lutaml/model/constants.rb
60
61
  - lib/lutaml/model/error.rb
61
62
  - lib/lutaml/model/error/collection_count_out_of_range_error.rb
62
63
  - lib/lutaml/model/error/collection_true_missing_error.rb
@@ -64,6 +65,7 @@ files:
64
65
  - lib/lutaml/model/error/invalid_value_error.rb
65
66
  - lib/lutaml/model/error/multiple_mappings_error.rb
66
67
  - lib/lutaml/model/error/pattern_not_matched_error.rb
68
+ - lib/lutaml/model/error/type/invalid_value_error.rb
67
69
  - lib/lutaml/model/error/type_error.rb
68
70
  - lib/lutaml/model/error/type_not_enabled_error.rb
69
71
  - lib/lutaml/model/error/unknown_adapter_type_error.rb
@@ -83,6 +85,8 @@ files:
83
85
  - lib/lutaml/model/schema/json_schema.rb
84
86
  - lib/lutaml/model/schema/json_schema_parser.rb
85
87
  - lib/lutaml/model/schema/relaxng_schema.rb
88
+ - lib/lutaml/model/schema/templates/simple_type.rb
89
+ - lib/lutaml/model/schema/xml_compiler.rb
86
90
  - lib/lutaml/model/schema/xsd_schema.rb
87
91
  - lib/lutaml/model/schema/yaml_schema.rb
88
92
  - lib/lutaml/model/schema_location.rb
@@ -134,9 +138,13 @@ files:
134
138
  - spec/fixtures/person.rb
135
139
  - spec/fixtures/sample_model.rb
136
140
  - spec/fixtures/vase.rb
141
+ - spec/fixtures/xml/invalid_math_document.xml
137
142
  - spec/fixtures/xml/latin_encoding.xml
143
+ - spec/fixtures/xml/math_document_schema.xsd
138
144
  - spec/fixtures/xml/shift_jis.xml
139
145
  - spec/fixtures/xml/special_char.xml
146
+ - spec/fixtures/xml/test_schema.xsd
147
+ - spec/fixtures/xml/valid_math_document.xml
140
148
  - spec/lutaml/model/attribute_spec.rb
141
149
  - spec/lutaml/model/cdata_spec.rb
142
150
  - spec/lutaml/model/collection_spec.rb
@@ -150,6 +158,7 @@ files:
150
158
  - spec/lutaml/model/inheritance_spec.rb
151
159
  - spec/lutaml/model/json_adapter_spec.rb
152
160
  - spec/lutaml/model/key_value_mapping_spec.rb
161
+ - spec/lutaml/model/map_all_spec.rb
153
162
  - spec/lutaml/model/map_content_spec.rb
154
163
  - spec/lutaml/model/mixed_content_spec.rb
155
164
  - spec/lutaml/model/multiple_mapping_spec.rb
@@ -159,6 +168,7 @@ files:
159
168
  - spec/lutaml/model/root_mappings_spec.rb
160
169
  - spec/lutaml/model/schema/json_schema_spec.rb
161
170
  - spec/lutaml/model/schema/relaxng_schema_spec.rb
171
+ - spec/lutaml/model/schema/xml_compiler_spec.rb
162
172
  - spec/lutaml/model/schema/xsd_schema_spec.rb
163
173
  - spec/lutaml/model/schema/yaml_schema_spec.rb
164
174
  - spec/lutaml/model/serializable_spec.rb