Skip to content

[BUG]: Python and Go output break on property names obj, list, -, backticks, and commas #3547

Open

Description

@trackoor

Output Language: Python, Go
CLI, npm, or app.quicktype.io: CLI and npm (quicktype / quicktype-core)
Version: 26.0.0 (also on master c39af7d)

Description

Round-tripping JSON samples through the generated Python and Go code fails for the property names below:

  1. Python (obj and optional list):
    • A property named obj is stored in a local variable named obj inside from_dict(obj), overwriting the obj parameter so every subsequent obj.get(...) / obj[...] reads from the wrong value and raises AttributeError (26.0.0) or TypeError (master).
    • An optional property named list emits list: list[...] | None = None in the class body. In Python, the = None assignment runs before the annotation is evaluated in the class scope, so list is None when list[...] is evaluated, raising TypeError: 'NoneType' object is not subscriptable on import.
  2. Go (-, backticks, and commas/quotes in JSON keys):
    • A property named "-" produces the struct tag `json:"-"`, which tells encoding/json to always omit the field instead of matching the key "-" (which requires `json:"-,"` per the encoding/json docs).
    • A property containing a backtick produces an unclosed raw string literal in the struct tag and does not compile.
    • A property containing a comma (such as "a,b") is emitted unescaped in `json:"a,b"`, so encoding/json treats ,b as tag options and reads/writes key "a" instead. Keys with quotes or backslashes fail encoding/json's isValidTag check and silently fall back to the Go struct field name.

Input Data

{"obj": {"a": 1}, "x": [{"b": 1}]}
{"x": [{"list": [1]}, {}]}
{"-": 1, "a,b": 2, "x": 3}
{"a`b": 1}

Expected Behaviour / Output

  • Both generated Python modules import cleanly and a.root_to_dict(a.root_from_dict(...)) returns {'obj': {'a': 1}, 'x': [{'b': 1}]}.
  • The generated Go code compiles and round-trips {"-":1,"a,b":2,"x":3}.

Current Behaviour / Output

Python output (a.py and b.py):

  File "a.py", line 64, in from_dict
    x = from_list(X.from_dict, obj.get("x"))
AttributeError: 'Obj' object has no attribute 'get'

  File "b.py", line 39, in X
    list: list[int] | None = None
TypeError: 'NoneType' object is not subscriptable

(On master c39af7d, a.py raises TypeError: 'Obj' object is not subscriptable on obj["x"].)

Go output (c.go and d/d.go):

{"a":0,"x":3}
c.go:	Empty int64 `json:"-"`
c.go:	AB    int64 `json:"a,b"`
c.go:	X     int64 `json:"x"`
d/d.go:	AB int64 `json:"a`b"`
vet: d/d.go:22:20: expected ';', found b (and 2 more errors)

Steps to Reproduce

# npm i quicktype@26.0.0
echo '{"obj": {"a": 1}, "x": [{"b": 1}]}' > a.json
echo '{"x": [{"list": [1]}, {}]}' > b.json
npx quicktype --lang python --top-level Root -o a.py a.json
npx quicktype --lang python --top-level Root -o b.py b.json
python3 -c 'import json, a; print(a.root_to_dict(a.root_from_dict(json.load(open("a.json")))))'
python3 -c 'import b'

echo '{"-": 1, "a,b": 2, "x": 3}' > c.json
echo '{"a`b": 1}' > d.json
mkdir -p d
npx quicktype --lang go --package main --top-level Root -o c.go c.json
npx quicktype --lang go --package main -o d/d.go d.json
cat > main.go <<'GO'
package main

import ("fmt"; "os")

func main() {
	b, _ := os.ReadFile("c.json")
	r, _ := UnmarshalRoot(b)
	out, _ := r.Marshal()
	fmt.Println(string(out))
}
GO
go mod init repro >/dev/null 2>&1; go run main.go c.go
grep json: c.go d/d.go
go vet ./d

Possible Solution

  • In Python, bool, dict, float, int, and str are in forbiddenPropertyNames in packages/quicktype-core/src/language/Python/constants.ts, and JSONPythonRenderer reserves from_dict/to_dict (fix(python): reserve dict conversion methods #3506). Adding list to forbiddenPropertyNames and reserving obj in JSONPythonRenderer.forbiddenForObjectProperties (or renaming the from_dict parameter) avoids both collisions.
  • In GolangRenderer, emit json:"-," (or json:"-,omitempty") when the JSON property name is "-", and use a quoted string literal for the struct tag when the key contains a backtick.

Credit: https://ucsbplse.github.io/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions