Skip to content

[java] Made JsonToWebElementConverter methods/fields protected #15885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

AB-xdev
Copy link
Contributor

@AB-xdev AB-xdev commented Jun 10, 2025

User description

So that the class can be properly extended.

🔗 Related Issues

Fixes #15884

💥 What does this PR do?

Make the corresponding fields/methods protected

🔧 Implementation Notes

So that the class can be properly extended/overridden.

💡 Additional Considerations

Don't use private (or similar things) in libraries that other people use.

I already encountered this problem so often that I had to write a small blog post.

🔄 Types of changes

  • Cleanup

PR Type

Other


Description

• Changed private fields/methods to protected in JsonToWebElementConverter
• Enables proper class extension and customization
• Improves library extensibility for downstream users


Changes walkthrough 📝

Relevant files
Enhancement
JsonToWebElementConverter.java
Change visibility modifiers from private to protected       

java/src/org/openqa/selenium/remote/JsonToWebElementConverter.java

• Changed driver field from private to protected
• Made setOwner()
method protected instead of private
• Made getElementKey() method
protected instead of private
• Made getShadowRootKey() method
protected instead of private

+4/-4     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • so that the class can be properly extended
    @CLAassistant
    Copy link

    CLAassistant commented Jun 10, 2025

    CLA assistant check
    All committers have signed the CLA.

    @selenium-ci selenium-ci added the C-java Java Bindings label Jun 10, 2025
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    API Change

    Changing visibility from private to protected is a breaking change that expands the API surface. This should be carefully considered as it makes these methods part of the public contract that subclasses can depend on, potentially limiting future refactoring flexibility.

    protected final RemoteWebDriver driver;
    
    public JsonToWebElementConverter(RemoteWebDriver driver) {
      this.driver = driver;
    }
    
    @Override
    public Object apply(Object result) {
      if (result instanceof Collection<?>) {
        Collection<?> results = (Collection<?>) result;
        return results.stream().map(this).collect(Collectors.toList());
      }
    
      if (result instanceof Map<?, ?>) {
        Map<?, ?> resultAsMap = (Map<?, ?>) result;
        String elementKey = getElementKey(resultAsMap);
        if (null != elementKey) {
          RemoteWebElement element = newRemoteWebElement();
          element.setId(String.valueOf(resultAsMap.get(elementKey)));
          return element;
        }
    
        String shadowKey = getShadowRootKey(resultAsMap);
        if (null != shadowKey) {
          return new ShadowRoot(driver, String.valueOf(resultAsMap.get(shadowKey)));
        }
    
        // some values are converted to null, so we can not use Collectors.toMap here
        Map<Object, Object> converted = new LinkedHashMap<>();
        resultAsMap.forEach((k, v) -> converted.put(k, this.apply(v)));
        return converted;
      }
    
      if (result instanceof RemoteWebElement) {
        return setOwner((RemoteWebElement) result);
      }
    
      if (result instanceof Number) {
        if (result instanceof Float || result instanceof Double) {
          return ((Number) result).doubleValue();
        }
        return ((Number) result).longValue();
      }
    
      return result;
    }
    
    protected RemoteWebElement newRemoteWebElement() {
      return setOwner(new RemoteWebElement());
    }
    
    protected RemoteWebElement setOwner(RemoteWebElement element) {
      if (driver != null) {
        element.setParent(driver);
        element.setFileDetector(driver.getFileDetector());
      }
      return element;
    }
    
    protected String getElementKey(Map<?, ?> resultAsMap) {
      for (Dialect d : Dialect.values()) {
        String elementKeyForDialect = d.getEncodedElementKey();
        if (resultAsMap.containsKey(elementKeyForDialect)) {
          return elementKeyForDialect;
        }
      }
      return null;
    }
    
    protected String getShadowRootKey(Map<?, ?> resultAsMap) {

    Copy link
    Contributor

    qodo-merge-pro bot commented Jun 10, 2025

    PR Code Suggestions ✨

    No code suggestions found for the PR.

    @AB-xdev AB-xdev changed the title Made JsonToWebElementConverter methods/fields protected [java] Made JsonToWebElementConverter methods/fields protected Jun 13, 2025
    Copy link
    Member

    @diemol diemol left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Thank you, @AB-xdev!

    @diemol diemol merged commit 093fd7f into SeleniumHQ:trunk Jun 23, 2025
    33 of 34 checks passed
    @AB-xdev AB-xdev deleted the make-JsonToWebElementConverter-protected branch June 24, 2025 06:20
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    [🐛 Bug]: JsonToWebElementConverter can't be properly extended
    6 participants