Fix/5109 debounce metadata field keystroke
References
- Fixes #5109
Description
Debounces metadata field name input and gates findDsoFieldVocabulary() behind validateMetadataField() to prevent 422 errors being triggered on every keystroke when editing item metadata.
Instructions for Reviewers
Root Cause
DsoEditMetadataValueComponent#ngOnChanges reassigned fieldType$ on every input change, immediately invoking findDsoFieldVocabulary() with no debounce. The isNotEmpty() guard in DsoEditMetadataFieldService passed for any non-empty string including partial inputs like dc.re, causing byMetadataAndCollection requests that resulted in 422 errors. Since the 422 propagates through the full Spring Security filter chain before reaching the controller layer, no @ExceptionHandler catches it — which is why the stack traces are so verbose.
Reproduced and confirmed on DSpace 9.1, DSpace 10.0 and DSpace 11.0-SNAPSHOT.
List of changes
dso-edit-metadata-value.component.ts: MovedfieldType$initialization tongOnInitusing the existing_mdField$BehaviorSubject with a reactive pipeline:debounceTimeWorkaround(300)+distinctUntilChanged()+ regex guard +validateMetadataField()before callingfindDsoFieldVocabulary(). Replacedincludes('.')with the same regex guard inngOnChanges. Removed unusedgetFieldType()method.dso-edit-metadata-field.service.ts: AddedVALID_METADATA_FIELD_PATTERNregex as a defensive guard infindDsoFieldVocabulary(), rejecting inputs that don't matchschema.elementorschema.element.qualifierformat before making any HTTP request.dso-edit-metadata-value.component.spec.ts: Added 10 new unit tests for thefieldType$pipeline usingfakeAsync/tick(300), consistent with the project'sdebounceTimeWorkaroundtesting approach.
Before
Typing dc.contributor.author (20 chars) generated ~40 requests and thousands of log lines per session, e.g.:
[422] dc.c is not a valid metadata
[422] dc.co is not a valid metadata
[422] dc.con is not a valid metadata
[422] dc.cont is not a valid metadata
... (stack trace repeating for each keystroke)
After
Typing dc.contributor.author generates exactly 1 request to byMetadataAndCollection, fired only after the field is confirmed valid:
GET /server/api/core/metadatafields/search/byFieldName ← field confirmed valid
GET /server/api/submission/vocabularies/search/byMetadataAndCollection ← 200 OK
How to Test
- Start DSpace backend and frontend
- Navigate to an item's edit metadata page:
/items/{uuid}/edit/metadata - Click "+ Add"
- Slowly type
dc.contributor.authorin the metadata field name input, pausing between keystrokes - Monitor backend logs:
docker logs -f dspace | grep -E "422|byMetadataAndCollection"
Expected result: No 422 errors appear in the logs during typing. A single byMetadataAndCollection request fires only after the complete field name has been confirmed as valid.
Checklist
- [x] My PR is created against the
mainbranch - [x] My PR is small in size (3 files modified)
- [ ] My PR passes ESLint —
npm run lintfails with a pre-existing error unrelated to this fix (src/themes/custom/eager-theme-components.ts). ESLint was run directly on the 3 modified files and passes without errors. - [x] My PR doesn't introduce circular dependencies (verified via
npx madge --circularon the affected directory) - [x] My PR includes TypeDoc comments for modified methods
- [x] My PR passes all specs/tests and includes new/updated specs
- [x] My PR aligns with Accessibility guidelines (no UI changes)
- [x] My PR uses i18n keys (no hardcoded English text added)
- [x] My PR includes details on how to test it
The failing test (AuthService > should return false when token is not expired) is unrelated to this PR. All tests related to the changes introduced in this PR pass successfully on both Node 20.x and 22.x.
Thank you for submitting this fix @shernandez-dev! Adding to the 11.0 project board for volunteer reviewers to take a look.