-
Notifications
You must be signed in to change notification settings - Fork 7
TEST: feat: add @Validation.MinItems and @Validation.MaxItems for attachment compositions #752
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
lisajulia
wants to merge
1
commit into
main
Choose a base branch
from
feat/validation-min-max-items
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...m/sap/cds/feature/attachments/handler/applicationservice/ItemsCountValidationHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * © 2024-2026 SAP SE or an SAP affiliate company and cds-feature-attachments contributors. | ||
| */ | ||
| package com.sap.cds.feature.attachments.handler.applicationservice; | ||
|
|
||
| import com.sap.cds.CdsData; | ||
| import com.sap.cds.feature.attachments.handler.common.ItemCountValidationHelper; | ||
| import com.sap.cds.services.cds.ApplicationService; | ||
| import com.sap.cds.services.cds.CdsCreateEventContext; | ||
| import com.sap.cds.services.cds.CdsUpdateEventContext; | ||
| import com.sap.cds.services.handler.EventHandler; | ||
| import com.sap.cds.services.handler.annotations.Before; | ||
| import com.sap.cds.services.handler.annotations.HandlerOrder; | ||
| import com.sap.cds.services.handler.annotations.ServiceName; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * The class {@link ItemsCountValidationHandler} validates {@code @Validation.MinItems} and | ||
| * {@code @Validation.MaxItems} annotations on attachment compositions during CREATE and UPDATE | ||
| * events. In draft mode (entity not yet activated) a warning is issued; on active entities an error | ||
| * is raised. | ||
| */ | ||
| @ServiceName(value = "*", type = ApplicationService.class) | ||
| public class ItemsCountValidationHandler implements EventHandler { | ||
|
|
||
| @Before | ||
| @HandlerOrder(HandlerOrder.LATE) | ||
| void processCreateBefore(CdsCreateEventContext context, List<CdsData> data) { | ||
| boolean isDraft = isDraftData(data); | ||
| ItemCountValidationHelper.validateItemCounts( | ||
| context.getTarget(), data, isDraft, context.getMessages()); | ||
| } | ||
|
|
||
| @Before | ||
| @HandlerOrder(HandlerOrder.LATE) | ||
| void processUpdateBefore(CdsUpdateEventContext context, List<CdsData> data) { | ||
| boolean isDraft = isDraftData(data); | ||
| ItemCountValidationHelper.validateItemCounts( | ||
| context.getTarget(), data, isDraft, context.getMessages()); | ||
| } | ||
|
|
||
| private boolean isDraftData(List<CdsData> data) { | ||
| return data.stream().anyMatch(d -> Boolean.FALSE.equals(d.get("IsActiveEntity"))); | ||
| } | ||
| } | ||
102 changes: 102 additions & 0 deletions
102
...c/main/java/com/sap/cds/feature/attachments/handler/common/ItemCountValidationHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * © 2024-2026 SAP SE or an SAP affiliate company and cds-feature-attachments contributors. | ||
| */ | ||
| package com.sap.cds.feature.attachments.handler.common; | ||
|
|
||
| import com.sap.cds.CdsData; | ||
| import com.sap.cds.reflect.CdsEntity; | ||
| import com.sap.cds.services.messages.Messages; | ||
| import java.util.List; | ||
|
|
||
| public final class ItemCountValidationHelper { | ||
|
|
||
| private static final String MIN_ITEMS_KEY = "attachment_minItems"; | ||
| private static final String MAX_ITEMS_KEY = "attachment_maxItems"; | ||
|
|
||
| public static void validateItemCounts( | ||
| CdsEntity entity, List<? extends CdsData> data, boolean isDraft, Messages messages) { | ||
| entity | ||
| .compositions() | ||
| .forEach( | ||
| composition -> { | ||
| String compositionName = composition.getName(); | ||
|
|
||
| // only validate if the composition was included in the payload | ||
| boolean presentInPayload = | ||
| data.stream().anyMatch(d -> d.containsKey(compositionName)); | ||
| if (!presentInPayload) { | ||
| return; | ||
| } | ||
|
|
||
| long count = | ||
| data.stream() | ||
| .filter(d -> d.containsKey(compositionName)) | ||
| .mapToLong( | ||
| d -> { | ||
| Object val = d.get(compositionName); | ||
| return val instanceof List<?> list ? list.size() : 0L; | ||
| }) | ||
| .sum(); | ||
|
|
||
| composition | ||
| .<Object>findAnnotation("Validation.MinItems") | ||
| .ifPresent( | ||
| annotation -> { | ||
| Object value = annotation.getValue(); | ||
| if (value instanceof Boolean) | ||
| return; // bare annotation without value → skip | ||
| int limit = ((Number) value).intValue(); | ||
| if (count < limit) { | ||
| String msgKey = | ||
| MIN_ITEMS_KEY + "_" + entity.getName() + "_" + compositionName; | ||
| issueMessage( | ||
| isDraft, | ||
| messages, | ||
| msgKey, | ||
| limit, | ||
| entity.getQualifiedName(), | ||
| compositionName); | ||
| } | ||
| }); | ||
|
|
||
| composition | ||
| .<Object>findAnnotation("Validation.MaxItems") | ||
| .ifPresent( | ||
| annotation -> { | ||
| Object value = annotation.getValue(); | ||
| if (value instanceof Boolean) | ||
| return; // bare annotation without value → skip | ||
| int limit = ((Number) value).intValue(); | ||
| if (count > limit) { | ||
| String msgKey = | ||
| MAX_ITEMS_KEY + "_" + entity.getName() + "_" + compositionName; | ||
| issueMessage( | ||
| isDraft, | ||
| messages, | ||
| msgKey, | ||
| limit, | ||
| entity.getQualifiedName(), | ||
| compositionName); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| private static void issueMessage( | ||
| boolean isDraft, | ||
| Messages messages, | ||
| String msgKey, | ||
| int limit, | ||
| String entityQualifiedName, | ||
| String compositionName) { | ||
| if (isDraft) { | ||
| messages.warn(msgKey, limit).target(entityQualifiedName, e -> e.to(compositionName)); | ||
| } else { | ||
| messages.error(msgKey, limit).target(entityQualifiedName, e -> e.to(compositionName)); | ||
| } | ||
| } | ||
|
|
||
| private ItemCountValidationHelper() { | ||
| // avoid instantiation | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...ap/cds/feature/attachments/handler/draftservice/DraftSaveItemsCountValidationHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * © 2024-2026 SAP SE or an SAP affiliate company and cds-feature-attachments contributors. | ||
| */ | ||
| package com.sap.cds.feature.attachments.handler.draftservice; | ||
|
|
||
| import com.sap.cds.CdsData; | ||
| import com.sap.cds.feature.attachments.handler.common.ItemCountValidationHelper; | ||
| import com.sap.cds.services.draft.DraftSaveEventContext; | ||
| import com.sap.cds.services.draft.DraftService; | ||
| import com.sap.cds.services.handler.EventHandler; | ||
| import com.sap.cds.services.handler.annotations.Before; | ||
| import com.sap.cds.services.handler.annotations.HandlerOrder; | ||
| import com.sap.cds.services.handler.annotations.ServiceName; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * The class {@link DraftSaveItemsCountValidationHandler} validates {@code @Validation.MinItems} and | ||
| * {@code @Validation.MaxItems} annotations on attachment compositions when a draft is saved | ||
| * (activated). As draft activation transitions to an active entity, violations always raise errors. | ||
| */ | ||
| @ServiceName(value = "*", type = DraftService.class) | ||
| public class DraftSaveItemsCountValidationHandler implements EventHandler { | ||
|
|
||
| @Before | ||
| @HandlerOrder(HandlerOrder.LATE) | ||
| void processBeforeDraftSave(DraftSaveEventContext context, List<CdsData> data) { | ||
| // isDraft=false: saving a draft to active must enforce the constraint as an error | ||
| ItemCountValidationHelper.validateItemCounts( | ||
| context.getTarget(), data, false, context.getMessages()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic Error:
isDraftdetection usesanyMatch, so a mixed batch of draft and active records is treated as all-draftIf a
datalist contains both draft records (IsActiveEntity=false) and active records (IsActiveEntity=trueor absent),anyMatchreturnstrueand all records in the batch are validated withisDraft=true(warnings only). Active records in the same batch would silently miss the required error.The draft/active distinction should be evaluated per record, not once for the whole list. This is closely related to the per-record aggregation fix in
ItemCountValidationHelper; passingisDraftper row into the helper (or moving theisDraftcheck inside the helper loop) would resolve both issues.Please provide feedback on the review comment by checking the appropriate box: