Skip to content

Commit 3f87932

Browse files
committed
switch safeyaml scalar transformation for psych
fixes a problem with serializing strings like "1._" and hopefully other problems lurking inside safeyaml closes #CNVS-28660 Change-Id: I45353dcfa84dbe5eba902a731219590f1ba1daf8 Reviewed-on: https://gerrit.instructure.com/76896 Reviewed-by: Cody Cutrer <cody@instructure.com> Tested-by: Jenkins Product-Review: James Williams <jamesw@instructure.com> QA-Review: James Williams <jamesw@instructure.com>
1 parent db8d2c1 commit 3f87932

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

lib/canvas_yaml.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,16 @@ def tokenize string
257257
end
258258
Psych::ScalarScanner.prepend(FloatScannerFix)
259259

260-
module IntegerTransformFix # fixes strings with leading underscores before integers being parsed as integers
261-
def transform?(value)
262-
SafeYAML::Transform::ToInteger::MATCHERS.each_with_index do |matcher, idx|
263-
if matcher.match(value)
264-
value = value.gsub(/[_,]/, "") if idx == 0 # only strip these out _after_ we've matched the integer
265-
return true, Integer(value)
266-
end
260+
module ScalarTransformFix
261+
def to_guessed_type(value, quoted=false, options=nil)
262+
return value if quoted
263+
264+
if value.is_a?(String)
265+
@ss ||= Psych::ScalarScanner.new(Psych::ClassLoader.new)
266+
return @ss.tokenize(value) # just skip straight to Psych if it's a scalar because SafeYAML's transform mades me a sad panda
267267
end
268-
try_edge_cases?(value)
268+
269+
value
269270
end
270271
end
271-
SafeYAML::Transform::ToInteger.prepend(IntegerTransformFix)
272+
SafeYAML::Transform.singleton_class.prepend(ScalarTransformFix)

spec/lib/safe_yaml_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,9 @@ def verify(result, key, klass)
188188
hash = {:blah => "_42"}
189189
expect(YAML.load(YAML.dump(hash))).to eq hash
190190
end
191+
192+
it "should also dump floaat looking strings followed by an underscore" do
193+
hash = {:blah => "42._"}
194+
expect(YAML.load(YAML.dump(hash))).to eq hash
195+
end
191196
end

0 commit comments

Comments
 (0)