lutaml-model 0.3.25 → 0.3.27

Sign up to get free protection for your applications and to get access to all the features.
@@ -170,6 +170,15 @@ module MixedContentSpec
170
170
  map_element :value, to: :value
171
171
  end
172
172
  end
173
+
174
+ class HexCode < Lutaml::Model::Serializable
175
+ attribute :content, :string
176
+
177
+ xml do
178
+ root "HexCode"
179
+ map_content to: :content
180
+ end
181
+ end
173
182
  end
174
183
 
175
184
  RSpec.describe "MixedContent" do
@@ -449,7 +458,7 @@ RSpec.describe "MixedContent" do
449
458
 
450
459
  it "serializes special char mixed content correctly" do
451
460
  parsed = MixedContentSpec::SpecialCharContentWithMixedTrue.from_xml(xml)
452
- serialized = parsed.to_xml
461
+ serialized = parsed.to_xml(encoding: "UTF-8")
453
462
 
454
463
  expect(serialized).to include(expected_xml)
455
464
  end
@@ -608,10 +617,68 @@ RSpec.describe "MixedContent" do
608
617
  end
609
618
  end
610
619
  end
620
+
621
+ context "when special char used as full entities, it persist as entities if no encoding provided" do
622
+ let(:xml) do
623
+ <<~XML
624
+ <HexCode>
625
+ &#x2211;computer security&#x220F; type of &#x200B; operation specified &#xB5; by an access right
626
+ </HexCode>
627
+ XML
628
+ end
629
+
630
+ describe ".from_xml" do
631
+ let(:expected_content) { "∑computer security∏ type of ​ operation specified µ by an access right" }
632
+
633
+ it "deserializes special char mixed content correctly" do
634
+ parsed = MixedContentSpec::HexCode.from_xml(xml)
635
+
636
+ expect(parsed.content.strip).to eq(expected_content)
637
+ end
638
+ end
639
+
640
+ describe ".to_xml" do
641
+ context "when default encoding xml" do
642
+ let(:expected_default_encoding_xml) { "∑computer security∏ type of ​ operation specified µ by an access right" }
643
+
644
+ it "serializes special char mixed content correctly with default encoding: UTF-8" do
645
+ parsed = MixedContentSpec::HexCode.from_xml(xml)
646
+ serialized = parsed.to_xml
647
+
648
+ expect(serialized.strip).to include(expected_default_encoding_xml)
649
+ end
650
+ end
651
+
652
+ context "when encoding: nil xml" do
653
+ let(:expected_encoding_nil_nokogiri_xml) { "&#x2211;computer security&#x220F; type of &#x200B; operation specified &#xB5; by an access right" }
654
+ let(:expected_encoding_nil_ox_xml) { "\xE2\x88\x91computer security\xE2\x88\x8F type of \xE2\x80\x8B operation specified \xC2\xB5 by an access right" }
655
+
656
+ it "serializes special char mixed content correctly with encoding: nil to get hexcode" do
657
+ parsed = MixedContentSpec::HexCode.from_xml(xml)
658
+ serialized = parsed.to_xml(encoding: nil)
659
+
660
+ if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
661
+ expected_output = expected_encoding_nil_ox_xml
662
+ expected_output.force_encoding("ASCII-8BIT")
663
+ else
664
+ expected_output = expected_encoding_nil_nokogiri_xml
665
+ end
666
+
667
+ expect(serialized.strip).to include(expected_output)
668
+ end
669
+ end
670
+ end
671
+ end
611
672
  end
612
673
 
613
674
  describe Lutaml::Model::XmlAdapter::NokogiriAdapter do
614
675
  it_behaves_like "mixed content behavior", described_class
676
+
677
+ it "raises error when serializes special char content with false encoding: 'ABC'" do
678
+ parsed = MixedContentSpec::HexCode.from_xml("<HexCode>&#x2211;computer security</HexCode>")
679
+
680
+ expect { parsed.to_xml(encoding: "ABC") }.to raise_error(StandardError, "unknown encoding name - ABC")
681
+ end
615
682
  end
616
683
 
617
684
  describe Lutaml::Model::XmlAdapter::OxAdapter do
@@ -2,6 +2,7 @@ require "spec_helper"
2
2
 
3
3
  class TestSerializable < Lutaml::Model::Serializable
4
4
  attribute :name, :string, values: ["Alice", "Bob", "Charlie"]
5
+ attribute :email, :string, pattern: /.*?\S+@.+\.\S+/
5
6
  attribute :age, :integer, collection: 1..3
6
7
 
7
8
  xml do
@@ -27,9 +28,12 @@ class TestSerializable < Lutaml::Model::Serializable
27
28
  end
28
29
 
29
30
  RSpec.describe Lutaml::Model::Serializable do
30
- let(:valid_instance) { TestSerializable.new(name: "Alice", age: [30]) }
31
+ let(:valid_instance) do
32
+ TestSerializable.new(name: "Alice", age: [30], email: "alice@gmail.com")
33
+ end
34
+
31
35
  let(:invalid_instance) do
32
- TestSerializable.new(name: "David", age: [25, 30, 35, 40])
36
+ TestSerializable.new(name: "David", age: [25, 30, 35, 40], email: "david@gmail")
33
37
  end
34
38
 
35
39
  describe "serialization methods" do
@@ -66,8 +70,9 @@ RSpec.describe Lutaml::Model::Serializable do
66
70
  it "returns errors for invalid attributes" do
67
71
  errors = invalid_instance.validate
68
72
  expect(errors).not_to be_empty
69
- expect(errors.first).to be_a(Lutaml::Model::InvalidValueError)
70
- expect(errors.last).to be_a(Lutaml::Model::CollectionCountOutOfRangeError)
73
+ expect(errors[0]).to be_a(Lutaml::Model::InvalidValueError)
74
+ expect(errors[1]).to be_a(Lutaml::Model::PatternNotMatchedError)
75
+ expect(errors[2]).to be_a(Lutaml::Model::CollectionCountOutOfRangeError)
71
76
  end
72
77
  end
73
78
 
@@ -127,6 +127,17 @@ module XmlMapping
127
127
  end
128
128
  end
129
129
 
130
+ class OverrideDefaultNamespacePrefix < Lutaml::Model::Serializable
131
+ attribute :same_element_name, SameNameDifferentNamespace
132
+
133
+ xml do
134
+ root "OverrideDefaultNamespacePrefix"
135
+ map_element :SameElementName, to: :same_element_name,
136
+ namespace: "http://www.omg.org/spec/XMI/20131001",
137
+ prefix: "abc"
138
+ end
139
+ end
140
+
130
141
  class SchemaLocationOrdered < Lutaml::Model::Serializable
131
142
  attribute :content, :string
132
143
  attribute :second, SchemaLocationOrdered
@@ -220,6 +231,30 @@ module XmlMapping
220
231
  prefix: nil
221
232
  end
222
233
  end
234
+
235
+ class Documentation < Lutaml::Model::Serializable
236
+ attribute :content, :string
237
+
238
+ xml do
239
+ root "documentation", mixed: true
240
+ namespace "http://www.w3.org/2001/XMLSchema", "xsd"
241
+
242
+ map_content to: :content
243
+ end
244
+ end
245
+
246
+ class Schema < Lutaml::Model::Serializable
247
+ attribute :documentation, Documentation, collection: true
248
+
249
+ xml do
250
+ root "schema"
251
+ namespace "http://www.w3.org/2001/XMLSchema", "xsd"
252
+
253
+ map_element :documentation, to: :documentation,
254
+ namespace: "http://www.w3.org/2001/XMLSchema",
255
+ prefix: "xsd"
256
+ end
257
+ end
223
258
  end
224
259
 
225
260
  RSpec.describe Lutaml::Model::XmlMapping do
@@ -245,6 +280,25 @@ RSpec.describe Lutaml::Model::XmlMapping do
245
280
  end
246
281
  end
247
282
 
283
+ context "overriding child namespace prefix" do
284
+ let(:input_xml) do
285
+ <<~XML
286
+ <OverrideDefaultNamespacePrefix xmlns:abc="http://www.omg.org/spec/XMI/20131001" xmlns:GML="http://www.sparxsystems.com/profiles/GML/1.0" xmlns:CityGML="http://www.sparxsystems.com/profiles/CityGML/1.0">
287
+ <abc:SameElementName App="hello">
288
+ <GML:ApplicationSchema>GML App</GML:ApplicationSchema>
289
+ <CityGML:ApplicationSchema>CityGML App</CityGML:ApplicationSchema>
290
+ <abc:ApplicationSchema>App</abc:ApplicationSchema>
291
+ </abc:SameElementName>
292
+ </OverrideDefaultNamespacePrefix>
293
+ XML
294
+ end
295
+
296
+ it "expect to round-trips" do
297
+ parsed = XmlMapping::OverrideDefaultNamespacePrefix.from_xml(input_xml)
298
+ expect(parsed.to_xml).to be_equivalent_to(input_xml)
299
+ end
300
+ end
301
+
248
302
  context "with same name elements" do
249
303
  let(:input_xml) do
250
304
  <<~XML
@@ -859,5 +913,19 @@ RSpec.describe Lutaml::Model::XmlMapping do
859
913
  expect(XmlMapping::SpecialCharContentWithMapAll.from_xml(xml).to_xml).to eq(expected_xml)
860
914
  end
861
915
  end
916
+
917
+ context "when mixed content is true and child is content_mapping" do
918
+ let(:xml) do
919
+ <<~XML
920
+ <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
921
+ <xsd:documentation>asdf</xsd:documentation>
922
+ </xsd:schema>
923
+ XML
924
+ end
925
+
926
+ it "round-trips xml" do
927
+ expect(XmlMapping::Schema.from_xml(xml).to_xml).to be_equivalent_to(xml)
928
+ end
929
+ end
862
930
  end
863
931
  end
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.3.25
4
+ version: 0.3.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-12 00:00:00.000000000 Z
11
+ date: 2024-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -59,6 +59,7 @@ files:
59
59
  - lib/lutaml/model/error/collection_count_out_of_range_error.rb
60
60
  - lib/lutaml/model/error/incorrect_mapping_argument_error.rb
61
61
  - lib/lutaml/model/error/invalid_value_error.rb
62
+ - lib/lutaml/model/error/pattern_not_matched_error.rb
62
63
  - lib/lutaml/model/error/type_error.rb
63
64
  - lib/lutaml/model/error/type_not_enabled_error.rb
64
65
  - lib/lutaml/model/error/unknown_adapter_type_error.rb
@@ -127,6 +128,7 @@ files:
127
128
  - spec/fixtures/vase.rb
128
129
  - spec/fixtures/xml/special_char.xml
129
130
  - spec/lutaml/model/attribute_spec.rb
131
+ - spec/lutaml/model/cdata_spec.rb
130
132
  - spec/lutaml/model/collection_spec.rb
131
133
  - spec/lutaml/model/comparable_model_spec.rb
132
134
  - spec/lutaml/model/custom_model_spec.rb