Skip to content

Add unit tests verifying contained resource validation fix#609

Merged
andrzejskowronski merged 4 commits intodevelopfrom
copilot/fix-validation-error-goals
Oct 30, 2025
Merged

Add unit tests verifying contained resource validation fix#609
andrzejskowronski merged 4 commits intodevelopfrom
copilot/fix-validation-error-goals

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 29, 2025

When validating a CarePlan with multiple contained Goal resources, validation errors for an invalid contained resource incorrectly referenced valid contained resources. This occurred when contained resources had different profiles with fixed values.

Test Coverage

Added ContainedResourceValidationTests.cs with two test cases:

  • Invalid contained resource isolation: Verifies only the invalid Goal (contained[1]) appears in error messages, not the valid Goal (contained[0])
  • Valid contained resources: Confirms no errors when both Goals comply with their respective profiles

Both tests create CarePlan resources with contained Goals having fixed URI constraints:

// Goal 1: valid system
new Coding { System = "http://fixed-uri.com/system", Code = "G1" }

// Goal 2: invalid system (violates fixed value)
new Coding { System = "http://incorrect-uri.com/system", Code = "G2" }

Technical Changes

  • Uses SnapshotSource to handle differential-only StructureDefinition profiles
  • Removed version constraint override in R4 test project (now uses base props version)
  • Tests confirm issue is resolved in SDK6
Original prompt

This section details on the original issue you should resolve

<issue_title>The first resource in contained section is shown in the error message although the resource is valid</issue_title>
<issue_description>I have a CarePlan resource with two contained Goal resources. Each Goal resource has a profile to restrict the Goal.description.coding.system to a fixedUri. The 2nd Goal resource does not comply with the profile and is expected to generate a validation error. However, the validation result incorrectly shows the 1st Goal resource in additional to the 2nd one.

Goal profile:
https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g1 and https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g2 are same except the url difference.

{
  "resourceType": "StructureDefinition",
  "url": "https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g1",
  "name": "Goal-with-fixedurl-g2",
  "status": "draft",
  "fhirVersion": "4.3.0",
  "kind": "resource",
  "abstract": false,
  "type": "Goal",
  "baseDefinition": "http://hl7.org/fhir/StructureDefinition/Goal",
  "derivation": "constraint",
  "differential": {
    "element": [
      {
        "id": "Goal.description.coding.system",
        "path": "Goal.description.coding.system",
        "fixedUri": "http://fixed-uri.com/system"
      }
    ]
  }
}

CarePlan resource:

{
  "resourceType": "CarePlan",
  "contained": [
    {
      "resourceType": "Goal",
      "id": "f873f0480edf4e7da98c01be4c05ad91",
      "meta": {
        "profile": [
          "https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g1"
        ]
      },
      "lifecycleStatus": "active",
      "description": {
        "coding": [
          {
            "system": "http://fixed-uri.com/system",
            "code": "G1"
          }
        ]
      },
      "subject": {
        "reference": "Patient/example",
        "display": "Peter James Chalmers"
      }
    },
    {
      "resourceType": "Goal",
      "id": "f873f0480edf4e7da98c01be4c05ad92",
      "meta": {
        "profile": [
          "https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g2"
        ]
      },
      "lifecycleStatus": "active",
      "description": {
        "coding": [
          {
            "system": "http://incorrect-uri.com/system",
            "code": "G2"
          }
        ]
      },
      "subject": {
        "reference": "Patient/example",
        "display": "Peter James Chalmers"
      }
    }
  ],
  "status": "active",
  "intent": "plan",
  "subject": {
    "reference": "Patient/example",
    "display": "Peter James Chalmers"
  },
  "goal": [
    {
      "reference": "#f873f0480edf4e7da98c01be4c05ad91"
    },
    {
      "reference": "#f873f0480edf4e7da98c01be4c05ad92"
    }
  ]
}

The validation OperationOutcome:

{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "invalid",
      "details": {
        "coding": [
          {
            "system": "http://hl7.org/fhir/dotnet-api-operation-outcome",
            "code": "1008"
          },
          {
            "system": "http://fire.ly/dotnet-sdk-operation-outcome-structdef-reference",
            "code": "CarePlan.contained->Goal(https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g2).description.coding.system"
          }
        ],
        "text": "Value 'http://incorrect-uri.com/system' is not exactly equal to fixed value 'http://fixed-uri.com/system'"
      },
      "location": [
        "CarePlan.contained[1].description[0].coding[0].system[0], element CarePlan.contained->Goal(https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g2).description.coding.system"
      ],
      "expression": [
        "CarePlan.contained[1].description[0].coding[0].system[0], element CarePlan.contained->Goal(https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g2).description.coding.system"
      ]
    },
    {
      "severity": "error",
      "code": "invalid",
      "details": {
        "coding": [
          {
            "system": "http://hl7.org/fhir/dotnet-api-operation-outcome",
            "code": "1008"
          },
          {
            "system": "http://fire.ly/dotnet-sdk-operation-outcome-structdef-reference",
            "code": "CarePlan.goal->Goal(https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g2).description.coding.system"
          }
        ],
        "text": "Value 'http://incorrect-uri.com/system' is not exactly equal to fixed value 'http://fixed-uri.com/system'"
      },
      "location": [
        "CarePlan.contained[0].description[0].coding[0].system[0], element CarePlan.goal->Goal(https://example.org/fhir/StructureDefinition/Goal-with-fixedurl-g2).description.coding.system"
      ],
      "expression": [
        "CarePlan.contained[0].description[0].coding[0].system[0], element CarePlan.goal->Goal(https://example.org/fhir/StructureDefinition/Goal-with-f...

</details>

- Fixes FirelyTeam/firely-validator-api#489

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

Copilot AI and others added 2 commits October 29, 2025 12:40
- Created ContainedResourceValidationTests.cs with two test cases
- First test verifies only invalid Goal appears in error messages
- Second test verifies both valid Goals pass validation
- Fixed package version issue in R4 test project
- Tests confirm the issue is fixed in SDK6

Co-authored-by: alexzautke <548617+alexzautke@users.noreply.github.com>
Co-authored-by: alexzautke <548617+alexzautke@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix validation error for contained Goal resources Add unit tests verifying contained resource validation fix Oct 29, 2025
Copilot AI requested a review from alexzautke October 29, 2025 12:49
Copy link
Copy Markdown
Member

@Kasdejong Kasdejong left a comment

Choose a reason for hiding this comment

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

more tests, always nice...

@Kasdejong Kasdejong marked this pull request as ready for review October 30, 2025 15:13
@andrzejskowronski andrzejskowronski merged commit adab023 into develop Oct 30, 2025
2 checks passed
@andrzejskowronski andrzejskowronski deleted the copilot/fix-validation-error-goals branch October 30, 2025 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants