Conversation
| [Fact] | ||
| public void GivenAResource_WhenValidatingWithNullRequestContext_ThenRecursiveValidationDefaultsToDisabled() | ||
| { | ||
| _requestContextAccessor.RequestContext.Returns((IFhirRequestContext)null); |
Check warning
Code scanning / CodeQL
Useless upcast Warning
| if (fhirContext.RequestHeaders.ContainsKey(KnownHeaders.RecursiveValidation) | ||
| && fhirContext.RequestHeaders.TryGetValue(KnownHeaders.RecursiveValidation, out var headerValue)) | ||
| { | ||
| if (bool.TryParse(headerValue, out bool recursiveValidation)) | ||
| { | ||
| return recursiveValidation; | ||
| } | ||
| } |
Check notice
Code scanning / CodeQL
Nested 'if' statements can be combined Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 23 hours ago
In general, to fix this issue you should merge nested if statements that lack else branches into a single if whose condition is the logical AND (&&) of the original conditions. This maintains the same logical requirement (all conditions must be true) while simplifying control flow and reducing indentation.
In this file, within GetRecursiveValidationSetting, there is an outer if on lines 100–102 checking for the presence of the RecursiveValidation header and trying to get its value, then an inner if on lines 103–106 attempting to parse that value to a boolean. The behavior is: only when the header exists, is retrievable, and parses successfully do we return recursiveValidation; otherwise we fall through and return false. To fix this without changing functionality, we should combine the header presence check and the TryParse call into a single if statement. That means replacing the outer if block (including the inner if and its braces) with one if whose condition first checks ContainsKey and TryGetValue, and then bool.TryParse. All of the logic stays in GetRecursiveValidationSetting and no new imports or methods are required.
Concretely:
- In
src/Microsoft.Health.Fhir.Core/Features/Validation/ResourceContentValidator.cs, inGetRecursiveValidationSetting(), replace the block starting withif (fhirContext.RequestHeaders.ContainsKey(...through the closing brace beforereturn false;with a single combinedifthat callsbool.TryParsedirectly in the condition and returnsrecursiveValidationif successful. - No additional dependencies, using directives, or helper methods are needed.
| @@ -98,12 +98,10 @@ | ||
|
|
||
| var fhirContext = _contextAccessor.RequestContext; | ||
| if (fhirContext.RequestHeaders.ContainsKey(KnownHeaders.RecursiveValidation) | ||
| && fhirContext.RequestHeaders.TryGetValue(KnownHeaders.RecursiveValidation, out var headerValue)) | ||
| && fhirContext.RequestHeaders.TryGetValue(KnownHeaders.RecursiveValidation, out var headerValue) | ||
| && bool.TryParse(headerValue, out bool recursiveValidation)) | ||
| { | ||
| if (bool.TryParse(headerValue, out bool recursiveValidation)) | ||
| { | ||
| return recursiveValidation; | ||
| } | ||
| return recursiveValidation; | ||
| } | ||
|
|
||
| return false; |
Description
Adds a header
x-ms-recursive-validationto turn on recursive validation for resources. Without this header only the top level resources are validated.x-ms-recursive-validation: trueRelated issues
Addresses [issue #].
Testing
Manual and new unit tests
FHIR Team Checklist
Semver Change (docs)
Patch|Skip|Feature|Breaking (reason)