fix: NullReferenceException in ParameterCompatibleFilter.ParseResource (Fixes #5114)#5630
Open
rtmalikian wants to merge 1 commit into
Open
Conversation
Fixes microsoft#5114 The ParseResource method accesses .Resource on the result of List.Find() without null-checking. When a Parameters resource has no parameter named 'resource', or the parameter's Resource property is null, this causes a NullReferenceException. Changes: - Add null-conditional operator on resource.TypeName check - Add null-conditional on Parameters.Parameter access - Guard against Find() returning null before accessing .Resource - When guard fails, fall through returning the original resource (which will produce a proper validation error downstream) Signed-off-by: rtmalikian <rtmalikian@gmail.com>
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.
Fixes #5114
Summary
Adds null-safety guards to
ParameterCompatibleFilter.ParseResource()to preventNullReferenceExceptionwhen processing Parameters resources without a "resource" parameter.Problem
When a
Parametersresource is submitted through the validation pipeline and doesn't contain a parameter named"resource", theParseResourcemethod crashes with:This occurs because
List<T>.Find()returnsnullwhen no match is found, and the code immediately accesses.Resourceon the result without null-checking.Root Cause
Line 26 in
ParameterCompatibleFilter.cschains three dereferences without null guards:Fix
Three null-safety improvements:
resource?.TypeName— null-conditional on the resource itselfparameters.Parameter?.Find(...)— null-conditional on the Parameter collectionresourceParam?.Resource != null— guard againstFind()returning null or the found parameter having a null ResourceWhen the guard fails, the method returns the original
Parametersresource unchanged, which produces a properResourceNotValidException(type mismatch) downstream instead of an unhandled crash.Files Changed
src/Microsoft.Health.Fhir.Shared.Api/Features/Filters/ParameterCompatibleFilter.cs— Add null-safety guards toParseResourceVerification
Parametercollection is null, no exception is thrownChangelog
Files Changed
src/Microsoft.Health.Fhir.Shared.Api/Features/Filters/ParameterCompatibleFilter.cs— Add null-conditional operators and guard clauseVerification
dotnet buildavailable in this environment; pattern consistency verified against sibling filter classesAbout the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.
📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a
Disclosure: This code was developed with assistance from mimo-v2.5-pro (Xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.