Skip to content

FHIR_TYPES_MAPS global state prevents using multiple FHIR versions in same process #202

Description

@c-lexer

Description:

When using multiple FHIR versions (e.g., STU3 and R5) in the same Python process, validation fails because fhir_core.types.FHIR_TYPES_MAPS is a shared global dictionary that gets overwritten.

Steps to reproduce:

# Import STU3 first
from fhir.resources.STU3.bundle import Bundle as STU3Bundle
data = {'type': 'searchset', 'entry': []}
STU3Bundle.model_validate(data)  # Works

# Now try R5 with valid R5 data
from fhir.resources.bundle import Bundle as R5Bundle
r5_data = {
    'type': 'searchset',
    'entry': [{
        'resource': {
            'resourceType': 'CarePlan',
            'status': 'active',
            'intent': 'plan',
            'subject': {'reference': 'Patient/123', 'type': 'Patient'}  # R5 field
        }
    }]
}
R5Bundle.model_validate(r5_data)  # FAILS: "Extra inputs are not permitted"

Workaround:

import importlib
import fhir_core.types

fhir_core.types.FHIR_TYPES_MAPS.clear()
from fhir.resources import fhirtypes
importlib.reload(fhirtypes)
# Now R5 validation works

Environment:

fhir-resources: 8.0.0
fhir-core: 1.1.0
Python: 3.13

Suggested fix:
Each FHIR version should maintain its own type map, or the map should be version-aware to prevent cross-contamination.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions