Add path role inheritance#2553
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2553 +/- ##
============================================
- Coverage 86.39% 86.29% -0.10%
- Complexity 1525 1535 +10
============================================
Files 156 156
Lines 4409 4445 +36
Branches 537 542 +5
============================================
+ Hits 3809 3836 +27
- Misses 366 374 +8
- Partials 234 235 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I'm not sure what Codecov is complaining about—the changes look fine to me. Anyway, here's a usage example of the PR changes. You can still do the following: path("/admin", () -> {
get("/branches", adminRouter::listBranches, Role.ADMIN);
get("/branches/{branch}", adminRouter::getBranch, Role.ADMIN);
patch("/branches/{branch}", adminRouter::updateBranch, Role.ADMIN);
post("/branches/{branch}/upload/{type}", adminRouter::uploadLoader, Role.ADMIN);
});However, you can now optionally do this instead, allowing roles to be inherited from the path("/admin", () -> {
get("/branches", adminRouter::listBranches);
get("/branches/{branch}", adminRouter::getBranch);
patch("/branches/{branch}", adminRouter::updateBranch);
post("/branches/{branch}/upload/{type}", adminRouter::uploadLoader);
}, Role.ADMIN);Makes working with roles much cleaner. |
|
augment review |
🤖 Augment PR SummarySummary: This PR adds role inheritance to Javalin’s static Changes:
Technical Notes: Path/role scope state is managed via ThreadLocal deques and is unwound using 🤖 Was this summary useful? React with 👍 or 👎 |
| * the given roles to all endpoints in the group by default. | ||
| * All paths are normalized, so you can call both | ||
| * path("/path") or path("path") depending on your preference | ||
| */ |
There was a problem hiding this comment.
The group-level roles are merged with any endpoint-provided roles via routeRolesInScope(...), so endpoints can add roles but can’t replace/remove inherited ones. Consider clarifying this merge/inheritance behavior in the Javadoc here to avoid confusion around “override” semantics.
Severity: low
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.
| } | ||
|
|
||
| private static RouteRole[] routeRolesInScope(@NotNull RouteRole... roles) { | ||
| if (routeRoleDeque.get().isEmpty()) { |
There was a problem hiding this comment.
It looks like the routeRoleDeque.get().isEmpty() early-return path in routeRolesInScope(...) may be uncovered (matching the Codecov note). Consider adding a small apiBuilder test that registers a route without any surrounding path(..., roles) and asserts the resulting role set is unchanged/empty as expected.
Severity: low
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.
|
@AoElite wouldn't passing the role before the route groups read nicer? path("/api", Role.AUTHENTICATED) {
path("/admin", Role.ADMIN) {
...
}
}path("/api", {
path("/admin", {
...
}, Role.ADMIN)
}, Role.AUTHENTICATED) |
b661c76 to
338c402
Compare
…date `TestApiBuilderRouteRoles` to include role collections in tests.
|
@tipsy I've made changes to align more on what you wanted. You now specify the roles before the endpoint with a collection. |
|
Thanks @AoElite ! The current approach modifies every HTTP verb method in Also: the PR description says endpoints can "override" inherited roles, but it seems the implementation only accumulates — an endpoint inside a scoped path always gets the parent roles plus its own. The original |
…rage for role clearing and merging logic.
|
Looks good @AoElite, thanks ! |
Bumps `javalin.version` from 7.1.0 to 7.2.0. Updates `io.javalin:javalin-bundle` from 7.1.0 to 7.2.0 Release notes *Sourced from [io.javalin:javalin-bundle's releases](https://github.com/javalin/javalin/releases).* > 7.2.0 > ----- > > What's Changed > -------------- > > * [performance] Replace Stream API with List and reduce per-request allocations by [`@tipsy`](https://github.com/tipsy) in [javalin/javalin#2567](https://redirect.github.com/javalin/javalin/pull/2567) > * feat: introduce `JavalinJackson3` as an optional `JsonMapper` implementation for Jackson 3 by [`@yvasyliev`](https://github.com/yvasyliev) in [javalin/javalin#2548](https://redirect.github.com/javalin/javalin/pull/2548) > * [workflow]: Bump codecov/codecov-action from 5.5.2 to 5.5.3 in the dependencies group by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2569](https://redirect.github.com/javalin/javalin/pull/2569) > * Add path role inheritance by [`@AoElite`](https://github.com/AoElite) in [javalin/javalin#2553](https://redirect.github.com/javalin/javalin/pull/2553) > * [deps]: Bump the dependencies group across 1 directory with 23 updates by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2575](https://redirect.github.com/javalin/javalin/pull/2575) > * [workflow]: Bump codecov/codecov-action from 5.5.3 to 6.0.0 in the dependencies group across 1 directory by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2570](https://redirect.github.com/javalin/javalin/pull/2570) > * [workflow]: Bump actions/github-script from 8 to 9 in the dependencies group by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2581](https://redirect.github.com/javalin/javalin/pull/2581) > * [cleanup] Remove JavalinTest.class by [`@vorburger`](https://github.com/vorburger) in [javalin/javalin#2584](https://redirect.github.com/javalin/javalin/pull/2584) > * [deps] Bump Jetty from 12.1.7 to 12.1.8 by [`@vorburger`](https://github.com/vorburger) in [javalin/javalin#2585](https://redirect.github.com/javalin/javalin/pull/2585) > * Add ability to emit plain data messages in SSE by [`@1cg`](https://github.com/1cg) in [javalin/javalin#2578](https://redirect.github.com/javalin/javalin/pull/2578) > * fix: sanitize newlines in SSE event and id fields by [`@eddieran`](https://github.com/eddieran) in [javalin/javalin#2580](https://redirect.github.com/javalin/javalin/pull/2580) > * [deps] Bump stable deps and sync OptionalDependency.kt by [`@tipsy`](https://github.com/tipsy) in [javalin/javalin#2588](https://redirect.github.com/javalin/javalin/pull/2588) > > New Contributors > ---------------- > > * [`@AoElite`](https://github.com/AoElite) made their first contribution in [javalin/javalin#2553](https://redirect.github.com/javalin/javalin/pull/2553) > * [`@vorburger`](https://github.com/vorburger) made their first contribution in [javalin/javalin#2584](https://redirect.github.com/javalin/javalin/pull/2584) > * [`@1cg`](https://github.com/1cg) made their first contribution in [javalin/javalin#2578](https://redirect.github.com/javalin/javalin/pull/2578) > * [`@eddieran`](https://github.com/eddieran) made their first contribution in [javalin/javalin#2580](https://redirect.github.com/javalin/javalin/pull/2580) > > **Full Changelog**: <javalin/javalin@javalin-parent-7.1.0...javalin-parent-7.2.0> Commits * [`c67b118`](javalin/javalin@c67b118) [maven-release-plugin] prepare release javalin-parent-7.2.0 * [`b89fdf7`](javalin/javalin@b89fdf7) [deps] Bump stable deps and sync OptionalDependency.kt ([#2588](https://redirect.github.com/javalin/javalin/issues/2588)) * [`a3ad657`](javalin/javalin@a3ad657) [sse] Sanitize newlines in event and id fields * [`e0f5458`](javalin/javalin@e0f5458) [sse] Add ability to emit plain data messages * [`fa51869`](javalin/javalin@fa51869) [deps] Bump Jetty from 12.1.7 to 12.1.8 ([#2585](https://redirect.github.com/javalin/javalin/issues/2585)) * [`4bc70e9`](javalin/javalin@4bc70e9) [cleanup] Remove JavalinTest.class ([#2584](https://redirect.github.com/javalin/javalin/issues/2584)) * [`1901feb`](javalin/javalin@1901feb) [workflow]: Bump actions/github-script in the dependencies group ([#2581](https://redirect.github.com/javalin/javalin/issues/2581)) * [`152f7b3`](javalin/javalin@152f7b3) [workflow]: Bump codecov/codecov-action in the dependencies group ([#2570](https://redirect.github.com/javalin/javalin/issues/2570)) * [`2c4d1ef`](javalin/javalin@2c4d1ef) [deps]: Bump the dependencies group across 1 directory with 23 updates ([#2575](https://redirect.github.com/javalin/javalin/issues/2575)) * [`64f3a75`](javalin/javalin@64f3a75) [apibuilder] Refactor role-scoping internals to Kotlin * Additional commits viewable in [compare view](javalin/javalin@7.1.0...javalin-parent-7.2.0) Updates `io.javalin.community.openapi:javalin-openapi-plugin` from 7.1.0 to 7.2.0 Release notes *Sourced from [io.javalin.community.openapi:javalin-openapi-plugin's releases](https://github.com/javalin/javalin-openapi/releases).* > 7.2.0 > ----- > > **Changes** > > * Bumped Javalin to 7.2.0 > > **Sponsors** > Thanks to everyone who supported me this month 💜 > > **Minimal requirements** > > * Java 17+ / Kotlin 2.3+ > * Javalin 7.2.0 > > 7.1.1-rc.1 > ---------- > > **Changes** > > * [javalin/javalin-openapi#275](https://redirect.github.com/javalin/javalin-openapi/issues/275) [Make ClassLoader used for loading resources configurable](javalin/javalin-openapi@de1b6b7) > > **Sponsors** > Thanks to everyone who supported me this month 💜 ... (truncated) Commits * [`123e727`](javalin/javalin-openapi@123e727) [GH-279](https://redirect.github.com/javalin/javalin-openapi/issues/279) Adjust to Javalin's breaking changed in InternalRouter * [`3ea0e98`](javalin/javalin-openapi@3ea0e98) [GH-279](https://redirect.github.com/javalin/javalin-openapi/issues/279) Release 7.2.0 (Resolves [#279](https://redirect.github.com/javalin/javalin-openapi/issues/279)) * [`0471f8a`](javalin/javalin-openapi@0471f8a) [GH-277](https://redirect.github.com/javalin/javalin-openapi/issues/277) Release 7.1.1-rc.2 * [`3831f78`](javalin/javalin-openapi@3831f78) [GH-277](https://redirect.github.com/javalin/javalin-openapi/issues/277) Cover handling of complex types in query parameters (Resolves [#277](https://redirect.github.com/javalin/javalin-openapi/issues/277)) * See full diff in [compare view](javalin/javalin-openapi@7.1.0...7.2.0) Updates `io.javalin.community.openapi:javalin-swagger-plugin` from 7.1.0 to 7.2.0 Release notes *Sourced from [io.javalin.community.openapi:javalin-swagger-plugin's releases](https://github.com/javalin/javalin-openapi/releases).* > 7.2.0 > ----- > > **Changes** > > * Bumped Javalin to 7.2.0 > > **Sponsors** > Thanks to everyone who supported me this month 💜 > > **Minimal requirements** > > * Java 17+ / Kotlin 2.3+ > * Javalin 7.2.0 > > 7.1.1-rc.1 > ---------- > > **Changes** > > * [javalin/javalin-openapi#275](https://redirect.github.com/javalin/javalin-openapi/issues/275) [Make ClassLoader used for loading resources configurable](javalin/javalin-openapi@de1b6b7) > > **Sponsors** > Thanks to everyone who supported me this month 💜 ... (truncated) Commits * [`123e727`](javalin/javalin-openapi@123e727) [GH-279](https://redirect.github.com/javalin/javalin-openapi/issues/279) Adjust to Javalin's breaking changed in InternalRouter * [`3ea0e98`](javalin/javalin-openapi@3ea0e98) [GH-279](https://redirect.github.com/javalin/javalin-openapi/issues/279) Release 7.2.0 (Resolves [#279](https://redirect.github.com/javalin/javalin-openapi/issues/279)) * [`0471f8a`](javalin/javalin-openapi@0471f8a) [GH-277](https://redirect.github.com/javalin/javalin-openapi/issues/277) Release 7.1.1-rc.2 * [`3831f78`](javalin/javalin-openapi@3831f78) [GH-277](https://redirect.github.com/javalin/javalin-openapi/issues/277) Cover handling of complex types in query parameters (Resolves [#277](https://redirect.github.com/javalin/javalin-openapi/issues/277)) * See full diff in [compare view](javalin/javalin-openapi@7.1.0...7.2.0) Updates `io.javalin:javalin-micrometer` from 7.1.0 to 7.2.0 Release notes *Sourced from [io.javalin:javalin-micrometer's releases](https://github.com/javalin/javalin/releases).* > 7.2.0 > ----- > > What's Changed > -------------- > > * [performance] Replace Stream API with List and reduce per-request allocations by [`@tipsy`](https://github.com/tipsy) in [javalin/javalin#2567](https://redirect.github.com/javalin/javalin/pull/2567) > * feat: introduce `JavalinJackson3` as an optional `JsonMapper` implementation for Jackson 3 by [`@yvasyliev`](https://github.com/yvasyliev) in [javalin/javalin#2548](https://redirect.github.com/javalin/javalin/pull/2548) > * [workflow]: Bump codecov/codecov-action from 5.5.2 to 5.5.3 in the dependencies group by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2569](https://redirect.github.com/javalin/javalin/pull/2569) > * Add path role inheritance by [`@AoElite`](https://github.com/AoElite) in [javalin/javalin#2553](https://redirect.github.com/javalin/javalin/pull/2553) > * [deps]: Bump the dependencies group across 1 directory with 23 updates by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2575](https://redirect.github.com/javalin/javalin/pull/2575) > * [workflow]: Bump codecov/codecov-action from 5.5.3 to 6.0.0 in the dependencies group across 1 directory by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2570](https://redirect.github.com/javalin/javalin/pull/2570) > * [workflow]: Bump actions/github-script from 8 to 9 in the dependencies group by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2581](https://redirect.github.com/javalin/javalin/pull/2581) > * [cleanup] Remove JavalinTest.class by [`@vorburger`](https://github.com/vorburger) in [javalin/javalin#2584](https://redirect.github.com/javalin/javalin/pull/2584) > * [deps] Bump Jetty from 12.1.7 to 12.1.8 by [`@vorburger`](https://github.com/vorburger) in [javalin/javalin#2585](https://redirect.github.com/javalin/javalin/pull/2585) > * Add ability to emit plain data messages in SSE by [`@1cg`](https://github.com/1cg) in [javalin/javalin#2578](https://redirect.github.com/javalin/javalin/pull/2578) > * fix: sanitize newlines in SSE event and id fields by [`@eddieran`](https://github.com/eddieran) in [javalin/javalin#2580](https://redirect.github.com/javalin/javalin/pull/2580) > * [deps] Bump stable deps and sync OptionalDependency.kt by [`@tipsy`](https://github.com/tipsy) in [javalin/javalin#2588](https://redirect.github.com/javalin/javalin/pull/2588) > > New Contributors > ---------------- > > * [`@AoElite`](https://github.com/AoElite) made their first contribution in [javalin/javalin#2553](https://redirect.github.com/javalin/javalin/pull/2553) > * [`@vorburger`](https://github.com/vorburger) made their first contribution in [javalin/javalin#2584](https://redirect.github.com/javalin/javalin/pull/2584) > * [`@1cg`](https://github.com/1cg) made their first contribution in [javalin/javalin#2578](https://redirect.github.com/javalin/javalin/pull/2578) > * [`@eddieran`](https://github.com/eddieran) made their first contribution in [javalin/javalin#2580](https://redirect.github.com/javalin/javalin/pull/2580) > > **Full Changelog**: <javalin/javalin@javalin-parent-7.1.0...javalin-parent-7.2.0> Commits * [`c67b118`](javalin/javalin@c67b118) [maven-release-plugin] prepare release javalin-parent-7.2.0 * [`b89fdf7`](javalin/javalin@b89fdf7) [deps] Bump stable deps and sync OptionalDependency.kt ([#2588](https://redirect.github.com/javalin/javalin/issues/2588)) * [`a3ad657`](javalin/javalin@a3ad657) [sse] Sanitize newlines in event and id fields * [`e0f5458`](javalin/javalin@e0f5458) [sse] Add ability to emit plain data messages * [`fa51869`](javalin/javalin@fa51869) [deps] Bump Jetty from 12.1.7 to 12.1.8 ([#2585](https://redirect.github.com/javalin/javalin/issues/2585)) * [`4bc70e9`](javalin/javalin@4bc70e9) [cleanup] Remove JavalinTest.class ([#2584](https://redirect.github.com/javalin/javalin/issues/2584)) * [`1901feb`](javalin/javalin@1901feb) [workflow]: Bump actions/github-script in the dependencies group ([#2581](https://redirect.github.com/javalin/javalin/issues/2581)) * [`152f7b3`](javalin/javalin@152f7b3) [workflow]: Bump codecov/codecov-action in the dependencies group ([#2570](https://redirect.github.com/javalin/javalin/issues/2570)) * [`2c4d1ef`](javalin/javalin@2c4d1ef) [deps]: Bump the dependencies group across 1 directory with 23 updates ([#2575](https://redirect.github.com/javalin/javalin/issues/2575)) * [`64f3a75`](javalin/javalin@64f3a75) [apibuilder] Refactor role-scoping internals to Kotlin * Additional commits viewable in [compare view](javalin/javalin@7.1.0...javalin-parent-7.2.0) Updates `io.javalin:javalin-testtools` from 7.1.0 to 7.2.0 Release notes *Sourced from [io.javalin:javalin-testtools's releases](https://github.com/javalin/javalin/releases).* > 7.2.0 > ----- > > What's Changed > -------------- > > * [performance] Replace Stream API with List and reduce per-request allocations by [`@tipsy`](https://github.com/tipsy) in [javalin/javalin#2567](https://redirect.github.com/javalin/javalin/pull/2567) > * feat: introduce `JavalinJackson3` as an optional `JsonMapper` implementation for Jackson 3 by [`@yvasyliev`](https://github.com/yvasyliev) in [javalin/javalin#2548](https://redirect.github.com/javalin/javalin/pull/2548) > * [workflow]: Bump codecov/codecov-action from 5.5.2 to 5.5.3 in the dependencies group by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2569](https://redirect.github.com/javalin/javalin/pull/2569) > * Add path role inheritance by [`@AoElite`](https://github.com/AoElite) in [javalin/javalin#2553](https://redirect.github.com/javalin/javalin/pull/2553) > * [deps]: Bump the dependencies group across 1 directory with 23 updates by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2575](https://redirect.github.com/javalin/javalin/pull/2575) > * [workflow]: Bump codecov/codecov-action from 5.5.3 to 6.0.0 in the dependencies group across 1 directory by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2570](https://redirect.github.com/javalin/javalin/pull/2570) > * [workflow]: Bump actions/github-script from 8 to 9 in the dependencies group by [`@dependabot`](https://github.com/dependabot)[bot] in [javalin/javalin#2581](https://redirect.github.com/javalin/javalin/pull/2581) > * [cleanup] Remove JavalinTest.class by [`@vorburger`](https://github.com/vorburger) in [javalin/javalin#2584](https://redirect.github.com/javalin/javalin/pull/2584) > * [deps] Bump Jetty from 12.1.7 to 12.1.8 by [`@vorburger`](https://github.com/vorburger) in [javalin/javalin#2585](https://redirect.github.com/javalin/javalin/pull/2585) > * Add ability to emit plain data messages in SSE by [`@1cg`](https://github.com/1cg) in [javalin/javalin#2578](https://redirect.github.com/javalin/javalin/pull/2578) > * fix: sanitize newlines in SSE event and id fields by [`@eddieran`](https://github.com/eddieran) in [javalin/javalin#2580](https://redirect.github.com/javalin/javalin/pull/2580) > * [deps] Bump stable deps and sync OptionalDependency.kt by [`@tipsy`](https://github.com/tipsy) in [javalin/javalin#2588](https://redirect.github.com/javalin/javalin/pull/2588) > > New Contributors > ---------------- > > * [`@AoElite`](https://github.com/AoElite) made their first contribution in [javalin/javalin#2553](https://redirect.github.com/javalin/javalin/pull/2553) > * [`@vorburger`](https://github.com/vorburger) made their first contribution in [javalin/javalin#2584](https://redirect.github.com/javalin/javalin/pull/2584) > * [`@1cg`](https://github.com/1cg) made their first contribution in [javalin/javalin#2578](https://redirect.github.com/javalin/javalin/pull/2578) > * [`@eddieran`](https://github.com/eddieran) made their first contribution in [javalin/javalin#2580](https://redirect.github.com/javalin/javalin/pull/2580) > > **Full Changelog**: <javalin/javalin@javalin-parent-7.1.0...javalin-parent-7.2.0> Commits * [`c67b118`](javalin/javalin@c67b118) [maven-release-plugin] prepare release javalin-parent-7.2.0 * [`b89fdf7`](javalin/javalin@b89fdf7) [deps] Bump stable deps and sync OptionalDependency.kt ([#2588](https://redirect.github.com/javalin/javalin/issues/2588)) * [`a3ad657`](javalin/javalin@a3ad657) [sse] Sanitize newlines in event and id fields * [`e0f5458`](javalin/javalin@e0f5458) [sse] Add ability to emit plain data messages * [`fa51869`](javalin/javalin@fa51869) [deps] Bump Jetty from 12.1.7 to 12.1.8 ([#2585](https://redirect.github.com/javalin/javalin/issues/2585)) * [`4bc70e9`](javalin/javalin@4bc70e9) [cleanup] Remove JavalinTest.class ([#2584](https://redirect.github.com/javalin/javalin/issues/2584)) * [`1901feb`](javalin/javalin@1901feb) [workflow]: Bump actions/github-script in the dependencies group ([#2581](https://redirect.github.com/javalin/javalin/issues/2581)) * [`152f7b3`](javalin/javalin@152f7b3) [workflow]: Bump codecov/codecov-action in the dependencies group ([#2570](https://redirect.github.com/javalin/javalin/issues/2570)) * [`2c4d1ef`](javalin/javalin@2c4d1ef) [deps]: Bump the dependencies group across 1 directory with 23 updates ([#2575](https://redirect.github.com/javalin/javalin/issues/2575)) * [`64f3a75`](javalin/javalin@64f3a75) [apibuilder] Refactor role-scoping internals to Kotlin * Additional commits viewable in [compare view](javalin/javalin@7.1.0...javalin-parent-7.2.0)
This pull request enhances the
ApiBuilderin Javalin to support defining default route roles for endpoint groups. The main improvement is the ability to specify roles at the group (path) level, which are then automatically applied to all endpoints defined within that group, unless overridden. This change streamlines role management for grouped routes and reduces repetitive code.Endpoint group role management:
pathmethod that accepts aRouteRole... rolesparameter, allowing default roles to be set for all endpoints within a group. Internally, a newrouteRoleDequeis used to manage role context for nested groups. [1] [2]Automatic role propagation to handlers:
get,post,put,patch,delete,head,query), so that if roles are not explicitly provided, the roles in the current group context are automatically applied. This is achieved by calling a newrouteRolesInScope()helper.These changes make it easier and less error-prone to manage security roles for grouped endpoints in Javalin applications.