Skip to content

Fix Gem::YAMLSerializer roundtrip of quoted metadata keys and values#9617

Merged
hsbt merged 3 commits into
masterfrom
claude/blissful-panini-09da64
Jun 12, 2026
Merged

Fix Gem::YAMLSerializer roundtrip of quoted metadata keys and values#9617
hsbt merged 3 commits into
masterfrom
claude/blissful-panini-09da64

Conversation

@hsbt

@hsbt hsbt commented Jun 11, 2026

Copy link
Copy Markdown
Member

What was the end-user or developer problem that led to this PR?

Gem::YAMLSerializer could not roundtrip gemspec metadata containing quotes or other special characters because the emitter wrote them as plain scalars and the parser did not understand quoted mapping keys.

Fix #9560

What is your fix for the problem, implemented in this PR?

This change quotes emitted strings that would be misread back, parses quoted mapping keys, and treats "#" as a comment only when preceded by whitespace, so the output now roundtrips and matches Psych in both directions.

Make sure the following tasks are checked

Metadata may contain any UTF-8 keys and values, but the emitter wrote
strings wrapped in quotes as plain scalars and never quoted mapping
keys, so quotes were stripped on load and keys containing ": " or "#"
misparsed. When a quoted key appeared first in a nested mapping, the
whole mapping was swallowed as a scalar and the remaining pairs leaked
into top-level specification attributes, raising NameError. The parser
also kept Psych's quote wrappers in mapping keys when reading gems
packaged by Psych-based RubyGems.

Quote emitted strings that would be misread back, parse quoted mapping
keys on load, and treat "#" as a comment only when preceded by
whitespace, matching Psych in both directions.

Fix #9560

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 11, 2026 22:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves Gem::YAMLSerializer so gemspec metadata containing quotes and other special characters can be emitted and parsed in a way that roundtrips correctly (including Psych-style quoted mapping keys).

Changes:

  • Expanded parser support to recognize quoted mapping keys and refined handling of # as a comment delimiter.
  • Updated emitter quoting rules so keys/values that would otherwise be misread are emitted as quoted scalars.
  • Added/expanded tests covering quoted keys/values and special-character metadata cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
test/rubygems/test_gem_safe_yaml.rb Adds roundtrip and compatibility tests for quoted/special metadata keys and values.
lib/rubygems/yaml_serializer.rb Updates YAML parsing/emitting to better handle quoted keys and special characters in scalars.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/rubygems/yaml_serializer.rb Outdated
Comment on lines +439 to +441
when "#"
# A "#" starts a comment only when preceded by whitespace.
return val[0...i].rstrip if i.zero? || val[i - 1] == " " || val[i - 1] == "\t"
hsbt and others added 2 commits June 12, 2026 07:54
quote_string only escaped backslash, double quote and a few whitespace
characters, so other C0 control characters were emitted as raw bytes,
which is invalid YAML inside quoted scalars. Quote strings containing
control characters and escape them with the YAML escapes Psych uses,
unescaping them symmetrically on load.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
strip_comment returned the value unchanged when it consisted entirely
of a comment, so "key: # comment" loaded as the literal string
"# comment" instead of nil as Psych does. Return an empty value so the
comment is dropped, and remove the now unreachable i.zero? condition.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@hsbt hsbt merged commit d0b05b1 into master Jun 12, 2026
107 checks passed
@hsbt hsbt deleted the claude/blissful-panini-09da64 branch June 12, 2026 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gem::YAMLSerializer may not roundtrip metadata

2 participants