TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.47k stars 2.57k forks source link

Bug Report: Violation of Liskov Substitution Principle #1425

Closed 2Dsa3 closed 3 weeks ago

2Dsa3 commented 3 months ago

Description of the Problem In the Java-WebSocket repository, there is a violation of the Liskov Substitution Principle (LSP) at line 275 in the file PerMessageDeflateExtension.java. The parent class always accepts an extension while the child class does not. These differing behaviors violate the Liskov Substitution Principle.

Steps to Reproduce Open the PerMessageDeflateExtension.java file located at src/main/java/org/java_websocket/extensions/permessage_deflate/. Navigate to line 275. Observe the method implementation in both the parent and child classes. Expected Behavior The child class should adhere to the contract established by the parent class, ensuring consistent behavior. In this context, if the parent class always accepts an extension, the child class should either follow this behavior or explicitly handle exceptions in a manner that preserves the expected functionality.

Proposed Solution To resolve this violation, the following steps are recommended:

Ensure Consistent Behavior: Modify the child class to align with the behavior of the parent class, ensuring that extensions are handled consistently. Refactor Code: If the differing behaviors are necessary, consider refactoring the code to use a different inheritance or composition model to avoid violating the LSP. Code Example Parent Class Implementation:

java Copiar código public class ParentExtension { public boolean acceptExtension() { // Logic to always accept the extension return true; } } Child Class Implementation (Current):

java Copiar código public class ChildExtension extends ParentExtension { @Override public boolean acceptExtension() { // Logic that does not always accept the extension return false; // Example of violation } } Child Class Implementation (Proposed):

java Copiar código public class ChildExtension extends ParentExtension { @Override public boolean acceptExtension() { // Ensure behavior aligns with the parent class return super.acceptExtension(); } } By ensuring that the child class adheres to the contract established by the parent class, we maintain consistency and comply with the Liskov Substitution Principle.

Additional Information Providing any other details that can help resolve the issue would be beneficial.

This approach should help address the violation of the Liskov Substitution Principle and ensure more maintainable and reliable code within the Java-WebSocket project.