Skip to content

Apply SMART v2 scope filters to chained and _has searches - #6089

Open
adithipo wants to merge 12 commits into
mainfrom
personal/adithip/smart-chained-search-scope-filters
Open

adithipo wants to merge 12 commits into
mainfrom
personal/adithip/smart-chained-search-scope-filters

Conversation

@adithipo

@adithipo adithipo commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Makes SMART v2 scope search-parameter filters (e.g. Condition.rs?category=problem-list-item) apply consistently to chained and _has searches, aligning them with direct search and _include/_revinclude.

Changes in SearchOptionsFactory:

  • Permitted: the scope's filters are applied within the chained / _has portion of the query (the chain target for forward chains, the referencing type for _has). When multiple filtered scopes cover that type and each filters on the same single parameter, their values are combined as one comma-separated (OR) value, e.g. category=a,b. Identical duplicate scopes are treated as one.
  • Withdrawn / refused (403): as in direct search (ADR 2609), a resource-specific scope whose filter can't be applied in a chain is withdrawn, and the request is refused only if no usable scope remains. An unusable filter on a wildcard (*.rs?...) scope always refuses the request. Unusable cases: multiple filtered scopes that cannot be combined (different or multiple parameters, or comparator values such as ge2020), modified or chained scope parameters (e.g. :not), chains resolving to multiple target types, a nested chain under a filtered link, unsupported scope parameters, and _id/_lastUpdated/_type or vector filters.
  • Wildcard scopes: a filtered wildcard scope (*.rs?...) is always applied in the chained / _has portion, even when a resource-specific scope also grants that type (ADR 2609). Resource-specific scopes are used only when no wildcard scope applies.
  • Unchanged: requests without fine-grained access control, with an unfiltered wildcard scope, or where an unfiltered resource-specific scope covers the chain's type and no wildcard scope applies.

Supporting SQL changes:

  • ChainFlatteningRewriter: predicates on a chain link that have no search-parameter table (e.g. _id) are now evaluated on the target resource, so a user's _id predicate can be combined with an appended scope filter.
  • SqlExpressionRewriter / SqlQueryGenerator: chain-link predicates keep their chain level. After a union, chain predicates and nested chain links join to the preceding CTE on the chain target. The second half of a split (concatenated) predicate reuses the first half's input.

Applies to SQL and Cosmos, all FHIR versions. No schema changes.

Related issues

Addresses AB#214773.

Testing

  • Unit tests in SearchOptionsFactorySmartScopeTests cover filter application in chains and _has, same-parameter scope merging, wildcard filters combined with resource-specific scopes, the refused (403) cases above, and unchanged behavior for unfiltered scopes.
  • A unit test in ChainFlatteningRewriterTests covers a chain link combining _id with a search-parameter predicate.
  • Unit tests in SqlQueryGeneratorTests cover chain predicates and nested chains following a SMART union, including split predicates.
  • These pass locally (dotnet test --project <R4 Core / SqlServer unit test project> --filter-class ...).
  • Integration tests in SmartSearchTests cover filtered scopes applied to forward chains (by name and by _id) and _has against SQL and Cosmos; these run in CI.

FHIR Team Checklist

  • Update the title of the PR to be succinct and less than 65 characters
  • Add a milestone to the PR for the sprint that it is merged (i.e. add S47)
  • Tag the PR with the type of update: Bug, Build, Dependencies, Enhancement, New-Feature or Documentation
  • Tag the PR with Open source, Azure API for FHIR (CosmosDB or common code) or Azure Healthcare APIs (SQL or common code) to specify where this change is intended to be released.
  • Tag the PR with Schema Version backward compatible or Schema Version backward incompatible or Schema Version unchanged if this adds or updates Sql script which is/is not backward compatible with the code.
  • When changing or adding behavior, if your code modifies the system design or changes design assumptions, please create and include an ADR.
  • CI is green before merge Build Status
  • Review squash-merge requirements

Semver Change (docs)

Patch

@adithipo
adithipo requested a review from a team as a code owner November 23, 2026 20:45
@adithipo adithipo added Bug Bug bug bug. Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Open source This change is only relevant to the OSS code or release. Schema Version unchanged No-PaaS-breaking-change No-ADR ADR not needed labels Nov 23, 2026
Copilot AI balanced review requested due to automatic review settings November 23, 2026 20:45
@adithipo adithipo added the Enhancement-Test Enhancement on tests. label Nov 23, 2026
@adithipo adithipo self-assigned this Nov 23, 2026
@adithipo adithipo added this to the FY27\Q2\2Wk\2Wk11 milestone Nov 23, 2026
@adithipo adithipo added Enhancement-Test Enhancement on tests. and removed Enhancement-Test Enhancement on tests. labels Nov 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

An unmatched trailing backslash can alter merged scope values, allowing chained or _has searches to match related resources excluded by both granted scopes.

1 open issue
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file
What changed in this PR

This PR extends SMART v2 scope filtering to chained and _has searches, aligning related-resource searches with direct search and include behavior.

Changes:

  • Applies scope filters inside chains, merges compatible alternatives, and rejects combinations that cannot be enforced.
  • Updates SQL chain rewriting to evaluate resource-level predicates on the chain target.
  • Adds unit and integration coverage plus a localized authorization error message.
File Description
test/​Microsoft.Health.Fhir.Shared.Tests.Integration/​Features/​Smart/​SmartSearchTests.cs Tests filtered forward chains, _id chains, and _has.
src/​Microsoft.Health.Fhir.SqlServer/​Features/​Search/​Expressions/​Visitors/​ChainFlatteningRewriter.cs Places resource-level predicates on the chain target.
src/​Microsoft.Health.Fhir.SqlServer.UnitTests/​Features/​Search/​Expressions/​ChainFlatteningRewriterTests.cs Tests combined resource and search-parameter predicates.
src/​Microsoft.Health.Fhir.Shared.Core/​Features/​Search/​SearchOptionsFactory.cs Enforces scope filters and validates supported chain combinations.
src/​Microsoft.Health.Fhir.Shared.Core.UnitTests/​Features/​Search/​SearchOptionsFactorySmartScopeTests.cs Covers filter application, merging, rejection, and unchanged behavior.
src/​Microsoft.Health.Fhir.Core/​Resources.resx Adds the unenforceable-chain authorization message.
src/​Microsoft.Health.Fhir.Core/​Resources.Designer.cs Exposes the new localized message.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 23, 2026 20:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical SQL binding defect applies the chain target’s scope filter to the primary resource when both resource types have filtered scopes, dropping valid search matches.

1 open issue
1 resolved since last review
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

@codecov-commenter

Codecov Comments Bot (codecov-commenter) commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.18%. Comparing base (d3c04c8) to head (5fcd766).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6089      +/-   ##
==========================================
+ Coverage   81.51%   82.18%   +0.66%     
==========================================
  Files        1036     1037       +1     
  Lines       40497    40672     +175     
  Branches     6611     6668      +57     
==========================================
+ Hits        33013    33427     +414     
+ Misses       5916     5666     -250     
- Partials     1568     1579      +11     

see 16 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

adithipo and others added 7 commits November 23, 2026 20:36
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@adithipo
adithipo force-pushed the personal/adithip/smart-chained-search-scope-filters branch from f56fb0a to 8d1dffa Compare November 24, 2026 01:42
Copilot AI balanced review requested due to automatic review settings November 24, 2026 01:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Authorization-sensitive changes span SQL and Cosmos, but provider-backed results are unavailable and new tests cover only R4/R4B, requiring maintainer validation of enforcement and compatibility.

No open issues 🎉

1 resolved since last review
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Inline findings from my review of 8d1dffa6, grouped by priority. The two must-fix items are based on static traces; I did not run them against a database.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 23:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Two critical issues remain: decoded scope-key collisions can silently drop grants and make results scope-order dependent, while nested-chain predecessor handling can exclude matching range-only records.

2 open issues
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 23:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Ambiguous wildcard authorization semantics and missing storage-backed validation for security-sensitive chained-search and CTE-binding changes require maintainer clarification and runtime verification.

No open issues 🎉

2 resolved since last review
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

…ope logic

Wildcard SMART scope filters are now always applied to chain and _has targets, even when an unfiltered typed scope also grants the type. Move chain scope helpers into SearchOptionsFactory.SmartChainScopes.cs.
Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical chain-query bug after a SMART union can skip the preceding _id restriction and return unrelated patients, requiring corrected CTE selection and a SQL-backed regression test.

1 open issue
2 resolved since last review
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Authorization-boundary and SQL CTE binding changes need maintainer confirmation of storage-backed SQL/Cosmos CI results, since nested split-predicate tests assert SQL text without verifying execution behavior.

1 open issue
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because inconsistent wildcard scope handling between direct and chained searches may allow matches through resources that the same token excludes from direct searches.

No open issues 🎉

1 resolved since last review
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Bug Bug bug bug. Enhancement-Test Enhancement on tests. No-ADR ADR not needed No-PaaS-breaking-change Open source This change is only relevant to the OSS code or release. Schema Version unchanged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants