Return 200 empty payload for cached reads with empty result - #3822
Open
Souvik Ghosh (souvikghosh04) wants to merge 1 commit into
Open
Souvik Ghosh (souvikghosh04) wants to merge 1 commit into
Souvik Ghosh (souvikghosh04) wants to merge 1 commit into
Conversation
When entity/runtime caching is enabled and a read returns zero rows, the cached value surfaces as a default(JsonElement) with JsonValueKind.Undefined and no backing document. ParseResultIntoJsonDocument called JsonSerializer.SerializeToUtf8Bytes on it, which throws InvalidOperationException via JsonElement.WriteTo -> CheckValidInstance(), producing HTTP 500. Guard the Undefined case and return null, matching the non-cached path that renders an empty payload. This path is shared by REST and GraphQL. Adds a regression unit test.
Copilot started reviewing on behalf of
Souvik Ghosh (souvikghosh04)
September 21, 2026 11:39
View session
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The fix matches existing non-cached empty-result behavior and has focused regression coverage.
Review effort: Lite
Findings: None
What changed in this PR
Fixes cached empty SQL reads that previously caused HTTP 500 by handling undefined JsonElement values before serialization.
Changes:
- Return
nullfor undefined cached results. - Add a regression unit test.
| File | Description |
|---|---|
src/Core/Resolvers/SqlQueryEngine.cs |
Guards undefined cached results. |
src/Service.Tests/UnitTests/SqlQueryEngineHelperTests.cs |
Tests undefined-element handling. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Mohit G (gmohit21)
approved these changes
Sep 21, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #3704 (and its duplicate #3446).
When entity/runtime caching is enabled and a REST/GraphQL read returns zero
rows, DAB returned HTTP 500 (
System.InvalidOperationException) duringresponse serialization. With caching disabled, the identical request correctly
returns
200with an empty payload (e.g.{"value":[]}).Root cause
In the cache read path,
SqlQueryEngine.GetResultInCacheScenario→ParseResultIntoJsonDocument(JsonElement? result):GetJsonResultAsync<JsonElement>yieldsdefault(JsonElement)— a struct whoseValueKindisJsonValueKind.Undefinedand which has no backing
JsonDocument. Because it is a value type, it is notnull, so the nullableresulthas a value.ParseResultIntoJsonDocumentthen calledJsonSerializer.SerializeToUtf8Bytes(result), which invokesJsonElement.WriteTo→CheckValidInstance()and throwsInvalidOperationException, surfaced as HTTP 500.The non-cached path never hits this: it returns a
nullJsonDocument?for anempty result, which downstream renders as an empty payload. The cache path was
missing that empty guard. The failure occurs on both cache miss and cache hit,
and applies to list (
FOR JSON PATH) and by-PK reads, for both REST andGraphQL (this method is shared).
Fix
Guard the
Undefinedcase inParseResultIntoJsonDocumentand returnnull,matching the non-cached empty-result path:
The pre-existing
null-nullable behavior (serializing to a JSONnulldocument)is intentionally left unchanged, since only the
Undefinedelement throws.Tests
SqlQueryEngineHelperTests.ParseResultIntoJsonDocument_UndefinedElement_ReturnsNull,which invokes the method with
default(JsonElement)and asserts it returnsnull(previously it threwInvalidOperationException).ParseResultIntoJsonDocument_HandlesValuesAndNullcases continueto pass unchanged.
SqlQueryEngineHelperTestspass;dotnet format --verify-no-changesisclean on the changed files.