SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
29.78k stars 8.02k forks source link

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

Closed sbabcoc closed 1 month ago

sbabcoc commented 1 month ago

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
github-actions[bot] commented 1 month ago

@sbabcoc, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] commented 1 week ago

This issue has been automatically locked since there has not been any recent activity since it was closed. Please open a new issue for related bugs.