lutaml-model 0.8.188 → 0.8.189

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d74882885d43e5ff3acae984c6b3d26f33de2036d79effdfa55deb66f6d24d4
4
- data.tar.gz: 95d002c88f4a4a28de4cea44094321afcfdb0a38f787cf4e04f9c394b857da34
3
+ metadata.gz: 9ed52d827fd7fca7dafb9bd7c00a5d4adddee322cf878a8129a5131341273eb5
4
+ data.tar.gz: ef350764122a25d3edbfd1f4ffab1bef0ced5752889fddcd5d8521c637cba5e8
5
5
  SHA512:
6
- metadata.gz: 843dde8a0001e048af6777c7829c8d4022d0ff2537c42f69a14d9b63cf190aece0362f14d94ba072e74a744e8db786f5ec2e393b5623377f2d5983f5da058922
7
- data.tar.gz: 29cfad3f89b75b1288b17b5addc2c83cae737d6929e05ab9b903f106267d9afdd43fd1bda9ba8f106893d0f5e23d0b017da71e842d3e0c84c9034a9c5b97e79a
6
+ metadata.gz: 2a73d59d088991a2a54cf72294900b37c0132202add09d92a5ee7dc31c0c30448b201d7326ed1b36df37e4d4498f90bc5d1a29d167ea8b8841532ad8f3da8776
7
+ data.tar.gz: a0e142d241acf509858db6e988ffef80752fff2d9bb5458694a19d28339ee797d23275876b5e5005b9f58f57bb99c60148ebd84bfbb69c194d6cec6829c19ccc
@@ -6,6 +6,18 @@ module Lutaml
6
6
  module Hash
7
7
  class StandardAdapter < Document
8
8
  def self.parse(hsh, _options = {})
9
+ # from_hash's root contract: the document IS a hash. A
10
+ # non-hash previously crashed deep in the value extractor
11
+ # (a NoMethodError wrapped as InvalidFormatError); the
12
+ # #1228 guard made that path reachable by scalar values for
13
+ # mapped complex attributes, so the root validation lives
14
+ # here where it belongs.
15
+ unless hsh.respond_to?(:key?)
16
+ raise Lutaml::Model::InvalidFormatError.new(
17
+ :hash, "the document is not a Hash (got #{hsh.class})"
18
+ )
19
+ end
20
+
9
21
  hsh
10
22
  end
11
23
 
@@ -643,6 +643,18 @@ module Lutaml
643
643
  return resolved_type.new(value)
644
644
  end
645
645
 
646
+ # A plain scalar for a mapped complex attribute coerces onto the
647
+ # type's `content` mapping — the 0.8.x contract (#1228:
648
+ # `title: "Coll"` builds the localized-title with content
649
+ # "Coll"). On the derived/inline path a type mapping no content
650
+ # keeps the legacy verbatim behavior (the deserialization
651
+ # boundary raises instead — see cast_present).
652
+ if value.is_a?(::String) && !hash_type? &&
653
+ resolved_type.include?(Lutaml::Model::Serialize)
654
+ content_attr = scalar_content_attribute_for(resolved_type)
655
+ return resolved_type.new(content_attr => value) if content_attr
656
+ end
657
+
646
658
  # Special handling for Reference types - pass the metadata
647
659
  # (the comparison is attribute-constant; memoize the verdict)
648
660
  if reference_type?
@@ -665,6 +677,40 @@ module Lutaml
665
677
  type == Lutaml::Model::Type::Hash
666
678
  end
667
679
 
680
+ # The attribute a plain scalar coerces onto for a Serializable
681
+ # type (#1228): the whole-value `map to: :content` form (a
682
+ # content/root mapping whose sole target is a scalar attribute)
683
+ # or the keyed rule named "content".
684
+ def scalar_content_attribute_for(resolved_type)
685
+ %i[key_value yaml json hash].each do |format|
686
+ mapping = begin
687
+ resolved_type.mappings_for(format)
688
+ rescue StandardError
689
+ nil
690
+ end
691
+ next unless mapping
692
+
693
+ # Whole-value form: `map to: :content` (no wire key) — the
694
+ # collection-item coercion contract.
695
+ whole = Array(mapping.mappings).find do |rule|
696
+ rule.name.nil? && rule.to && !rule.delegate
697
+ end
698
+ if whole
699
+ target_attr = resolved_type.attributes[whole.to]
700
+ return whole.to if target_attr && !target_attr.collection?
701
+ end
702
+
703
+ # Keyed form: the rule named "content".
704
+ Array(mapping.mappings).each do |rule|
705
+ next unless rule.name == "content"
706
+
707
+ target_attr = resolved_type.attributes[rule.to]
708
+ return rule.to if target_attr && !target_attr.collection?
709
+ end
710
+ end
711
+ nil
712
+ end
713
+
668
714
  def setter
669
715
  :"#{@name}="
670
716
  end
@@ -1097,6 +1143,32 @@ instance_object = nil)
1097
1143
  "not build one (received #{casted.class})"
1098
1144
  end
1099
1145
 
1146
+ # The 0.8.x scalar-coercion contract (#1228): a plain string for
1147
+ # a mapped-model attribute whose type does not own a scalar
1148
+ # .cast coerces onto the type's `content` mapping (title:
1149
+ # "Coll" builds the localized title with content "Coll"). A
1150
+ # type mapping no scalar `content` cannot host the scalar —
1151
+ # that is a junk document and raises (the group-walk junk-item
1152
+ # contract).
1153
+ if serializable_type?(register) && value.is_a?(::String) &&
1154
+ !(klass.respond_to?(:cast) &&
1155
+ klass.method(:cast).owner == klass.singleton_class)
1156
+ content_attr = scalar_content_attribute_for(klass)
1157
+ unless content_attr
1158
+ raise Lutaml::Model::InvalidFormatError.new(
1159
+ format,
1160
+ "scalar value for '#{name}' whose type #{klass.name} " \
1161
+ "maps no `content` attribute",
1162
+ )
1163
+ end
1164
+
1165
+ propagated = Serialize::DeserializationContext.propagate(options)
1166
+ return klass.apply_mappings(
1167
+ { content_attr => value }, format,
1168
+ propagated.merge(register: register)
1169
+ )
1170
+ end
1171
+
1100
1172
  if can_serialize?(klass, value, format, options)
1101
1173
  propagated = Serialize::DeserializationContext.propagate(options)
1102
1174
  klass.apply_mappings(value, format,
@@ -49,7 +49,25 @@ instance_object, name)
49
49
 
50
50
  return doc if name.nil? || rule.root_mapping?
51
51
  return convert_to_format(doc, format) if rule.raw_mapping?
52
- return Utils.fetch_str_or_sym(doc, name) if Utils.string_or_symbol_key?(doc, name)
52
+ # The hash-key probe runs only on hash-like values (#1228): a
53
+ # plain string for a mapped complex attribute flows through to
54
+ # the type's own cast, it never keys a probe.
55
+ if doc.respond_to?(:key?) && Utils.string_or_symbol_key?(doc, name)
56
+ return Utils.fetch_str_or_sym(doc, name)
57
+ end
58
+
59
+ # A scalar document for a complex-typed attribute rides to the
60
+ # cast, which coerces it onto the type's content mapping
61
+ # (#1228) — the default-value path would drop it.
62
+ if attr && !doc.is_a?(::Hash)
63
+ resolved = begin
64
+ attr.type(register)
65
+ rescue StandardError
66
+ nil
67
+ end
68
+ return doc if resolved.is_a?(Class) &&
69
+ resolved.include?(Lutaml::Model::Serialize)
70
+ end
53
71
 
54
72
  if attr
55
73
  resolved = attr.default_value(register, instance_object)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.188"
5
+ VERSION = "0.8.189"
6
6
  end
7
7
  end
@@ -377,9 +377,31 @@ module Lutaml
377
377
  else
378
378
  plan[:descriptor]
379
379
  end
380
- compact = d.serialize(d.build_result(buf[base, len - base]))
381
- result = ::Leptris::XML.parse(compact).to_xml(indent: 2,
382
- no_decl: true)
380
+ compact = nil
381
+ begin
382
+ compact = d.serialize(d.build_result(buf[base, len - base]))
383
+ rescue ::Leptris::XML::Error => e
384
+ # The engine rejected the ops stream (status=-5: an
385
+ # ops plan_index the descriptor's row scoping refuses —
386
+ # the theme/styles class, leptris#1966; the deep
387
+ # ieee-idams shapes are the #1243 repro). The abort
388
+ # message already carries the shape (op count, plan
389
+ # count, max plan_index). Surface the fallback and
390
+ # bail to the DOM lane, whose output is byte-identical;
391
+ # correctness first, the ops/descriptor numbering for
392
+ # these shapes is engine-side work with the repro
393
+ # attached.
394
+ warn "[Lutaml::Model] leptris native emission aborted " \
395
+ "on #{instance.class.name}; falling back to the " \
396
+ "DOM lane (#{e.message})"
397
+ raise if ENV["LUTAML_NATIVE_EMISSION_STRICT"]
398
+
399
+ len = base
400
+ end
401
+ if compact
402
+ result = ::Leptris::XML.parse(compact).to_xml(indent: 2,
403
+ no_decl: true)
404
+ end
383
405
  end
384
406
  ensure
385
407
  buf.slice!(len..) if buf.size > len
@@ -2,12 +2,12 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- # #978: a JSON scalar handed to a mapped-model attribute crashed with
6
- # an internal NoMethodError (key? on String) wrapped as
7
- # InvalidFormatError. A type that declares its own .cast owns scalar
8
- # construction from that scalar; a cast-less type falls through to the
9
- # legacy conversion chain, whose parse error surfaces the offending
10
- # token — the remedy there is declaring .cast on the type.
5
+ # lutaml-model#978 + #1228: a JSON scalar handed to a mapped-model
6
+ # attribute crashed with an internal NoMethodError (key? on String)
7
+ # wrapped as InvalidFormatError. A type that declares its own .cast
8
+ # owns scalar construction from that scalar; a cast-less type with a
9
+ # scalar `content` rule coerces onto it (#1228); a type without one
10
+ # keeps the format parse error — or declares .cast to own it.
11
11
  RSpec.describe "from_json scalar into a mapped-model attribute" do
12
12
  let(:marked_class) do
13
13
  Class.new(Lutaml::Model::Serializable) do
@@ -60,11 +60,39 @@ RSpec.describe "from_json scalar into a mapped-model attribute" do
60
60
  expect(model.description.content).to eq("text")
61
61
  end
62
62
 
63
- it "falls through to the format parse error for a cast-less mapped type" do
64
- expect { ScalarHolder.from_json('{"plain_desc": "text"}') }
65
- .to raise_error(
66
- Lutaml::Model::InvalidFormatError,
67
- /unexpected token 'text'/,
68
- )
63
+ # #1228 reconciled with #978: a cast-LESS type whose key_value
64
+ # mapping carries a scalar `content` rule coerces the scalar onto
65
+ # that rule (title: "text" builds the model with content "text") —
66
+ # the 0.8.x contract Relaton's localized titles ride. The .cast
67
+ # ownership contract above still takes precedence for types that
68
+ # declare their own scalar construction.
69
+ it "coerces the scalar onto the content mapping for a cast-less mapped type" do
70
+ model = ScalarHolder.from_json('{"plain_desc": "text"}')
71
+
72
+ expect(model.plain_desc).to be_a(ScalarPlain)
73
+ expect(model.plain_desc.content).to eq("text")
74
+ end
75
+
76
+ it "raises the format error for a scalar on a mapped type without a content rule" do
77
+ shape_class = Class.new(Lutaml::Model::Serializable) do
78
+ attribute :label, :string
79
+
80
+ key_value do
81
+ map "label", to: :label
82
+ end
83
+ end
84
+ stub_const("ScalarShapeless", shape_class)
85
+
86
+ holder = Class.new(Lutaml::Model::Serializable) do
87
+ attribute :shape, "ScalarShapeless"
88
+
89
+ key_value do
90
+ map "shape", to: :shape
91
+ end
92
+ end
93
+ stub_const("ScalarShapelessHolder", holder)
94
+
95
+ expect { ScalarShapelessHolder.from_json('{"shape": "text"}') }
96
+ .to raise_error(Lutaml::Model::InvalidFormatError, /maps no `content`/)
69
97
  end
70
98
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.188
4
+ version: 0.8.189
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.