Skip to content

Add opt-in diagnostic logging for OData query validation failures - #2908

Merged
WanjohiSammy merged 8 commits into
masterfrom
fix/query-validation-error-logging
Jul 28, 2026
Merged

WanjohiSammy merged 8 commits into
masterfrom
fix/query-validation-error-logging

Conversation

@WanjohiSammy

Copy link
Copy Markdown
Member

Issues

This pull request fixes #xxx.

Description

Ports the query-validation diagnostic logging feature OData/AspNetCoreOData#1592 to the ASP.NET Core WebApi stack.

What's included

  • EnableQueryAttribute.EnableQueryValidationErrorLogging to opt in per action.
  • Global ODataOptions.EnableQueryValidationErrorLogging + ODataOptions.QueryValidationErrorLogLevel (default Warning); an explicit value on the attribute overrides the global value.
  • New internal QueryValidationErrorLogger that emits the endpoint route template, queried type, attempted $select/$expand, and the failure reason together with the validation exception, categorized for EnableQueryAttribute.
  • Diagnostics never change the response produced for the failed query.

Checklist (Uncheck if it is not completed)

  • Test cases added
  • Build and test with one-click build and test script passed

Additional work necessary

If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.

Condense the verbose <summary> blocks added for the opt-in query validation error logging feature while preserving default values, <see cref> links, and the 'never changes the response' invariant.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 573eb3d9-6fb1-4a5d-8bbf-87c0ddbf3dde
requestQueryData is already dereferenced unconditionally by the preceding QueryValidationRunBeforeActionExecution check, so the 'if (requestQueryData != null)' guard could never be false. Removing it resolves the contradictory non-null/nullable invariant within the method; behavior is unchanged since the guarded branch was always taken.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 573eb3d9-6fb1-4a5d-8bbf-87c0ddbf3dde
Adds two AspNetCore tests for the query-validation diagnostic: one asserting the combined $select+$expand branch of QueryValidationErrorLogger.FormatRequestedQueryOptions, and one that sets a RouteEndpoint and asserts the logged Endpoint route template. Extends the test harness with an optional endpoint parameter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 573eb3d9-6fb1-4a5d-8bbf-87c0ddbf3dde
Add a test that exercises the post-action validation branch (QueryValidationRunBeforeActionExecution == false) by seeding request state with no OData path so pre-action validation is skipped, then driving OnActionExecuted with an IQueryable result and an invalid $select. Asserts a single diagnostic entry is written with the element type, requested options, and reason.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 573eb3d9-6fb1-4a5d-8bbf-87c0ddbf3dde
xuzhg
xuzhg previously approved these changes Jul 27, 2026
@WanjohiSammy
WanjohiSammy requested review from xuzhg July 27, 2026 20:07
@WanjohiSammy
WanjohiSammy merged commit 97faca2 into master Jul 28, 2026
1 check passed
@WanjohiSammy
WanjohiSammy deleted the fix/query-validation-error-logging branch July 28, 2026 06:53
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