nazrulworld / fhir.resources

FHIR Resources https://www.hl7.org/fhir/resourcelist.html
https://pypi.org/project/fhir.resources/
Other
365 stars 104 forks source link

FHIR resource as pydantic request model in FastAPI #132

Open ankushgovil opened 1 year ago

ankushgovil commented 1 year ago

FastAPI supports using pydantic models to define JSON request body for REST API endpoints;

I need the FHIR resources model to be used as a request in FastAPI Going through the README; it doesn't seems to be compatible with pydantic model.

An example is shown of Patient resource from release 5.1.1, with python 3.10 FastAPI 0.85.0 and pydantic 1.10.2:

fastapi import APIRouter
from fhir.resources.patient import Patient

router = APIRouter()

@router.get(
    "/patient",
    response_model=Patient)
def get_patient(
        patient=Patient
):
    return patient

This gives a couple of type errors on endpoint setup:

File "/Users/ankush.govil/work/external-integration/venv/lib/python3.10/site-packages/fastapi/openapi/utils.py", line 103, in get_openapi_operation_parameters "schema": field_schema( File "pydantic/schema.py", line 247, in pydantic.schema.field_schema File "pydantic/schema.py", line 215, in pydantic.schema.get_field_info_schema File "pydantic/schema.py", line 985, in pydantic.schema.encode_default File "pydantic/json.py", line 90, in pydantic.json.pydantic_encoder TypeError: Object of type 'ModelMetaclass' is not JSON serializable

Also, If I able to comment this type issue the request model example in swagger is coming as

{
  "resource_type": "Patient",
  "fhir_comments": "string",
  "id": "7qIl65KS1dTPe6YnEzHGED",
  "_implicitRules": "Unknown Type: FHIRPrimitiveExtension",
  "meta": "Unknown Type: Meta",
  "contained": [
    "Unknown Type: Resource"
  ],
  "extension": [
    "Unknown Type: Extension"
  ],
  "modifierExtension": [
    "Unknown Type: Extension"
  ],
  "text": "Unknown Type: Narrative",
  "active": true,
  "address": [
    "Unknown Type: Address"
  ],
  "birthDate": "2023-06-16",
  "communication": [
    "Unknown Type: PatientCommunication"
  ],
  "contact": [
    "Unknown Type: PatientContact"
  ],
  "deceasedBoolean": true,
  "deceasedDateTime": "2023-06-16T07:50:51.114Z",
  "gender": "Unknown Type: FHIRPrimitiveExtension",
  "generalPractitioner": [
    "Unknown Type: Reference"
  ],
  "identifier": [
    "Unknown Type: Identifier"
  ],
  "link": [
    "Unknown Type: PatientLink"
  ],
  "managingOrganization": "Unknown Type: Reference",
  "maritalStatus": "Unknown Type: CodeableConcept",
  "multipleBirthBoolean": "Unknown Type: FHIRPrimitiveExtension",
  "multipleBirthInteger": "Unknown Type: FHIRPrimitiveExtension",
  "name": [
    "Unknown Type: HumanName"
  ],
  "photo": [
    "Unknown Type: Attachment"
  ],
  "telecom": [
    "Unknown Type: ContactPoint"
  ]
}

Does FastAPI support these FHIR resources as a request model in pydantic?