Skip to content

Optimize mimetype validation to only run when annotation exists#761

Open
Schmarvinius wants to merge 3 commits intomainfrom
optimize-mimetype-validation-early-exit
Open

Optimize mimetype validation to only run when annotation exists#761
Schmarvinius wants to merge 3 commits intomainfrom
optimize-mimetype-validation-early-exit

Conversation

@Schmarvinius
Copy link
Collaborator

@Schmarvinius Schmarvinius commented Mar 22, 2026

Optimize MIME Type Validation to Short-Circuit When No Annotation Exists

Refactor

♻️ Refactored the MIME type validation logic in AttachmentValidationHelper to short-circuit early when no media entity has the @Core.AcceptableMediaTypes annotation, avoiding unnecessary data extraction and MIME resolution overhead.

Changes

  • AttachmentValidationHelper.java: Moved the allowedTypesByElementName resolution (via MediaTypeResolver) to occur before the data extraction step. If the resulting map is empty (i.e., no entity has the annotation), the method returns immediately — skipping the AttachmentDataExtractor call entirely. Also updated findInvalidFilesByElementName to skip unconstrained elements by using map.get() instead of getOrDefault() with a wildcard fallback.

  • MediaTypeResolver.java: Changed fetchAcceptableMediaTypes to return Optional<List<String>> instead of List<String>. Entities without the @Core.AcceptableMediaTypes annotation are now excluded from the result map (via ifPresent), so the map accurately reflects only annotated entities.

  • AttachmentValidationHelperTest.java: Added a new test case doesNothing_whenNoEntityHasAcceptableMediaTypesAnnotation that verifies the early-return behavior — when MediaTypeResolver returns an empty map, AttachmentDataExtractor is never invoked.

  • MediaTypeResolverTest.java: Renamed and corrected the test shouldExcludeEntityWithoutAnnotation (previously shouldResolveMediaTypesUsingCascader) to assert that entities without the annotation are excluded from the result map rather than included with a wildcard fallback.

  • 🔄 Regenerate and Update Summary

📬 Subscribe to the Hyperspace PR Bot DL to get the latest announcements and pilot features!

PR Bot Information

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

  • Correlation ID: dbc62480-25ff-11f1-8e0c-572de37a2b5d
  • Output Template: Default Template
  • Event Trigger: pull_request.edited
  • Summary Prompt: Default Prompt
  • LLM: anthropic--claude-4.6-sonnet

@Schmarvinius Schmarvinius self-assigned this Mar 22, 2026
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 pull request introduces a correctness issue: the new early-return optimisation relies on allowedTypesByElementName.isEmpty(), but MediaTypeResolver always populates the map (falling back to WILDCARD_MEDIA_TYPE for unannotated entities), so the guard can never trigger. The companion test masks this by fully mocking MediaTypeResolver to return Map.of(), giving false confidence that the optimisation works end-to-end. Both issues need to be addressed together — either by changing MediaTypeResolver to exclude unannotated entities from the result, or by using a different signal to detect the "no annotation present" case.

PR Bot Information

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

  • Event Trigger: pull_request.opened
  • Correlation ID: d7e27720-25fe-11f1-89a4-6b40a77c1aec
  • LLM: anthropic--claude-4.6-sonnet

MediaTypeResolver.fetchAcceptableMediaTypes() now returns Optional
and only populates the map for entities that have the
@Core.AcceptableMediaTypes annotation. This makes the isEmpty()
early-return guard in AttachmentValidationHelper effective.

Also adjusts findInvalidFilesByElementName to skip unconstrained
elements instead of defaulting to wildcard.
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 overall quality of this PR is good — the optimization is logically sound and the test coverage is adequate. The one comment I posted about verifyNoInteractions() scope was incorrect (the call is already inside the try block); no real defects were identified. The only minor concern is that WILDCARD_MEDIA_TYPE becomes effectively dead code in the error-message fallback path after this refactor, but this is a minor maintainability note rather than a correctness issue.

PR Bot Information

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

  • Correlation ID: d967c220-25ff-11f1-93dd-2d62b4339b3e
  • LLM: anthropic--claude-4.6-sonnet
  • Event Trigger: issue_comment.created

@hyperspace-insights hyperspace-insights bot deleted a comment from Schmarvinius Mar 22, 2026
After the refactor, invalidFilesByElement can only contain keys
that exist in acceptableMediaTypesByElementName, making the
getOrDefault fallback to WILDCARD_MEDIA_TYPE unreachable. Replace
with direct get() and remove the unused constant.
@Schmarvinius Schmarvinius requested a review from a team March 22, 2026 15:48
(elementName, files) -> {
// Resolve the allowed media types for this field / element.
List<String> acceptableTypes =
acceptableMediaTypesByElementName.getOrDefault(elementName, WILDCARD_MEDIA_TYPE);
Copy link

@schiwekM schiwekM Mar 22, 2026

Choose a reason for hiding this comment

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

Is this functional behaviour check adjustment intended? From my PoV / is no longer possible to send is it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is intentional. For the user there is no difference. Its just that elements without @Core.AcceptableMediaTypes are skipped for validation since there is no restriction, having the same effect as restricting them with a wildcard, just without the computation.
This is pure performance improvements (which we should do more often ^^)

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