lutaml-model 0.8.157 → 0.8.158

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: 5a7a7b4352ec266e959f6a4304acafba60a4433953c36fdd3c4cfc2d7bafeccf
4
- data.tar.gz: 827c5280c74a9d9c6a517adc00779444ba0c305fbecab9ab8ccc491a2f09ddc0
3
+ metadata.gz: 74c783e3897939ae61d1cbd03782641f0ed8facb94ed8098588a7491504db44c
4
+ data.tar.gz: c30df2f8628c15e232559c658ca820e1d4597b976a87d98936703c64bf99d5c9
5
5
  SHA512:
6
- metadata.gz: bc20f062d7a51cd5c9bcaf5e7df729ba80551e161a146611773d5e8697650fa9dad7f83424c93ef95e0b75fe4db118b844ef2661891b6499c896c226f29c3d0e
7
- data.tar.gz: 1874b8520c7cca77ffaac37e985f69d80fcb242d8c66b53e10c94cf4f8c12eee9930650e6f36cb03b93ce3f5e3c15115136deebb0865c438f79884bc24f881c8
6
+ metadata.gz: 2c48df71e7b19c791954371707b6ff671f2fd234f8de0be7ef4c5dbce1838b7ab349b669124793c15037f6bdd788a9260924e195cfc1d34ca779ecc9f07471ab
7
+ data.tar.gz: f4069257888fb2b6c9fa5e9bd8913fd7bb2b0a369c5d65336f154902e96b5d5e3295c182adef140e78a0cb3ee008d8a8dceaefa0563fee71ee16a70bf3309c1d
@@ -1,3 +1,8 @@
1
+ # DEPRECATED: the Opal lane is on the deprecation path (the moxml-ts
2
+ # wasm binding is the JavaScript endgame). The lane stays green — the
3
+ # boot repair chain lives in lib/compat/opal — but it is
4
+ # maintenance-only and non-gating; removal is planned for a future
5
+ # release.
1
6
  name: opal
2
7
 
3
8
  on:
@@ -11,6 +11,17 @@ nav_order: 99
11
11
 
12
12
  == Overview
13
13
 
14
+ [IMPORTANT]
15
+ ====
16
+ Opal support in lutaml-model is **deprecated**. The Opal boot lane, its
17
+ compat shims, and the vendored Opal forks remain functional in the 0.8
18
+ line but receive maintenance-only attention, and removal is planned for
19
+ a future release. New JavaScript-targeted work is directed at the
20
+ moxml-ts wasm binding instead.
21
+ ====
22
+
23
+
24
+
14
25
  https://opalrb.com[Opal] is a Ruby-to-JavaScript compiler that allows Ruby code to run in the browser. Lutaml::Model supports running under Opal, enabling XML/JSON/YAML serialization in client-side applications.
15
26
 
16
27
  The XML parsing layer is provided by the https://github.com/lutaml/moxml[moxml] gem (v0.2+), which detects the Opal runtime and uses the REXML adapter automatically.
@@ -190,11 +190,19 @@ Requires native extensions (i.e. compiled C code).
190
190
  Requires the `nokogiri` gem.
191
191
 
192
192
  Oga::
193
- (optional)
193
+ (optional, *deprecated*)
194
194
  Pure Ruby XML parser.
195
195
  Does not require native extensions.
196
196
  Requires the `oga` gem.
197
197
 
198
+ [NOTE]
199
+ ====
200
+ The Oga engine is deprecated: moxml's default engine (Nokogiri, or the
201
+ leptris native engine when present) is the supported XML lane.
202
+ Selecting Oga emits a deprecation warning; removal is planned for a
203
+ future release.
204
+ ====
205
+
198
206
  Ox::
199
207
  (optional)
200
208
  Fast XML parser and object serializer for Ruby, implemented partially in C.
@@ -202,13 +210,21 @@ Requires native extensions (i.e. compiled C code).
202
210
  Requires the `ox` gem.
203
211
 
204
212
  REXML::
205
- (optional)
213
+ (optional, *deprecated*)
206
214
  Pure Ruby XML parser, bundled as a default gem with Ruby.
207
215
  Moved from standard library to a default gem in Ruby 3.0.
208
216
  Opal-compatible: Opal reimplements `strscan` and `stringio` in its stdlib,
209
217
  enabling REXML to compile cleanly to JavaScript.
210
218
  Requires the `rexml` gem (bundled with Ruby by default).
211
219
 
220
+ [NOTE]
221
+ ====
222
+ The REXML engine is deprecated: moxml's default engine (Nokogiri, or
223
+ the leptris native engine when present) is the supported XML lane.
224
+ Selecting REXML emits a deprecation warning; removal is planned for a
225
+ future release.
226
+ ====
227
+
212
228
 
213
229
  .Using an XML adapter
214
230
  [source,ruby]
@@ -29,6 +29,21 @@ module Lutaml
29
29
  #
30
30
  class AdapterResolver
31
31
  class << self
32
+ # Adapters on the deprecation path. moxml is the only supported
33
+ # XML facade going forward, and its default engine (Nokogiri,
34
+ # with the leptris native engine preferred when present) is the
35
+ # supported lane; the pure-Ruby REXML and Oga engines remain
36
+ # loadable but select for the last time in the 0.8 line.
37
+ DEPRECATED_ADAPTERS = {
38
+ %i[xml rexml] => "the REXML engine is deprecated; moxml's " \
39
+ "default engine (Nokogiri, or the leptris " \
40
+ "native engine when present) is the supported " \
41
+ "XML lane",
42
+ %i[xml oga] => "the Oga engine is deprecated; moxml's default " \
43
+ "engine (Nokogiri, or the leptris native engine " \
44
+ "when present) is the supported XML lane",
45
+ }.freeze
46
+
32
47
  # --- Resolution ---
33
48
 
34
49
  # Resolve the adapter class for a format using the full resolution chain.
@@ -215,12 +230,26 @@ module Lutaml
215
230
  adapter = format.to_s
216
231
  type = normalize_type_name(type_name, format)
217
232
 
233
+ warn_deprecated_adapter(format, type_name)
218
234
  load_adapter_file(adapter, type)
219
235
  load_moxml_adapter(type_name, format)
220
236
 
221
237
  class_for(adapter, type)
222
238
  end
223
239
 
240
+ # One deprecation warning per (format, adapter) per process.
241
+ def warn_deprecated_adapter(format, type_name)
242
+ message = DEPRECATED_ADAPTERS[[format, type_name.to_sym]]
243
+ return if message.nil? || warned_deprecated.key?([format, type_name])
244
+
245
+ warned_deprecated[[format, type_name]] = true
246
+ warn "[Lutaml::Model] DEPRECATED: #{message}."
247
+ end
248
+
249
+ def warned_deprecated
250
+ @warned_deprecated ||= {}
251
+ end
252
+
224
253
  # Normalize type name to file/class name pattern.
225
254
  #
226
255
  # @param type_name [Symbol] raw type name
@@ -29,7 +29,9 @@ module Lutaml
29
29
 
30
30
  def clear_context(context_id)
31
31
  @store.each_key do |key|
32
- @store.delete(key) if key[0] == context_id
32
+ # Non-Array keys are the bare, nil-context form (see
33
+ # CachedTypeResolver#type_cache_key).
34
+ @store.delete(key) if (key.is_a?(Array) ? key[0] : nil) == context_id
33
35
  end
34
36
  end
35
37
 
@@ -34,7 +34,9 @@ module Lutaml
34
34
 
35
35
  def clear_context(context_id)
36
36
  @mutex.synchronize do
37
- @store.delete_if { |key, _| key[0] == context_id }
37
+ # Non-Array keys are the bare, nil-context form (see
38
+ # CachedTypeResolver#type_cache_key).
39
+ @store.delete_if { |key, _| (key.is_a?(Array) ? key[0] : nil) == context_id }
38
40
  end
39
41
  end
40
42
 
@@ -65,13 +65,7 @@ module Lutaml
65
65
  def resolve(name, context, materialize: true)
66
66
  return @delegate.resolve(name, context, materialize: false) unless materialize
67
67
 
68
- cache_key = if name.is_a?(Class)
69
- [context_id_for(context), name]
70
- else
71
- build_cache_key(name, context)
72
- end
73
-
74
- @cache_backend.fetch_or_store(cache_key) do
68
+ @cache_backend.fetch_or_store(type_cache_key(name, context)) do
75
69
  @delegate.resolve(name, context)
76
70
  end
77
71
  end
@@ -82,13 +76,7 @@ module Lutaml
82
76
  # @param context [TypeContext, nil] The resolution context
83
77
  # @return [Boolean] true if type can be resolved
84
78
  def resolvable?(name, context)
85
- cache_key = if name.is_a?(Class)
86
- [context_id_for(context), name]
87
- else
88
- build_cache_key(name, context)
89
- end
90
-
91
- return true if @cache_backend.key?(cache_key)
79
+ return true if @cache_backend.key?(type_cache_key(name, context))
92
80
 
93
81
  @delegate.resolvable?(name, context)
94
82
  end
@@ -133,14 +121,17 @@ module Lutaml
133
121
 
134
122
  private
135
123
 
136
- # Build a cache key from name and context.
137
- # Uses array for faster comparison than string concatenation.
138
- #
139
- # @param name [Symbol, String] The type name
140
- # @param context [TypeContext, nil] The resolution context
141
- # @return [Array] The cache key (faster than string)
142
- def build_cache_key(name, context)
143
- [context_id_for(context), name.to_sym]
124
+ # Composite cache key: `[context_id, name]` when a context is present,
125
+ # otherwise the bare name (Symbol or Class). The bare form avoids one
126
+ # Array allocation per resolution in the common single-register run;
127
+ # it cannot collide with composite keys since Arrays and
128
+ # Symbols/Classes never match as Hash keys. Keys belonging to the nil
129
+ # context are therefore the non-Array ones (see the backends'
130
+ # #clear_context).
131
+ def type_cache_key(name, context)
132
+ bare = name.is_a?(Class) ? name : name.to_sym
133
+ cid = context_id_for(context)
134
+ cid ? [cid, bare] : bare
144
135
  end
145
136
 
146
137
  # A nil context (e.g. a register ID that has no registered type
@@ -75,6 +75,12 @@ module Lutaml
75
75
  end
76
76
  end
77
77
 
78
+ # Frozen per-subclass ivar name for the `collection` accessors —
79
+ # see the instance-method memo below.
80
+ def collection_ivar_name
81
+ @collection_ivar_name ||= :"@#{instance_name}"
82
+ end
83
+
78
84
  attr_reader :instance_type,
79
85
  :instance_name,
80
86
  :sort_by_field,
@@ -325,7 +331,11 @@ module Lutaml
325
331
  mappings = mappings_for(format)
326
332
  data = super
327
333
 
328
- if !collection_structured_format?(format) && mappings.no_root? && !mappings.root_mapping
334
+ # Scalar mappings make the document hash-shaped (#1086): the
335
+ # unwrap below would strip it back to a bare items array and
336
+ # drop the scalar keys.
337
+ if !collection_structured_format?(format) && mappings.no_root? &&
338
+ !mappings.root_mapping && !collection_has_scalar_mappings?(format)
329
339
  unwrap_unwrapped_data(data)
330
340
  else
331
341
  data
@@ -339,19 +349,50 @@ module Lutaml
339
349
  data = wrap_unwrapped_input(format, mappings, data)
340
350
  end
341
351
 
352
+ # Hash-shaped documents (scalar mappings declared, #1086) walk
353
+ # the rules like any model: the collection flag would treat the
354
+ # document as the bare items array and lose the scalar keys.
355
+ if !collection_structured_format?(format) &&
356
+ collection_has_scalar_mappings?(format) && data.is_a?(::Hash)
357
+ return super(format, data, options.merge(from_collection: true))
358
+ end
359
+
342
360
  super(format, data, options.merge(from_collection: true))
343
361
  end
344
362
 
345
363
  def of(format, data, options = {})
346
364
  mappings = mappings_for(format)
347
365
 
348
- if !collection_structured_format?(format) && mappings.no_root? && !mappings.root_mapping
366
+ # Hash-shaped documents carrying a declared scalar key (#1086)
367
+ # are already keyed; the no-root wrap would double-nest them.
368
+ # Bare arrays (the legacy items document), keyed-element
369
+ # documents (child_mappings walks the hash keys), and scalar-less
370
+ # collections all keep the historical wrap.
371
+ if !collection_structured_format?(format) && mappings.no_root? &&
372
+ !mappings.root_mapping && data.is_a?(::Array)
373
+ data = { mappings.find_by_to!(instance_name).name => data }
374
+ elsif !collection_structured_format?(format) && data.is_a?(::Hash) &&
375
+ collection_has_scalar_mappings?(format) &&
376
+ !(data.keys & collection_scalar_mapping_names(format)).empty?
377
+ # keep the keyed document as-is
378
+ elsif !collection_structured_format?(format) && mappings.no_root? &&
379
+ !mappings.root_mapping
349
380
  data = { mappings.find_by_to!(instance_name).name => data }
350
381
  end
351
382
 
352
383
  super(format, data, options.merge(from_collection: true))
353
384
  end
354
385
 
386
+ # Names of the collection's scalar (non-instance) key-value
387
+ # mappings (#1086).
388
+ def collection_scalar_mapping_names(format)
389
+ instance = instance_name
390
+ mappings_for(format).mappings
391
+ .reject { |rule| rule.to == instance }
392
+ .flat_map { |rule| Array(rule.name) }
393
+ .compact
394
+ end
395
+
355
396
  # Hook: returns true for formats that use structured (tree-based) serialization
356
397
  # like XML. Key-value formats (JSON, YAML, TOML) return false (default).
357
398
  # XML overrides to return true.
@@ -431,7 +472,11 @@ module Lutaml
431
472
 
432
473
  attr_reader :lutaml_register
433
474
 
434
- def initialize(items = [],
475
+ # Shared frozen empty array for the constructor's default argument;
476
+ # `items` is only read in initialize, never mutated.
477
+ EMPTY_ITEMS = [].freeze
478
+
479
+ def initialize(items = EMPTY_ITEMS,
435
480
  lutaml_register: Lutaml::Model::Config.default_register)
436
481
  super()
437
482
 
@@ -460,12 +505,16 @@ lutaml_register: Lutaml::Model::Config.default_register)
460
505
  super(format, options.merge(collection: true))
461
506
  end
462
507
 
508
+ # The ivar name is static per subclass (set by `instances`); the
509
+ # dynamic :"@#{...}" symbol minted per access was 11% of a
510
+ # YAML-materialization profile's allocations (56k per 60 docs on
511
+ # the glossarist corpus). The name is memoized on the class.
463
512
  def collection
464
- instance_variable_get(:"@#{self.class.instance_name}")
513
+ instance_variable_get(self.class.collection_ivar_name)
465
514
  end
466
515
 
467
516
  def collection=(collection)
468
- instance_variable_set(:"@#{self.class.instance_name}", collection)
517
+ instance_variable_set(self.class.collection_ivar_name, collection)
469
518
  sort_items!
470
519
  build_index_caches!
471
520
  end
@@ -566,18 +566,42 @@ context = nil, pre_cast: false)
566
566
  end
567
567
  end
568
568
 
569
+ # Pooled invokers for symbol/string custom methods, keyed by
570
+ # [mapper class, method]. The receiver is stateless in the #550
571
+ # contract — it receives the model and value as arguments — but a
572
+ # fresh mapper_class.new per rule application ran the full
573
+ # Serializable constructor chain on every call (33% of a YAML
574
+ # materialization profile on the glossarist corpus). One receiver
575
+ # per (class, method) is pooled, with the arity probe resolved
576
+ # once alongside it. A custom method that stashes instance state
577
+ # on the receiver would observe cross-call persistence — today's
578
+ # fresh receiver discarded that state between calls, so such
579
+ # methods were already broken call-over-call.
580
+ def custom_method_invokers
581
+ @custom_method_invokers ||= {}
582
+ end
583
+
584
+ def custom_method_invoker(mapper_class, custom)
585
+ custom_method_invokers[[mapper_class, custom]] ||= begin
586
+ receiver = mapper_class.new
587
+ { receiver: receiver,
588
+ context_arity:
589
+ self.class.custom_method_arity(receiver.class, custom).to_i >= 3 }
590
+ end
591
+ end
592
+
569
593
  def handle_custom_method(model, value, mapper_class, context = nil)
570
594
  custom = custom_methods[:from]
571
595
  return if !custom || value.nil?
572
596
 
573
597
  if custom.is_a?(String) || custom.is_a?(Symbol)
574
- target = mapper_class.new
598
+ invoker = custom_method_invoker(mapper_class, custom)
575
599
  # lutaml-model#550: custom methods may declare a third context
576
600
  # parameter to receive the options passed to `from_*`.
577
- if self.class.custom_method_arity(target.class, custom).to_i >= 3
578
- target.public_send(custom, model, value, context)
601
+ if invoker[:context_arity]
602
+ invoker[:receiver].public_send(custom, model, value, context)
579
603
  else
580
- target.public_send(custom, model, value)
604
+ invoker[:receiver].public_send(custom, model, value)
581
605
  end
582
606
  else
583
607
  # Callable (proc/lambda): transforms the value and the rule
@@ -33,7 +33,7 @@ module Lutaml
33
33
  # @param attrs [Hash] Attribute values (ignored when block given)
34
34
  # @param options [Hash] Options (ignored when block given)
35
35
  # @param block [Proc, nil] Optional builder block
36
- def initialize(attrs = {}, options = {}, &block)
36
+ def initialize(attrs = EMPTY_HASH, options = EMPTY_HASH, &block)
37
37
  # Call parent initialize
38
38
  super(attrs, options)
39
39
 
@@ -433,7 +433,18 @@ module Lutaml
433
433
  resolve_serialization_mapping_option(format, options)
434
434
 
435
435
  if instance.is_a?(Array)
436
- return instance.map { |item| public_send(:"as_#{format}", item) }
436
+ # Collection subclasses that declare scalar key-value mappings
437
+ # serialize hash-shaped (#1086): their rules include
438
+ # non-instance keys, so the per-item map would silently drop
439
+ # them. Plain Arrays and legacy collections (instances-only
440
+ # mappings) keep the per-item map.
441
+ if instance.is_a?(Lutaml::Model::Collection) &&
442
+ !collection_structured_format?(format) &&
443
+ collection_has_scalar_mappings?(format)
444
+ # fall through to the rule walk below
445
+ else
446
+ return instance.map { |item| public_send(:"as_#{format}", item) }
447
+ end
437
448
  end
438
449
 
439
450
  unless instance.is_a?(model)
@@ -454,6 +465,20 @@ module Lutaml
454
465
  transformer.model_to_data(self, instance, format, options)
455
466
  end
456
467
 
468
+ # Whether the bound Collection class declares key-value mappings
469
+ # beyond its instance collection rule (#1086). Such mappings make
470
+ # the document hash-shaped (instance key plus scalar keys) instead
471
+ # of the legacy bare items array.
472
+ def collection_has_scalar_mappings?(format)
473
+ return false unless model < Lutaml::Model::Collection
474
+
475
+ mappings = model.mappings_for(format)
476
+ instance_name = model.instance_name
477
+ mappings.mappings.any? { |rule| rule.to != instance_name }
478
+ rescue StandardError
479
+ false
480
+ end
481
+
457
482
  # Hook for format-specific pre-serialization logic.
458
483
  # XML overrides to resolve XML mapping imports.
459
484
  #
@@ -43,6 +43,11 @@ module Lutaml
43
43
  # getters default to it instead of a `*args` splat.
44
44
  NO_ARG = Object.new.freeze
45
45
 
46
+ # Shared frozen empty hash for constructor default arguments. The init
47
+ # walk only reads attrs/options, so one frozen instance replaces the
48
+ # per-instantiation `{}` defaults (68k+ allocations per corpus parse).
49
+ EMPTY_HASH = {}.freeze
50
+
46
51
  INTERNAL_ATTRIBUTES = %i[@using_default @lutaml_register @lutaml_parent @lutaml_root
47
52
  @register_records].freeze
48
53
 
@@ -162,7 +167,7 @@ module Lutaml
162
167
  # re-materialize defaults that the post-run reset discards.
163
168
  attr_accessor :lutaml_constructor_seeded
164
169
 
165
- def initialize(attrs = {}, options = {})
170
+ def initialize(attrs = EMPTY_HASH, options = EMPTY_HASH)
166
171
  if lutaml_constructor_seeded
167
172
  # The deserialization constructor seeded constructor-equivalent
168
173
  # compiled state (defaults resolved, non-defaulted nil) and
@@ -13,39 +13,28 @@ module Lutaml
13
13
  Concurrent::Map.new
14
14
  end
15
15
 
16
- def initialize(rule, doc, format, attr, register, options,
16
+ # The extractor is pure stateless logic over its arguments; the
17
+ # per-call `new(*args).call` instance (Services::Base's default)
18
+ # allocated an object plus seven ivar writes per rule per element
19
+ # (~24k objects per 60 glossarist documents). The class-level call
20
+ # runs the same walk without the throwaway instance. The instance
21
+ # API is kept for compatibility.
22
+ def self.call(rule, doc, format, attr, register, _options,
17
23
  instance_object)
18
- super()
19
-
20
- @rule = rule
21
- @doc = doc
22
- @format = format
23
- @attr = attr
24
- @register = register
25
- @options = options
26
- @instance_object = instance_object
27
- end
24
+ rule_names = RULE_NAMES_CACHE[rule] ||=
25
+ rule.multiple_mappings? ? rule.name : [rule.name]
28
26
 
29
- def call
30
27
  rule_names.each do |rule_name|
31
- value = rule_value_for(rule_name)
28
+ value = rule_value_for(rule, doc, format, attr, register,
29
+ instance_object, rule_name)
32
30
  return value if Utils.initialized?(value)
33
31
  end
34
32
 
35
- uninitialized_value
36
- end
37
-
38
- private
39
-
40
- attr_reader :rule, :doc, :format, :attr, :register, :options,
41
- :instance_object
42
-
43
- def rule_names
44
- RULE_NAMES_CACHE[rule] ||=
45
- rule.multiple_mappings? ? rule.name : [rule.name]
33
+ Lutaml::Model::UninitializedClass.instance
46
34
  end
47
35
 
48
- def rule_value_for(name)
36
+ def self.rule_value_for(rule, doc, format, attr, register,
37
+ instance_object, name)
49
38
  # When name is nil but document is a hash-like object with a single key matching
50
39
  # the attribute name, extract that value. This handles the case where
51
40
  # map to: :content is used and the document is {"content": "value"}
@@ -58,31 +47,46 @@ instance_object)
58
47
  end
59
48
  end
60
49
 
61
- return doc if root_or_nil?(name)
50
+ return doc if name.nil? || rule.root_mapping?
62
51
  return convert_to_format(doc, format) if rule.raw_mapping?
63
- return fetch_value(name) if Utils.string_or_symbol_key?(doc, name)
52
+ return Utils.fetch_str_or_sym(doc, name) if Utils.string_or_symbol_key?(doc, name)
64
53
 
65
54
  if attr
66
55
  resolved = attr.default_value(register, instance_object)
67
56
  return resolved unless Utils.uninitialized?(resolved)
68
57
  end
69
58
 
70
- uninitialized_value
59
+ Lutaml::Model::UninitializedClass.instance
71
60
  end
72
61
 
73
- def root_or_nil?(name)
74
- name.nil? || rule.root_mapping?
62
+ def self.convert_to_format(doc, format)
63
+ adapter = Lutaml::Model::Config.adapter_for(format)
64
+ adapter.new(doc).public_send(:"to_#{format}")
75
65
  end
76
66
 
77
- def fetch_value(name)
78
- Utils.fetch_str_or_sym(doc, name)
67
+ def initialize(rule, doc, format, attr, register, options,
68
+ instance_object)
69
+ super()
70
+
71
+ @rule = rule
72
+ @doc = doc
73
+ @format = format
74
+ @attr = attr
75
+ @register = register
76
+ @options = options
77
+ @instance_object = instance_object
79
78
  end
80
79
 
81
- def convert_to_format(doc, format)
82
- adapter = Lutaml::Model::Config.adapter_for(format)
83
- adapter.new(doc).public_send(:"to_#{format}")
80
+ attr_reader :rule, :doc, :format, :attr, :register, :options,
81
+ :instance_object
82
+
83
+ def call
84
+ self.class.call(rule, doc, format, attr, register, options,
85
+ instance_object)
84
86
  end
85
87
 
88
+ private
89
+
86
90
  def uninitialized_value
87
91
  Lutaml::Model::UninitializedClass.instance
88
92
  end
@@ -172,7 +172,14 @@ module Lutaml
172
172
  # string_or_symbol_key?(hash, :key) # => true
173
173
  # string_or_symbol_key?(hash, "invalid_key") # => false
174
174
  def string_or_symbol_key?(hash, key)
175
- hash.key?(key.to_s) || hash.key?(key.to_sym)
175
+ if key.is_a?(String)
176
+ hash.key?(key) || hash.key?(key.to_sym)
177
+ elsif key.is_a?(Symbol)
178
+ # Same frozen-string probe as fetch_str_or_sym's Symbol branch.
179
+ hash.key?(key) || hash.key?(key.name)
180
+ else
181
+ hash.key?(key.to_s) || hash.key?(key.to_sym)
182
+ end
176
183
  end
177
184
 
178
185
  # Fetch the value from the hash using the key in string or symbol format
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.157"
5
+ VERSION = "0.8.158"
6
6
  end
7
7
  end
@@ -6,6 +6,10 @@ require "moxml/adapter/oga"
6
6
  module Lutaml
7
7
  module Xml
8
8
  module Adapter
9
+ # @deprecated the Oga engine is on the deprecation path — moxml's
10
+ # default engine (Nokogiri, or the leptris native engine when
11
+ # present) is the supported XML lane. Selection emits a
12
+ # deprecation warning; removal lands in a future release.
9
13
  class OgaAdapter < BaseAdapter
10
14
  MOXML_ADAPTER = Moxml::Adapter::Oga
11
15
  BUILDER_CLASS = Builder::Oga
@@ -7,6 +7,10 @@ require "moxml/adapter/rexml"
7
7
  module Lutaml
8
8
  module Xml
9
9
  module Adapter
10
+ # @deprecated the REXML engine is on the deprecation path — moxml's
11
+ # default engine (Nokogiri, or the leptris native engine when
12
+ # present) is the supported XML lane. Selection emits a
13
+ # deprecation warning; removal lands in a future release.
10
14
  class RexmlAdapter < BaseAdapter
11
15
  MOXML_ADAPTER = Moxml::Adapter::Rexml
12
16
  BUILDER_CLASS = Builder::Rexml
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1086: on a Collection subclass, scalar `map` declarations
6
+ # in key_value were silently ignored alongside `instances` — the scalar
7
+ # attributes neither serialized nor deserialized (data loss, no error).
8
+ # The document is hash-shaped (instance key plus scalar keys) when
9
+ # scalar mappings are declared; collections without scalar mappings keep
10
+ # the legacy bare items array.
11
+ module CollectionScalarMapsSpec
12
+ class Entry < Lutaml::Model::Serializable
13
+ attribute :name, :string
14
+ key_value { map "name", to: :name }
15
+ end
16
+
17
+ class StampToy < Lutaml::Model::Collection
18
+ instances :entries, Entry
19
+ attribute :stamp, :integer, default: -> { 0 }
20
+ key_value do
21
+ map "entries", to: :entries
22
+ map "stamp", to: :stamp
23
+ end
24
+ end
25
+ end
26
+
27
+ RSpec.describe "Collection with scalar key_value mappings (#1086)" do
28
+ let(:toy) do
29
+ collection = CollectionScalarMapsSpec::StampToy.new
30
+ collection.entries = [CollectionScalarMapsSpec::Entry.new(name: "x")]
31
+ collection.stamp = 42
32
+ collection
33
+ end
34
+
35
+ it "serializes the scalars alongside the instances" do
36
+ expect(toy.to_yaml).to include("stamp: 42")
37
+ expect(toy.to_yaml).to include("entries:")
38
+ end
39
+
40
+ it "deserializes the scalars from the hash-shaped document" do
41
+ parsed = CollectionScalarMapsSpec::StampToy.from_yaml(toy.to_yaml)
42
+ expect(parsed.stamp).to eq(42)
43
+ expect(parsed.entries.size).to eq(1)
44
+ expect(parsed.entries.first.name).to eq("x")
45
+ end
46
+
47
+ it "keeps the legacy bare-array document parseable" do
48
+ parsed = CollectionScalarMapsSpec::StampToy.from_yaml("---\n- name: y\n")
49
+ expect(parsed).to be_a(CollectionScalarMapsSpec::StampToy)
50
+ expect(parsed.entries.size).to eq(1)
51
+ expect(parsed.entries.first.name).to eq("y")
52
+ end
53
+
54
+ it "round-trips through the hash shape" do
55
+ parsed = CollectionScalarMapsSpec::StampToy.from_yaml(toy.to_yaml)
56
+ expect(parsed.to_yaml).to eq(toy.to_yaml)
57
+ end
58
+ 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.157
4
+ version: 0.8.158
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -1753,6 +1753,7 @@ files:
1753
1753
  - spec/lutaml/model/collection_mutation_spec.rb
1754
1754
  - spec/lutaml/model/collection_reader_liveness_spec.rb
1755
1755
  - spec/lutaml/model/collection_register_spec.rb
1756
+ - spec/lutaml/model/collection_scalar_maps_spec.rb
1756
1757
  - spec/lutaml/model/collection_single_value_coercion_spec.rb
1757
1758
  - spec/lutaml/model/collection_spec.rb
1758
1759
  - spec/lutaml/model/collection_state_nesting_spec.rb