Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 14, 2025

Fix: Add static keyword to wsBefore and wsAfter methods in ApiBuilder

Issue

Cannot use wsBefore & wsAfter in ApiBuilder because the methods are missing the static keyword.

Plan

  • Explore repository and understand the issue
  • Build project to verify current state
  • Add static keyword to wsBefore and wsAfter methods in ApiBuilder.java
  • Create focused test to validate the fix
  • Build and test the changes
  • Verify the fix works as expected
  • Simplify test based on feedback
  • Verify handler types and paths in test

Changes Made

  1. ApiBuilder.java: Added static keyword to 4 methods:

    • wsBefore(@NotNull String path, @NotNull Consumer<WsConfig> wsConfig) (line 402)
    • wsBefore(@NotNull Consumer<WsConfig> wsConfig) (line 410)
    • wsAfter(@NotNull String path, @NotNull Consumer<WsConfig> wsConfig) (line 418)
    • wsAfter(@NotNull Consumer<WsConfig> wsConfig) (line 426)
  2. TestApiBuilderWebSocket.java: Created test that verifies:

    • All four static methods can be called in ApiBuilder
    • Routes are properly registered (5 handlers total)
    • Each handler has correct type (WEBSOCKET_BEFORE, WEBSOCKET, WEBSOCKET_AFTER)
    • Each handler has correct path ("/ws/", "", "/ws/test")

Test Results

  • Test passes ✓
  • Build successful ✓
  • All existing tests pass ✓

Root Cause

The wsBefore and wsAfter methods were missing the static modifier, unlike other similar methods in ApiBuilder like ws, before, and after. This prevented them from being used in the static context of ApiBuilder.

Original prompt

This section details on the original issue you should resolve

<issue_title>Cannot use wsBefore & wsAfter in ApiBuilder</issue_title>
<issue_description>Actual behavior (the bug)
Cannot use wsBefore & wsAfter in ApiBuilder

Expected behavior
Use of wsBefore & wsAfter in ApiBuilder

To Reproduce
Attempt to use wsBefore & wsAfter in ApiBuilder

Additional context
Signatures of wsBefore & wsAfter seem to be missing static

Can't use:

/**
* Adds a WebSocket before handler for the specified path to the {@link Javalin} instance.
* The method can only be called inside a config.routes.apiBuilder(EndpointGroup)}.
*/
public void wsBefore(@NotNull String path, @NotNull Consumer<WsConfig> wsConfig) {
staticInstance().wsBefore(prefixPath(path), wsConfig);
}
/**
* Adds a WebSocket before handler for the current path to the {@link Javalin} instance.
* The method can only be called inside a config.routes.apiBuilder(EndpointGroup)}.
*/
public void wsBefore(@NotNull Consumer<WsConfig> wsConfig) {
staticInstance().wsBefore(prefixPath("*"), wsConfig);
}
/**
* Adds a WebSocket after handler for the specified path to the {@link Javalin} instance.
* The method can only be called inside a config.routes.apiBuilder(EndpointGroup)}.
*/
public void wsAfter(@NotNull String path, @NotNull Consumer<WsConfig> wsConfig) {
staticInstance().wsAfter(prefixPath(path), wsConfig);
}
/**
* Adds a WebSocket after handler for the current path to the {@link Javalin} instance.
* The method can only be called inside a config.routes.apiBuilder(EndpointGroup)}.
*/
public void wsAfter(@NotNull Consumer<WsConfig> wsConfig) {
staticInstance().wsAfter(prefixPath("*"), wsConfig);
}

Can use:

/**
* Adds a WebSocket handler on the current path.
* The method can only be called inside a config.routes.apiBuilder(EndpointGroup)}.
*
* @see <a href="https://javalin.io/documentation#websockets">WebSockets in docs</a>
*/
public static void ws(@NotNull Consumer<WsConfig> ws) {
staticInstance().ws(prefixPath(""), ws);
}
/**
* Adds a WebSocket handler with the given roles for the current path.
* The method can only be called inside a config.routes.apiBuilder(EndpointGroup)}.
*
* @see <a href="https://javalin.io/documentation#websockets">WebSockets in docs</a>
*/
public static void ws(@NotNull Consumer<WsConfig> ws, @NotNull RouteRole... roles) {
staticInstance().ws(prefixPath(""), ws, roles);
}
</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix usage of wsBefore and wsAfter in ApiBuilder Add static keyword to wsBefore and wsAfter in ApiBuilder Nov 14, 2025
Copilot AI requested a review from tipsy November 14, 2025 08:57
@tipsy
Copy link
Member

tipsy commented Nov 14, 2025

@copilot please remove the example, and find a much shorter way to test the api builder. Maybe just assert that the router had the appropriate routes?

Copy link
Contributor Author

Copilot AI commented Nov 14, 2025

@copilot please remove the example, and find a much shorter way to test the api builder. Maybe just assert that the router had the appropriate routes?

Done. Removed the example and simplified the test to just verify that the router has the appropriate routes registered. The test now checks that all 5 handlers (wsBefore with/without path, ws, wsAfter with/without path) are properly registered. (71b71ae)

@tipsy
Copy link
Member

tipsy commented Nov 14, 2025

@copilot great, please verify the actual content though, not just the size.

Copy link
Contributor Author

Copilot AI commented Nov 14, 2025

@copilot great, please verify the actual content though, not just the size.

Updated the test to verify the actual content. Now it checks that each handler has the correct type (WEBSOCKET_BEFORE, WEBSOCKET, WEBSOCKET_AFTER) and correct path ("/ws/", "", "/ws/test"). (3c8ad38)

@codecov
Copy link

codecov bot commented Nov 14, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.48%. Comparing base (d011e39) to head (3c8ad38).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2501      +/-   ##
============================================
+ Coverage     86.29%   86.48%   +0.19%     
- Complexity     1443     1447       +4     
============================================
  Files           151      151              
  Lines          4180     4180              
  Branches        477      477              
============================================
+ Hits           3607     3615       +8     
+ Misses          377      369       -8     
  Partials        196      196              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tipsy tipsy marked this pull request as ready for review November 14, 2025 16:13
@tipsy tipsy merged commit eb163d7 into master Nov 14, 2025
29 checks passed
@tipsy tipsy deleted the copilot/fix-wsbefore-wsafter-usage branch November 14, 2025 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot use wsBefore & wsAfter in ApiBuilder

2 participants