OHSUCMP / ecp-sds-hardfork

eCare Plan Supplemental Data Store (hard-forked from hapi-fhir-jpaserver-starter v6.4.0)
Apache License 2.0
0 stars 2 forks source link

figure out how to validate resource source system / partition #4

Open mattStorer opened 1 year ago

mattStorer commented 1 year ago

debug session attempting to post a Condition for a foreign Patient resource progresses through the interceptors until we hit SupplementalDataStorePartitionInterceptor.validateResourceBelongsInPartition() which fails with message "id element present without base url; cannot match SDS partition" as the id has no base URL.

        IIdType id = resource.getIdElement();
        if (id == null || StringUtils.isBlank(id.getIdPart())) {
            if (!sdsProperties.getPartition().getLocalName().equals(partitionName)) {
                throw new InvalidRequestException(String.format("cannot store resource identified as belonging in local SDS partition into SDS partition \"%1$s\"", partitionName));
            }

        } else {
            if (!id.hasBaseUrl()) {
                throw new InvalidRequestException("id element present without base url; cannot match SDS partition");
            }

which makes sense, as this is the resource we're trying to post:

{
  "resourceType": "Condition",
  "id": "ec.LXbwbDy2AaPennhoSvNJPFLtrcz0FWeKB39VPds9I3",
  "clinicalStatus": {
    "coding": [
      {
        "system": "http://terminology.hl7.org/CodeSystem/condition-clinical",
        "version": "4.0.0",
        "code": "active",
        "display": "Active"
      }
    ],
    "text": "Active"
  },
  "verificationStatus": {
    "coding": [
      {
        "system": "http://terminology.hl7.org/CodeSystem/condition-ver-status",
        "version": "4.0.0",
        "code": "confirmed",
        "display": "Confirmed"
      }
    ],
    "text": "Confirmed"
  },
  "category": [
    {
      "coding": [
        {
          "system": "http://terminology.hl7.org/CodeSystem/condition-category",
          "code": "problem-list-item",
          "display": "Problem List Item"
        }
      ],
      "text": "Problem List Item"
    },
    {
      "coding": [
        {
          "system": "http://loinc.org",
          "code": "75310-3",
          "display": "Health concerns"
        },
        {
          "system": "http://hl7.org/fhir/us/core/CodeSystem/condition-category",
          "code": "health-concern",
          "display": "Health Concern"
        }
      ],
      "text": "Health Concern"
    }
  ],
  "code": {
    "coding": [
      {
        "system": "http://hl7.org/fhir/sid/icd-10-cm",
        "code": "F41.9"
      },
      {
        "system": "http://snomed.info/sct",
        "code": "48694002"
      }
    ],
    "text": "Anxiety"
  },
  "subject": {
    "reference": "Patient/eFTHaVbQzCEwOEE97maN2MC2jJi-r8nnkhRh.umMUlz03",
    "display": "Fhir, Rosie Pink"
  },
  "onsetPeriod": {
    "start": "2021-08-02",
    "end": "2021-08-02"
  },
  "recordedDate": "2021-11-29"
}

the X-Partition-Name header is https://epicmobile.ohsu.edu/FHIRDEV/api/FHIR/R4 as this resource comes from the OHSU POC FHIR server (all fake / test data), and this is also the base URL for that system. (the partition name and the base URL of the source system should be the same).

however, as you can see, the string https://epicmobile.ohsu.edu/FHIRDEV/api/FHIR/R4 doesn't occur anywhere in the source Condition resource above, so it seems this base URL might not be available within the context of resources we're trying to clone into the SDS in this way.

if this is definitely the case and there's no way to get around it, we'll need to sort out another way to validate partition membership, or just trust the source system to provide the correct partition and skip validation altogether.

timcoffman commented 1 year ago

Agreed, when a resource has no base url, we can choose to trust it is OK. This seems like a good "policy" flag that belongs in a configuration item.

timcoffman commented 1 year ago

I’m writing a few unit tests, for storing local and foreign resources as you describe. So far I found an issue with storing a non-partitionable resources. I’ll commit that soon.

timcoffman commented 1 year ago

Currently I can see that, though I’m providing an id on a new patient resource, when I ask the server to create it, the id is getting erased. It may need to be a PUT instead of a POST. I’ll try that.

mattStorer commented 1 year ago

Currently I can see that, though I’m providing an id on a new patient resource, when I ask the server to create it, the id is getting erased. It may need to be a PUT instead of a POST. I’ll try that.

Yes, PUT allows the ID to be retained, I've successfully tested that.

The new problem I'm encountering is that I can't PUT a foreign Patient resource without first PUTting the referenced Practitioner resource. But what if the referenced Practitioner references another resource? What if there are cyclical dependencies? What if we can't read certain resources due to scoping issues? We certainly can't remove references. Could we tell the system to just put them anyway, even though the references don't point to anything valid? I don't like that either. But it seems we need be very, very intentional about the order in which we write resources to the SDS if we want to avoid these sorts of issues.