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

Offen
#303 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
ruby
Bereich
tooling

Rechercherichtung

Beginne mit lib/psych/json/yaml_events.rb an der verlinkten Zeile und reproduziere das Problem mithilfe des ausführbaren Tests im bereitgestellten Gist. Verfolge, wie skalare Werte und Mapping-Schlüssel während Psych.to_json markiert werden, und füge anschließend einen Regressionstest hinzu, der zeigt, dass das ausgegebene Dokument gültiges JSON mit in Anführungszeichen gesetzten Schlüsseln ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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)

Vorherrschende Sprache
Ruby
Sterne
596
Forks
223
Ø Merge
8 Std. 38 Min.
Gemergte PRs (30 T.)
2

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus ruby/psych

Alle Issues in ruby/psych

Ähnliche Issues

Weitere Issues zu Ruby

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.