selenium icon indicating copy to clipboard operation
selenium copied to clipboard

[bidi] Generators drop the type of a field-less synthetic map record

Open titusfortner opened this issue 2 months ago • 5 comments

Description

The BiDi CDDL declares script.NodeProperties.attributes as {*text => text}: a JSON object with string keys and string values. The schema projector has no inline map form for a field, so it hoists the shape into a synthetic named record with no fields and a map key:

"script.NodePropertiesAttributes": {
  "kind": "record", "fields": [], "map": {"primitive": "string"},
  "synthetic": true, "owner": "script.NodeProperties", "label": "Attributes",
  "outbound": false, "inbound": true
}

All three generators decide what a record ref means by looking at kind and whether fields is empty. None of them consult the map key on a record node, so the value type the schema carries is dropped. The three bindings fail in different directions.

The same synthetic shape appears three more times in the Mozilla vendor section: mozDebugging.PausedParametersScopeVariables (map of a ref), session.CapabilityRequestFirefoxOptionsEnv (map of string), and session.CapabilityRequestFirefoxOptionsPrefs (map of a string/boolean/js-int union). Two of those are outbound.

Impact

Ruby (rb/lib/selenium/webdriver/bidi/support/bidi_generate.rb)

resolve_named returns the OPAQUE descriptor for any record with empty fields (line 975). The generated field is

attributes: {wire_key: 'attributes', required: false},

with no ref, primitive, list, or map. The record reader only runs the checks a descriptor names, so the raw wire value passes through untouched. The RBS reader is untyped.

Behavior with the generated NodeProperties on trunk (rb/lib/selenium/webdriver/bidi/protocol/script.rb:742):

good attributes:            {"id" => "main", "class" => "a b"}
attributes as string        "oops"                     # accepted
attributes with int value   {"id" => 1}                # accepted
attributes as array         ["id", "main"]             # accepted
localName as int (sibling)  SerializationError: NodeProperties#local_name expected string, got 1

Consumers get the right Hash for a conforming browser, but the field is the only one in the class exempt from shape and primitive validation, and the one precise type the schema gives Ruby is thrown away. variables in moz_debugging.rbs is untyped for the same reason.

Python (py/generate_bidi_protocol.py)

Identical rule at lines 592 and 681: a fieldless record resolves to the opaque descriptor and the Any type hint. Generated output:

attributes: Any | UnsetType = field(default=UNSET, metadata=meta("attributes"))

Siblings carry primitive="str" or ref=... and are checked by _read_field; this one is not. Same silent pass-through of a malformed value, same loss of the dict[str, str] type hint.

Java (PR #17777, java/src/org/openqa/selenium/bidi/BiDiGenerator.java)

The Java generator does handle a map typeRef when it appears inline on a field: resolveJavaType emits java.util.Map<String, V>, and serializeExpr and immutableCopyExpr have matching branches. But the projected schema never presents a map inline. It always presents it as a ref to the fieldless synthetic record, and a ref to a record kind resolves to a class via resolveRefToJavaClass.

So attributes becomes a nested NodeProperties.Attributes class with zero fields. That class is receivable and not extensible, so it gets @WarnOnUnknownFields. Deserializing {"id": "main", "class": "a b"} yields an empty Attributes object and a warning per key. The data is not merely unchecked, it is unreachable. The hand-written NodeProperties.java on trunk declares Optional<Map<String, String>> attributes, so the generated class is a regression against the current API.

Java's inline-map branches are dead code against the schema as projected, which is why they have not caught this.

Proposed fix

Teach each generator's ref resolution that a record with empty fields and a map key is the map it wraps:

  • Ruby: resolve to a map descriptor carrying the value primitive; add map to FieldIR; Record#read checks the value is a Hash and each value against PRIMITIVE_CHECKS (mirrored outbound); RBS emits Hash[String, String].
  • Python: same resolution; _Wire gains a map slot; _read_field checks dict shape and value primitive; type hint becomes dict[str, str].
  • Java: in resolveRefType, when the target record has empty fields and a map key, delegate to resolveJavaType with the map typeRef so the existing Map<String, V> path is used and no nested empty class is emitted for that synthetic type. computeSyntheticChildren should skip it too.

Suggested scope for a first pass: primitive-valued maps only. A ref-valued or union-valued map (the two remaining Mozilla types) can stay opaque until there is a reason to deserialize its values, which keeps each diff small and avoids deciding the vendor design question here.

An alternative is for the projector to stop hoisting maps and emit them inline on the field, which would make Java correct with no generator change and make the Ruby and Python gap an explicit missing feature rather than a silent one. That is a larger change to a shared artifact and affects every consumer of common/bidi/schema.json, so it is noted here rather than proposed.

Reproduction

ruby -Irb/lib -e 'require "selenium-webdriver"; require "selenium/webdriver/bidi"; require "selenium/webdriver/bidi/protocol"; p Selenium::WebDriver::BiDi::Protocol::Script::NodeProperties.from_json("nodeType"=>1, "childNodeCount"=>0, "attributes"=>"oops").attributes'

Expected: SerializationError. Actual: "oops".

For Python, run py/generate_bidi_protocol.py common/bidi/schema.json <out> and inspect the attributes field of NodeProperties in script.py. For Java, generate from the PR #17777 branch and inspect script/NodeProperties.java.

Related

  • PR #17777 (Java BiDi generator)
  • common/bidi/schema.json, type script.NodePropertiesAttributes

titusfortner avatar Sep 30 '26 19:09 titusfortner

Hi @titusfortner, I'd like to pick up the Java part of this. #18095 (the RemoteValue decode fix for #18076) is waiting on review, so I have time to work on this in the meantime.

Plan, following the primitive-valued-maps-first scope:

In resolveRefType, when the target record has empty fields and a map key, delegate to resolveJavaType with that map typeRef. That way it goes through the existing java.util.Map<String, V> path, and the serializeExpr / immutableCopyExpr branches are used.

Make computeSyntheticChildren skip these synthetic map records, so no empty nested NodeProperties.Attributes class (and no @WarnOnUnknownFields) is generated.

Add a test that deserializes {"id": "main", "class": "a b"} into NodeProperties and checks that attributes comes back as Optional<Map<String, String>>, matching the current hand-written API.

Leave the ref- and union-valued Mozilla types (PausedParametersScopeVariables, FirefoxOptionsPrefs) opaque for now, as you suggested.

Since BiDiGenerator only lives in #17777, how would you like me to submit this? I could open a PR against the #17777 branch, wait and open a follow-up once #17777 merges, or post the patch here for you to fold in.

yashp676 avatar Oct 02 '26 10:10 yashp676

If I'm doing something that relies on another PR, I usually rebase off the other PR and mark the PR draft with a message that the other PR must merge first. Hopefully we get 17777 merged soon all the code gets very complicated very quickly.

Thanks for the work on it.

titusfortner avatar Oct 02 '26 16:10 titusfortner

In resolveRefType, we need to ensure the check for empty fields also validates the presence of the map key before falling back to the OPAQUE descriptor. Since the Ruby generator currently treats any empty record as opaque, are we planning to introduce a new descriptor type for these synthetic maps, or should they be handled as a special case within the existing field resolution logic?

pricklymusic avatar Oct 02 '26 22:10 pricklymusic

HI @titusfortner I tried to tackle this on my pr here https://github.com/SeleniumHQ/selenium/pull/18121/changes, let me know if it makes sense!

aguspe avatar Oct 03 '26 14:10 aguspe

Opened #18122 for the Java part, as a draft rebased on #17777 as you suggested. It'll stay draft until #17777 merges, then I'll rebase onto trunk and mark it ready

yashp676 avatar Oct 04 '26 10:10 yashp676