Skip to content

[🐛 Bug]: Selenium Java API: Serialization of 'Rectangle' objects by the Json API includes derived properties #14035

@sbabcoc

Description

@sbabcoc

What happened?

The standard Rectangle class (org.openqa.selenium.Rectangle) lacks a toJson() method, which causes the serialization performed by the Json API to fall back to scanning for methods that look like JavaBean accessors to extract object properties. In addition to the methods that provide access to inherent properties (width, height, x, and y), the Rectangle class provides two methods that produce derived properties (dimension and point). Consequently, the JSON current returned for Rectangle objects includes these derived properties as well.

How can we reproduce the issue?

@Test
public void serializeRectangle() {
    Rectangle r = new Rectangle(640, 480, 320, 240);
    String j = new Json().toJson(r);
    System.out.println(j);
}

static class NewRectangle extends Rectangle {
    public NewRectangle(int x, int y, int height, int width) {
        super(x, y, height, width);
    }
    
    private Map<String, Object> toJson() {
        return Map.of("width", width, "height", height, "x", x, "y", y);
    }
}

@Test
public void serializeNewRectangle() {
    Rectangle r = new NewRectangle(640, 480, 320, 240);
    String j = new Json().toJson(r);
    System.out.println(j);
}

Relevant log output

// output from `serializeRectangle`
{
  "width": 240,
  "x": 640,
  "y": 480,
  "class": "org.openqa.selenium.Rectangle",
  "dimension": {
    "width": 240,
    "class": "org.openqa.selenium.Dimension",
    "height": 320
  },
  "point": {
    "x": 640,
    "y": 480,
    "class": "org.openqa.selenium.Point"
  },
  "height": 320
}

// output from `serializeNewRectangle`
{
  "x": 640,
  "y": 480,
  "width": 240,
  "height": 320
}


### Operating System

Irrelevant

### Selenium version

Java 4.21.0

### What are the browser(s) and version(s) where you see this issue?

None

### What are the browser driver(s) and version(s) where you see this issue?

None

### Are you using Selenium Grid?

Irrelevant

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-javaJava BindingsI-defectSomething is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions