Skip to content
\n

The call I am making that raises this error is:

\n
    openapi_response = RequestsOpenAPIResponse(api_response)\n    openapi_request = RequestsOpenAPIRequest(api_response.request)\n    openapi.validate_response(openapi_request, openapi_response)\n
\n

Where api_response is a requests.models.Response:

\n
(Pdb) type(api_response)\n<class 'requests.models.Response'>\n(Pdb) api_response.content\nb'\\n    \\n        \\n        \\n        {\"entry\":[{\"fullUrl\":\"https://internal-dev.api.service.nhs.uk/validated-relationships/FHIR/R4/RelatedPerson/47CD795E\",\"resource\":{\"id\":\"47CD795E\",\"identifier\":[{\"system\":\"https://fhir.nhs.uk/Id/nhs-number\",\"value\":\"9730675929\"}],\"patient\":{\"identifier\":{\"system\":\"https://fhir.nhs.uk/Id/nhs-number\",\"value\":\"9730676399\"},\"type\":\"Patient\"},\"relationship\":[{\"coding\":[{\"code\":\"MTH\",\"display\":\"mother\",\"system\":\"http://terminology.hl7.org/CodeSystem/v3-RoleCode\"}]}],\"resourceType\":\"RelatedPerson\"},\"search\":{\"mode\":\"match\"}}],\"link\":[{\"relation\":\"self\",\"url\":\"https://internal-dev.api.service.nhs.uk/validated-relationships-service-api/FHIR/R4/RelatedPerson?identifier=9730675929&patient%3Aidentifier=9730676399\"}],\"timestamp\":\"2024-04-24T10:05:26+00:00\",\"total\":1,\"type\":\"searchset\",\"resourceType\":\"Bundle\"}\\n'\n
\n

So it looks like openapi-core is expecting a dict but is getting bytes:

\n

\n
\n

\n openapi-core/openapi_core/casting/schemas/casters.py\n

\n

\n Lines 113 to 114\n in\n 99af2d3\n

\n
\n
\n \n\n \n \n \n \n\n \n \n \n \n
if not isinstance(value, dict):
raise CastError(value, self.schema[\"type\"])
\n
\n
\n

\n
(Pdb) openapi_response.data\nb'\\n    \\n        \\n        \\n        {\"entry\":[{\"fullUrl\":\"https://internal-dev.api.service.nhs.uk/validated-relationships/FHIR/R4/RelatedPerson/47CD795E\",\"resource\":{\"id\":\"47CD795E\",\"identifier\":[{\"system\":\"https://fhir.nhs.uk/Id/nhs-number\",\"value\":\"9730675929\"}],\"patient\":{\"identifier\":{\"system\":\"https://fhir.nhs.uk/Id/nhs-number\",\"value\":\"9730676399\"},\"type\":\"Patient\"},\"relationship\":[{\"coding\":[{\"code\":\"MTH\",\"display\":\"mother\",\"system\":\"http://terminology.hl7.org/CodeSystem/v3-RoleCode\"}]}],\"resourceType\":\"RelatedPerson\"},\"search\":{\"mode\":\"match\"}}],\"link\":[{\"relation\":\"self\",\"url\":\"https://internal-dev.api.service.nhs.uk/validated-relationships-service-api/FHIR/R4/RelatedPerson?identifier=9730675929&patient%3Aidentifier=9730676399\"}],\"timestamp\":\"2024-04-24T10:15:31+00:00\",\"total\":1,\"type\":\"searchset\",\"resourceType\":\"Bundle\"}\\n'\n(Pdb) type(openapi_response.data)\n<class 'bytes'>\n
\n

I have worked round this by subclassing RequestsOpenAPIResponse so the data attribute returns a dict which works for me but I'd like to understand why I'm seeing this issue in the first place and what, if anything, I am doing wrong.

\n

Thanks

","upvoteCount":2,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

Exactly that; you can use the same function as standard one

\n
from openapi_core.deserializing.media_types.util import json_loads
","upvoteCount":0,"url":"https://github.com/python-openapi/openapi-core/discussions/838#discussioncomment-9220988"}}}
Discussion options

You must be logged in to vote

Exactly that; you can use the same function as standard one

from openapi_core.deserializing.media_types.util import json_loads

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@chris-young-12-nhs
Comment options

@p1c2u
Comment options

Answer selected by chris-young-12-nhs
@chris-young-12-nhs
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants