Psych.to_json can create invalid JSON if scalar values are not marked at quoted

未关闭
#303 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
ruby
领域
tooling

调研方向

从链接所指的行开始查看 lib/psych/json/yaml_events.rb,并使用所提供 gist 中的可执行测试重现该问题。跟踪在 Psych.to_json 期间标量值和映射键是如何被标记的,然后添加一个回归测试,表明生成的文档是键带引号的有效 JSON。

由索引模型根据 Issue 内容生成。

描述

Steps to reproduce

I have created an executable test for this bug: https://gist.github.com/ianks/ba1f7175754780168c2a6618f13c95ec

I did not know whether to report this to Psych or Rails, just let me know if it belongs elsewhere and I will move the issue.

Expected behavior

Psych.to_json should emit valid JSON. Currently, not all keys are double-quoted; which although valid in JS, is not valid JSON.

Actual behavior

It emits invalid json, which can be seen in this example:

{"raw_attributes": {"id": null}, "attributes": {attributes: {"id": {name: "id", value_before_type_cast: null,
        type: {precision: null, scale: null, limit: null, range: {"begin": -2147483648,
            "end": 2147483648, "excl": true}}, original_attribute: null}}}, "new_record": true,
  "active_record_yaml_version": 1}
System configuration

Rails version: 5.0.0

Ruby version: 2.3.1

Notes

Upon further investigation, it seems the scalar types for attributes are not marked as quoted. Even if they are not marked as quoted, should we still quote the values since all keys need to be quoted in JSON? (corresponding code https://github.com/ruby/psych/blob/master/lib/psych/json/yaml_events.rb#L21)

主要语言
Ruby
星标
596
派生
223
平均合并
8 小时 38 分钟
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ruby/psych 的其他 Issue

查看 ruby/psych 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。