Creating a Dynamic with property default set produces a broken object #175

Open
opened 2025-12-30 01:21:46 +01:00 by adam · 3 comments
Owner

Originally created by @HT154 on GitHub (Jun 29, 2024).

The behavior as-is is very surprising! Check this out:

import "pkl:json"

hidden jsonString = """
  {
    "hello": "world",
    "default": "greeting"
  }
  """

asDynamic = new json.Parser {}.parse(jsonString)
asMapping = new json.Parser { useMapping = true }.parse(jsonString)

Result:

asDynamic {
  hello = "world"
}
asMapping {
  ["hello"] = "world"
  ["default"] = "greeting"
}

I think throwing an error (or at least some kind of warning) here instead of returning unexpected results is desirable. Something like "encountered object key default when parsing JSON, resulting Dynamic will be incomplete or invalid" might make sense.

N.B.: Attempting to access the default yields further surprises:

asDynamicDefault = asDynamic.default

Result:

❯ pkl eval test.pkl
–– Pkl Error ––
Expected value of type `Function1`, but got type `String`.
Value: "greeting"

1741 | hidden default: (unknown) -> Any = (_) -> new Dynamic {}
                       ^^^^^^^^^^^^^^^^
at pkl.base#Dynamic.default (https://github.com/apple/pkl/blob/0.26.0/stdlib/base.pkl#L1741)

1 | 
    ^
at generated (source:unavailable)

11 | asDynamicDefault = asDynamic.default
                        ^^^^^^^^^^^^^^^^^
at test#asDynamicDefault (file:///.../test.pkl, line 11)

106 | text = renderer.renderDocument(value)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.26.0/stdlib/base.pkl#L106)

This similarly affects yaml.Parser.parse() and Map.toDynamic().

Originally created by @HT154 on GitHub (Jun 29, 2024). The behavior as-is is very surprising! Check this out: ```pkl import "pkl:json" hidden jsonString = """ { "hello": "world", "default": "greeting" } """ asDynamic = new json.Parser {}.parse(jsonString) asMapping = new json.Parser { useMapping = true }.parse(jsonString) ``` Result: ```pkl asDynamic { hello = "world" } asMapping { ["hello"] = "world" ["default"] = "greeting" } ``` I think throwing an error (or at least some kind of warning) here instead of returning unexpected results is desirable. Something like "encountered object key `default` when parsing JSON, resulting `Dynamic` will be incomplete or invalid" might make sense. N.B.: Attempting to access the default yields further surprises: ```pkl asDynamicDefault = asDynamic.default ``` Result: ``` ❯ pkl eval test.pkl –– Pkl Error –– Expected value of type `Function1`, but got type `String`. Value: "greeting" 1741 | hidden default: (unknown) -> Any = (_) -> new Dynamic {} ^^^^^^^^^^^^^^^^ at pkl.base#Dynamic.default (https://github.com/apple/pkl/blob/0.26.0/stdlib/base.pkl#L1741) 1 | ^ at generated (source:unavailable) 11 | asDynamicDefault = asDynamic.default ^^^^^^^^^^^^^^^^^ at test#asDynamicDefault (file:///.../test.pkl, line 11) 106 | text = renderer.renderDocument(value) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.26.0/stdlib/base.pkl#L106) ``` This similarly affects `yaml.Parser.parse()` and `Map.toDynamic()`.
adam added the buggood first issue labels 2025-12-30 01:21:46 +01:00
Author
Owner

@bioball commented on GitHub (Jul 3, 2024):

Fair issue; seems like a good idea to throw here instead.

@bioball commented on GitHub (Jul 3, 2024): Fair issue; seems like a good idea to throw here instead.
Author
Owner

@lemonnn-8 commented on GitHub (Aug 24, 2024):

Hey, I can take this up.

@lemonnn-8 commented on GitHub (Aug 24, 2024): Hey, I can take this up.
Author
Owner

@HT154 commented on GitHub (Feb 7, 2025):

This actually doesn't just affect JSON/YAML parsing. It also affects Map.toDynamic():

❯ pkl eval pkl:base -x 'Map("default",  1, "other", 2).toMap().toDynamic().default'       
–– Pkl Error ––
Expected value of type `Function1`, but got type `Int`.
Value: 1
@HT154 commented on GitHub (Feb 7, 2025): This actually doesn't just affect JSON/YAML parsing. It also affects `Map.toDynamic()`: ``` ❯ pkl eval pkl:base -x 'Map("default", 1, "other", 2).toMap().toDynamic().default' –– Pkl Error –– Expected value of type `Function1`, but got type `Int`. Value: 1 ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#175