Fix referenceText to not emit invalid MQL for unsafe field names (fixes #709)#717
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates autocomplete field completion output to generate valid aggregation field references for “unsafe” field names, and enhances the DocumentDB Indexes tree node to show a live index count.
Changes:
- Generate
referenceTextusing$field.pathonly when each dot-separated segment is identifier-safe; otherwise emit a$getFieldexpression. - Add unit tests covering
$getFieldusage and escaping for embedded quotes. - Cache and display index counts in the Indexes tree item, refreshing the node when counts change.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/utils/json/data-api/autocomplete/toFieldCompletionItems.ts | Adds “safe reference” detection and emits $getField form for unsafe field paths. |
| src/utils/json/data-api/autocomplete/toFieldCompletionItems.test.ts | Adds tests for $getField reference generation and escaping. |
| src/tree/documentdb/IndexesItem.ts | Caches index count and updates tree item description with localized count. |
microsoft#709) - Added isSafeAggregationReference() helper that checks each dot-separated segment - Unsafe field names (dashes, spaces, quotes) now emit $getField form - Safe names (valid identifiers, nested paths) keep $field syntax
…anch, use queueMicrotask
d11404d to
c611c0a
Compare
Maintainer update — rebased + correctness fixesThanks for the contribution! I rebased this branch and extended the fix so it produces valid MQL in all the cases the issue implied. Summary of what changed since the last push: Scope cleanup (rebase)
Correctness fixes on top of the original
Where it applies
Before vs. after (what would be inserted)The nested case (the core fix)Tests addedNested unsafe leaf, safe-prefix collapse (
|
…R notes
- buildAggregationReference() emits { $getField: { field, input } } for
unsafe segments in nested paths (was a broken top-level lookup)
- stricter AGGREGATION_SAFE_SEGMENT_PATTERN routes $-containing names
(e.g. $price) through $getField instead of emitting $$price
- tests for nested unsafe leaf, safe-prefix collapse, mixed segments,
$-in-name, literal-dot ambiguity, and empty/degenerate segments
- mark future-work.md item 2 resolved; add docs/ai-and-plans/PRs note
c611c0a to
521f026
Compare
Summary
Stops
toFieldCompletionItems()from emitting invalid MQL aggregation references for unsafe field names (containing dashes, spaces, quotes,$-prefixed names, etc.).Previously,
referenceTextwas always$+ field path, which produces broken MQL for fields likeorder-items,my field, orsay"hi".Changes in
src/utils/json/data-api/autocomplete/toFieldCompletionItems.tsAGGREGATION_SAFE_SEGMENT_PATTERNregex -- stricter than the JS identifier pattern, disallows leading/embedded$which would create ambiguous variable referencesescapeFieldName()helper -- escapes backslashes and double-quotes for use in$getFieldexpressionsbuildAggregationReference()helper -- emits$fieldfor fully safe paths,{ $getField: "name" }for unsafe top-level fields, and{ $getField: { field: "<leaf>", input: <parent-ref> } }for nested paths with unsafe segmentsreferenceTextfield doc and replaced it with accurate documentationExamples
age$age$age(unchanged)address.city$address.city$address.city(unchanged)order-items$order-items(invalid){ $getField: "order-items" }a.order-items$a.order-items(invalid){ $getField: { field: "order-items", input: "$a" } }my field$my field(invalid){ $getField: "my field" }$price$$price(misread as variable){ $getField: "$price" }say"hi"$say"hi"(invalid){ $getField: "say\"hi\"" }Documentation files updated
src/utils/json/data-api/autocomplete/future-work.md-- item 2 marked as resolved, Option B ($getField) documenteddocs/ai-and-plans/PRs/717-referenceText-unsafe-field-names.md-- new PR tracking doc addedTesting
referenceTextgeneration including nested paths and$-prefixed namesIssue
Fixes #709