fix: remove global xml_adapter_type override#66
Merged
Conversation
Setting Lutaml::Model::Config.xml_adapter_type globally at require time is a side-effect that affects every other gem sharing the process. This caused hangs in metanorma-ietf where rfcxml sets :nokogiri but unitsml silently switches to :ox, breaking subsequent to_xml calls. unitsml uses standard Lutaml::Model::Serializable models and calls to_xml/from_xml on them -- none of this requires a specific adapter. The host application should control the adapter configuration.
Ensures an XML adapter is always available for lutaml-model's auto-detection, without needing to set the global adapter type.
suleman-uzair
approved these changes
Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
lib/unitsml.rbunconditionally sets the globalLutaml::Model::Config.xml_adapter_type = :oxat require time:This runs when
require "unitsml"is called (e.g. viaplurimath→unitsml), silently overwriting the global adapter for every other gem in the same Ruby process.Impact
rfcxmlthat explicitly set:nokogirihave their configuration overridden.metanorma-ietf, this causedto_xmlto hang on repeated calls because the adapter was switched mid-process from:nokogirito:ox.:oxadapter has known reentrancy issues that manifest as hangs when used in a process that was initialized with a different adapter.Reproduction
Fix
Two changes:
configureblock fromlib/unitsml.rb.oxas a runtime dependency in the gemspec.Why this works — the adapter convention for lutaml-model gems
Lutaml::Model::Config.xml_adapter_typeis a global singleton — one per process. When multiple gems set it, the lastrequirewins, which is fragile and order-dependent. The correct convention is:Only the application boundary sets the adapter. Libraries never do.
How auto-detection handles the default case
When
require "lutaml/model"runs, it auto-detects the best available adapter (nokogiri > ox > oga):This is already the right default. The problem is libraries overriding it afterward.
What each gem must do
spec.add_dependency "ox"). Do NOT callConfig.xml_adapter_type =. Auto-detection finds the installed adapter.Config.xml_adapter_typein application init, before loading libraries. This sticks because libraries no longer override it.Config.xml_adapter_typeinexe/xxxbeforerequire "unitsml".spec_helper.rbsets the adapter. Tests are an application boundary.Why this is seamless
gem install unitsmlstandaloneox→ auto-detection findsox→ worksmetanorma-ietfsets:nokogirithen loads unitsml:nokogiristicks → worksOther gems that should adopt this convention
Config.xml_adapter_type = :nokogirifromlib/rfcxml.rb, addnokogirias gemspec dependency.Config.xml_adapter_type =in library code, declare adapter gem in gemspec.