Skip to content

InMemoryFhirRepository.read() fails to match resources stored with plain IDs when looked up by relative reference #945

@c-schuler

Description

@c-schuler

InMemoryFhirRepository uses id.toUnqualifiedVersionless() as the inner map key for both update() (storage) and read() (lookup). Since the outer map already partitions by resource type (resource.fhirType()), the inner key only needs the ID part. However, toUnqualifiedVersionless() preserves whatever format was provided — with or without the resource type prefix.

This causes a mismatch when:

  1. A resource is stored via update() with a plain ID: resource.setId("test-lib") - inner key is "test-lib"
  2. A lookup via read() uses a relative reference: new IdType("Library/test-lib") - lookup key is "Library/test-lib"

These don't match, resulting in a ResourceNotFoundException.

This is the standard FHIR pattern - per the spec, a reference like "Library/test-lib" is a relative URL where Library is the resource type path segment and test-lib is the ID. The resource's actual ID should be "test-lib", not "Library/test-lib".

Suggested fix:

In read(), use id.getIdPart() instead of id.toUnqualifiedVersionless() for the inner map lookup. The same normalization should be applied in update(), create(), and delete() for consistency. Alternatively, normalize both sides to always include the resource type using withResourceType().

Affected file: cqf-fhir-utility/src/main/java/org/opencds/cqf/fhir/utility/repository/InMemoryFhirRepository.java

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions