Skip to content

TEST: Add @Validation.MinItems / @Validation.MaxItems support for attachmen…#753

Open
lisajulia wants to merge 1 commit intomainfrom
feat/validation-min-max-items-one
Open

TEST: Add @Validation.MinItems / @Validation.MaxItems support for attachmen…#753
lisajulia wants to merge 1 commit intomainfrom
feat/validation-min-max-items-one

Conversation

@lisajulia
Copy link
Contributor

…t compositions

Introduces ItemCountValidator which enforces minimum and maximum attachment counts on composition fields annotated with @Validation.MinItems and/or @Validation.MaxItems.

  • Annotation values can be integer literals, parseable strings, or property references resolved at runtime from the entity payload (e.g. stock)
  • Active create/update events: emit an error and throw immediately
  • Draft patch events: emit a warning only (draft tolerates invalid state)
  • Draft save (activate): query the draft DB for the real attachment count, then validate as an error — skip the in-payload check in Create/Update handlers during draft activate to avoid double-validation
  • i18n keys follow the Fiori elements override pattern: AttachmentMinItems[_EntityName_propertyName] / AttachmentMaxItems[_EntityName_propertyName]

…t compositions

Introduces `ItemCountValidator` which enforces minimum and maximum attachment
counts on composition fields annotated with `@Validation.MinItems` and/or
`@Validation.MaxItems`.

- Annotation values can be integer literals, parseable strings, or property
  references resolved at runtime from the entity payload (e.g. `stock`)
- Active create/update events: emit an error and throw immediately
- Draft patch events: emit a warning only (draft tolerates invalid state)
- Draft save (activate): query the draft DB for the real attachment count,
  then validate as an error — skip the in-payload check in Create/Update
  handlers during draft activate to avoid double-validation
- i18n keys follow the Fiori elements override pattern:
  `AttachmentMinItems[_EntityName_propertyName]` /
  `AttachmentMaxItems[_EntityName_propertyName]`
@hyperspace-insights
Copy link
Contributor

Summary

The following content is AI-generated and provides a summary of the pull request:


Add @Validation.MinItems / @Validation.MaxItems Support for Attachment Compositions

New Feature

✨ Introduces ItemCountValidator which enforces minimum and maximum attachment counts on composition fields annotated with @Validation.MinItems and/or @Validation.MaxItems.

Key behaviors:

  • Annotation values can be integer literals, parseable strings, or property references resolved at runtime from the entity payload (e.g., stock)
  • Active create/update events: emit an error and throw immediately on violation
  • Draft patch events: emit a warning only (draft tolerates invalid state)
  • Draft save (activate): query the draft DB for the actual attachment count, then validate as an error — skipping the in-payload check in Create/Update handlers to avoid double-validation
  • i18n keys follow the Fiori elements override pattern: AttachmentMinItems[_EntityName_propertyName] / AttachmentMaxItems[_EntityName_propertyName]

Changes

  • ItemCountValidator.java (new): Core validation logic. Reads @Validation.MinItems/@Validation.MaxItems annotations on composition elements, resolves integer threshold values (direct, string, or property reference), counts attachments in the payload, and emits warnings (draft) or errors (active) accordingly.

  • DraftActiveAttachmentsHandler.java: Updated constructor to accept PersistenceService. Added @Before handler validateItemCountBeforeSave that queries the draft DB for actual attachment counts and validates them as errors before draft activation.

  • CreateAttachmentsHandler.java / UpdateAttachmentsHandler.java: Integrated ItemCountValidator.validate() calls in @Before handlers, skipping validation during draft activate (when storageReader.get() is true).

  • DraftPatchAttachmentsHandler.java: Added draft-mode item count validation (warnings only) after processing attachment data.

  • Registration.java: Updated DraftActiveAttachmentsHandler instantiation to pass persistenceService.

  • i18n.properties: Added base i18n message keys AttachmentMinItems and AttachmentMaxItems with placeholder support for current count and limit.

  • ItemCountValidatorTest.java (new): Comprehensive unit tests covering annotation detection, value resolution (numeric, string, property reference), warning/error behavior, boundary conditions, and no-op behavior for unannotated entities.

  • DraftActiveAttachmentsHandlerTest.java: Extended with tests for validateItemCountBeforeSave covering entities with/without annotations, within-bounds, too-few, and too-many scenarios.

  • db-model.cds (test) / data-model.cds (integration tests): Added minMaxAttachments, minAttachments, maxAttachments, propertyRefAttachments compositions and stock integer field to test entities; added corresponding @Validation.MinItems/@Validation.MaxItems annotations.


  • 🔄 Regenerate and Update Summary
  • ✏️ Insert as PR Description (deletes this comment)
  • 🗑️ Delete comment
PR Bot Information

Version: 1.18.5 | 📖 Documentation | 🚨 Create Incident | 💬 Feedback

  • Correlation ID: aceed380-1edb-11f1-8807-c25194fb18a7
  • Summary Prompt: Default Prompt
  • Event Trigger: pull_request.opened
  • Output Template: Default Template
  • LLM: anthropic--claude-4.6-sonnet

💌 Have ideas or want to contribute? Create an issue and share your thoughts with us!
📑 Check out the documentation for more information.
📬 Subscribe to the Hyperspace PR Bot DL to get the latest announcements and pilot features!

Made with ❤️ by Hyperspace.

Copy link
Contributor

@hyperspace-insights hyperspace-insights bot left a comment

Choose a reason for hiding this comment

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

The PR introduces a well-structured ItemCountValidator feature, but has several logic defects that need attention: most critically, throwIfError() is called per-composition rather than after all compositions are validated (causing partial violation reporting), the UpdateAttachmentsHandler gates count validation inside a condition that can be false even when the composition is present in the payload, and draft-activate replaces real DB items with empty placeholders which would silently break property-reference constraints. Additionally, property-reference resolution only reads from the first data row, which is incorrect for multi-root payloads.

PR Bot Information

Version: 1.18.5 | 📖 Documentation | 🚨 Create Incident | 💬 Feedback

  • LLM: anthropic--claude-4.6-sonnet
  • Correlation ID: aceed380-1edb-11f1-8807-c25194fb18a7
  • Event Trigger: pull_request.opened


// Try resolving as a property reference on the first data item
if (!data.isEmpty() && !strValue.isEmpty()) {
Object propValue = data.get(0).get(strValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

Logic Error: Property-reference resolution only looks at the first data item

When multiple root rows are passed in data, the property reference (e.g. stock) is always read from data.get(0). If each root row can have a different stock value the constraint for rows 1..N is evaluated using the wrong threshold. The count across all rows is summed by countInPayload, but the limit only comes from row 0.

Consider resolving the property reference per root row and validating each row's composition count against that row's limit, or document that only single-root payloads are supported.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful

@lisajulia lisajulia force-pushed the feat/validation-min-max-items-one branch from 45611e6 to 32ac7aa Compare March 13, 2026 13:46
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.

1 participant