Title: How to deal with openapi_core.casting.schemas.exceptions.CastError: Failed to cast value to object error · python-openapi/openapi-core · Discussion #838 · GitHub
Open Graph Title: How to deal with openapi_core.casting.schemas.exceptions.CastError: Failed to cast value to object error · python-openapi/openapi-core · Discussion #838
X Title: How to deal with openapi_core.casting.schemas.exceptions.CastError: Failed to cast value to object error · python-openapi/openapi-core · Discussion #838
Description: How to deal with openapi_core.casting.schemas.exceptions.CastError: Failed to cast value to object error
Open Graph Description: Hi, I'm using openapi-core to validate responses against this OpenAPI schema: https://digital.nhs.uk/restapi/oas/573240 The error I'm getting is this: def _cast_proparties(self, value: Any, schema_...
X Description: Hi, I'm using openapi-core to validate responses against this OpenAPI schema: https://digital.nhs.uk/restapi/oas/573240 The error I'm getting is this: def _cast_proparties(self, value: Any,...
Opengraph URL: https://github.com/python-openapi/openapi-core/discussions/838
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"How to deal with openapi_core.casting.schemas.exceptions.CastError: Failed to cast value to object error","text":"Hi,
\nI'm using openapi-core to validate responses against this OpenAPI schema:
\nhttps://digital.nhs.uk/restapi/oas/573240
\nThe error I'm getting is this:
\n def _cast_proparties(self, value: Any, schema_only: bool = False) -> Any:\n if not isinstance(value, dict):\n> raise CastError(value, self.schema[\"type\"])\nE openapi_core.casting.schemas.exceptions.CastError: Failed to cast value to object type: b'\\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-23T17:01:35+00:00\",\"total\":1,\"type\":\"searchset\",\"resourceType\":\"Bundle\"}\\n'\n\n../../../../../Library/Caches/pypoetry/virtualenvs/tests-puqZLLMN-py3.9/lib/python3.9/site-packages/openapi_core/casting/schemas/casters.py:114: CastError\n
\nThe 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
\nWhere 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
\nSo 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 if not isinstance(value, dict): \n \n\n \n \n raise CastError(value, self.schema[\"type\"]) \n \n
\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
\nI 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.
\nThanks
","upvoteCount":2,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Exactly that; you can use the same function as standard one
\nfrom openapi_core.deserializing.media_types.util import json_loads
","upvoteCount":0,"url":"https://github.com/python-openapi/openapi-core/discussions/838#discussioncomment-9220988"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:d9ce6882-9f52-0090-aec9-894a7f0c47d4 |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | D936:3CFA0F:347BFB:4A6DD8:69786DFF |
| html-safe-nonce | 9205355b714144c7ab81af0a88aee60bc112e0a0d9fecc3f5303da2dbedee29f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOTM2OjNDRkEwRjozNDdCRkI6NEE2REQ4OjY5Nzg2REZGIiwidmlzaXRvcl9pZCI6IjM1MDc2MTk3Mjk5MjczMzU0MjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a2a677bf4ab27e3ff07ab5243292220c4a5810c691132767beec75ecbff2936b |
| hovercard-subject-tag | discussion:6553488 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/python-openapi/openapi-core/838/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/5ac4e15dc63fc7fab771cbd2bb1d8a7a0f2046731e0e6d3f70ef1e70b900448e/python-openapi/openapi-core/discussions/838 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5ac4e15dc63fc7fab771cbd2bb1d8a7a0f2046731e0e6d3f70ef1e70b900448e/python-openapi/openapi-core/discussions/838 |
| og:image:alt | Hi, I'm using openapi-core to validate responses against this OpenAPI schema: https://digital.nhs.uk/restapi/oas/573240 The error I'm getting is this: def _cast_proparties(self, value: Any, schema_... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2981c597c945c1d90ac6fa355ce7929b2f413dfe7872ca5c435ee53a24a1de50 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-openapi/openapi-core git https://github.com/python-openapi/openapi-core.git |
| octolytics-dimension-user_id | 126442889 |
| octolytics-dimension-user_login | python-openapi |
| octolytics-dimension-repository_id | 104200746 |
| octolytics-dimension-repository_nwo | python-openapi/openapi-core |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 104200746 |
| octolytics-dimension-repository_network_root_nwo | python-openapi/openapi-core |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 520b65a872113b919c1bbdb03834a50af15859fd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width