Skip to content

Mrc-6394 Fix issue of creating name with spaces #190

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

absternator
Copy link
Contributor

@absternator absternator commented Apr 8, 2025

issue with create URI should be using role.id not role.name

@absternator absternator requested a review from EmmaLRussell April 8, 2025 10:04
Copy link

codecov bot commented Apr 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.15%. Comparing base (32587f7) to head (cc1f2e1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #190   +/-   ##
=======================================
  Coverage   97.15%   97.15%           
=======================================
  Files         150      150           
  Lines        1476     1476           
  Branches      425      425           
=======================================
  Hits         1434     1434           
  Misses         41       41           
  Partials        1        1           

☔ 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.

@@ -24,7 +24,7 @@ class RoleController(private val roleService: RoleService, private val userRoleS
{
val role = roleService.createRole(createRole)

return ResponseEntity.created(URI.create("/roles/${role.name}")).body(role.toDto())
return ResponseEntity.created(URI.create("/roles/${role.id}")).body(role.toDto())
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused by this. The getRoleByName endpoint requires a name, not a numerical ID. How does this fix anything? Why do spaces actually cause an issue? Do we need to add a role "slug"? Should we ban spaces from role names?

Copy link
Member

@plietar plietar Apr 8, 2025

Choose a reason for hiding this comment

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

I think what you want is something like:

UriComponentsBuilder.fromUriString("/roles/{}").build(role.name)

In general doing naive string interpolation anywhere involving URLs is a bad idea.

That will properly encode the argument into the URI string. Spaces in the name should become %20 and slashes should become %2F (and we should make sure requests to /roles/foo%2fbar actually works the way we want it to).

Or we could just not return any URI here. I don't think the frontend uses it anyway.

EDIT: turns out we disallow slashes in names, except we only enforce that in the frontend, not the backend. Doing a GET of /roles/foo%2fbar is interpreted as /roles/foo/bar, which doesn't match any route (I'm not 100% sure if the %2f -> / translation is done by Spring or nginx). You can create such a role with a POST /role, but you cannot access it or even delete it since DELETE /roles/foo/bar will not work. I had to manually delete the row from the database.

I think it would be nice to put some enforcement of the name restrictions in the backend instead/in addition to the FE (for non-username role names, username role names and user names).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ohh yeah i thought we had /role/{id} endpoint... but you are right and we don't use URL anways so have removed... also good point with BE validation too so have added that

@absternator absternator requested a review from plietar April 9, 2025 08:08
import org.jetbrains.annotations.NotNull

data class CreateRole(
@field:NotNull
@field:Pattern(
regexp = "^[a-zA-Z0-9]+(\\s[a-zA-Z0-9]+)*\$",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same regex as FE

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.

2 participants