如何从 python 中的 fhirclient-4.0.0 prepare() 方法修复 'FHIRValidationError'?

How to fix 'FHIRValidationError' from fhirclient-4.0.0 prepare() method in python?

我正在使用 hapi R4 服务器测试 fhirclient 4.0.0 (Python 3.7)

settings = {
    'app_id': 'fhir',
#     'api_base': 'http://test.fhir.org/r4'
    'api_base': 'http://hapi.fhir.org/baseR4'
}
smart = client.FHIRClient(settings=settings)    

# capabilitystatement.CapabilityStatement.read_from('metadata', smart.server)
smart.prepare()

我收到此验证错误

FHIRValidationError: {root}:
  rest.0:
    resource.70:
      searchParam.14:
        'Non-optional property "type" on <models.capabilitystatement.CapabilityStatementRestResourceSearchParam object at 0x7fe54cef1668> is missing'

Link 到能力声明 http://hapi.fhir.org/baseR4/metadata

还有其他人面临这个问题吗?

下面是错误的完整踪迹..

---------------------------------------------------------------------------
FHIRValidationError                       Traceback (most recent call last)
<ipython-input-44-687512ba52eb> in <module>
      7 
      8 # capabilitystatement.CapabilityStatement.read_from('metadata', sof.server)
----> 9 sof.prepare()

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/client.py in prepare(self)
    115             if self.server.ready:
    116                 return True
--> 117             return self.server.prepare()
    118         return False
    119 

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/server.py in prepare(self)
    148         """
    149         if self.auth is None:
--> 150             self.get_capability()
    151         return self.auth.ready if self.auth is not None else False
    152 

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/server.py in get_capability(self, force)
     82             logger.info('Fetching CapabilityStatement from {0}'.format(self.base_uri))
     83             from models import capabilitystatement
---> 84             conf = capabilitystatement.CapabilityStatement.read_from('metadata', self)
     85             self._capability = conf
     86 

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/models/fhirabstractresource.py in read_from(cls, path, server)
    108 
    109         ret = server.request_json(path)
--> 110         instance = cls(jsondict=ret)
    111         instance.origin_server = server
    112         return instance

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/models/capabilitystatement.py in __init__(self, jsondict, strict)
    132         Type `str`. """
    133 
--> 134         super(CapabilityStatement, self).__init__(jsondict=jsondict, strict=strict)
    135 
    136     def elementProperties(self):

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/models/domainresource.py in __init__(self, jsondict, strict)
     40         Type `Narrative` (represented as `dict` in JSON). """
     41 
---> 42         super(DomainResource, self).__init__(jsondict=jsondict, strict=strict)
     43 
     44     def elementProperties(self):

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/models/resource.py in __init__(self, jsondict, strict)
     40         Type `Meta` (represented as `dict` in JSON). """
     41 
---> 42         super(Resource, self).__init__(jsondict=jsondict, strict=strict)
     43 
     44     def elementProperties(self):

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/models/fhirabstractresource.py in __init__(self, jsondict, strict)
     23                 .format(self.__class__, jsondict['resourceType']))
     24 
---> 25         super(FHIRAbstractResource, self).__init__(jsondict=jsondict, strict=strict)
     26 
     27     @classmethod

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/models/fhirabstractbase.py in __init__(self, jsondict, strict)
     64         if jsondict is not None:
     65             if strict:
---> 66                 self.update_with_json(jsondict)
     67             else:
     68                 try:

~/.virtualenvs/fhir/lib/python3.7/site-packages/fhirclient/models/fhirabstractbase.py in update_with_json(self, jsondict)
    229 
    230         if len(errs) > 0:
--> 231             raise FHIRValidationError(errs)
    232 
    233     def as_json(self):

FHIRValidationError: {root}:
  rest.0:
    resource.70:
      searchParam.14:
        'Non-optional property "type" on <models.capabilitystatement.CapabilityStatementRestResourceSearchParam object at 0x7fe54cef1668> is missing'

Github 问题:https://github.com/smart-on-fhir/client-py/issues/79

HAPI 服务器 returns 一个无效的 CapabilityStatement 资源,其中尚未填写 Location 的 searchParam 'near' 类型 - 我对此进行了一些检查。所以错误不在您的客户端中;实际上,它通过告诉您跟踪末尾 "FHIRValidationError".

下面的错误来完美地工作

您最好的选择是联系 HAPI 服务器开发人员并要求他们更正他们的 CapabilityStatement。

您确定您正在使用 fhirclient 4.0.0 吗?虽然 4.0.0 已经推出一年多了 pip 仍然有版本 3.2.0. 这里有一个问题:https://github.com/smart-on-fhir/client-py/issues/70

对当前版本的 HAPI 使用 fhirclient 3.2.0 将导致类似的错误。您可以直接从他们的 github 克隆 fhirclient 或使用带有 DSTU3 的 HAPI 服务器。托管您自己的服务器时,您可以将配置 hapi.properties 更改为 fhir_version=DSTU3.