René's URL Explorer Experiment


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

direct link

Domain: patch-diff.githubusercontent.com


Hey, it has json ld scripts:
{"@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,

\n

I'm using openapi-core to validate responses against this OpenAPI schema:

\n

https://digital.nhs.uk/restapi/oas/573240

\n

The 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
\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"}}}

route-pattern/_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format)
route-controllervoltron_discussions_fragments
route-actiondiscussion_layout
fetch-noncev2:d9ce6882-9f52-0090-aec9-894a7f0c47d4
current-catalog-service-hash9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be
request-idD936:3CFA0F:347BFB:4A6DD8:69786DFF
html-safe-nonce9205355b714144c7ab81af0a88aee60bc112e0a0d9fecc3f5303da2dbedee29f
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOTM2OjNDRkEwRjozNDdCRkI6NEE2REQ4OjY5Nzg2REZGIiwidmlzaXRvcl9pZCI6IjM1MDc2MTk3Mjk5MjczMzU0MjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmaca2a677bf4ab27e3ff07ab5243292220c4a5810c691132767beec75ecbff2936b
hovercard-subject-tagdiscussion:6553488
github-keyboard-shortcutsrepository,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/discussions_fragments/discussion_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/python-openapi/openapi-core/838/discussion_layout
twitter:imagehttps://opengraph.githubassets.com/5ac4e15dc63fc7fab771cbd2bb1d8a7a0f2046731e0e6d3f70ef1e70b900448e/python-openapi/openapi-core/discussions/838
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/5ac4e15dc63fc7fab771cbd2bb1d8a7a0f2046731e0e6d3f70ef1e70b900448e/python-openapi/openapi-core/discussions/838
og:image:altHi, 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None2981c597c945c1d90ac6fa355ce7929b2f413dfe7872ca5c435ee53a24a1de50
turbo-cache-controlno-preview
go-importgithub.com/python-openapi/openapi-core git https://github.com/python-openapi/openapi-core.git
octolytics-dimension-user_id126442889
octolytics-dimension-user_loginpython-openapi
octolytics-dimension-repository_id104200746
octolytics-dimension-repository_nwopython-openapi/openapi-core
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id104200746
octolytics-dimension-repository_network_root_nwopython-openapi/openapi-core
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release520b65a872113b919c1bbdb03834a50af15859fd
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-openapi%2Fopenapi-core%2Fdiscussions%2F838
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-openapi%2Fopenapi-core%2Fdiscussions%2F838
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fdiscussions_fragments%2Fdiscussion_layout&source=header-repo&source_repo=python-openapi%2Fopenapi-core
Reloadhttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
Reloadhttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
Reloadhttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
python-openapi https://patch-diff.githubusercontent.com/python-openapi
openapi-corehttps://patch-diff.githubusercontent.com/python-openapi/openapi-core
Please reload this pagehttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Fpython-openapi%2Fopenapi-core
Fork 136 https://patch-diff.githubusercontent.com/login?return_to=%2Fpython-openapi%2Fopenapi-core
Star 356 https://patch-diff.githubusercontent.com/login?return_to=%2Fpython-openapi%2Fopenapi-core
Code https://patch-diff.githubusercontent.com/python-openapi/openapi-core
Issues 74 https://patch-diff.githubusercontent.com/python-openapi/openapi-core/issues
Pull requests 14 https://patch-diff.githubusercontent.com/python-openapi/openapi-core/pulls
Discussions https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions
Actions https://patch-diff.githubusercontent.com/python-openapi/openapi-core/actions
Projects 0 https://patch-diff.githubusercontent.com/python-openapi/openapi-core/projects
Security 0 https://patch-diff.githubusercontent.com/python-openapi/openapi-core/security
Insights https://patch-diff.githubusercontent.com/python-openapi/openapi-core/pulse
Code https://patch-diff.githubusercontent.com/python-openapi/openapi-core
Issues https://patch-diff.githubusercontent.com/python-openapi/openapi-core/issues
Pull requests https://patch-diff.githubusercontent.com/python-openapi/openapi-core/pulls
Discussions https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions
Actions https://patch-diff.githubusercontent.com/python-openapi/openapi-core/actions
Projects https://patch-diff.githubusercontent.com/python-openapi/openapi-core/projects
Security https://patch-diff.githubusercontent.com/python-openapi/openapi-core/security
Insights https://patch-diff.githubusercontent.com/python-openapi/openapi-core/pulse
Answered https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#discussioncomment-9220988
p1c2uhttps://patch-diff.githubusercontent.com/p1c2u
chris-young-12-nhs https://patch-diff.githubusercontent.com/chris-young-12-nhs
Q&Ahttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/categories/q-a
How to deal with openapi_core.casting.schemas.exceptions.CastError: Failed to cast value to object error https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#top
chris-young-12-nhs https://patch-diff.githubusercontent.com/chris-young-12-nhs
Answered https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#discussioncomment-9220988
p1c2uhttps://patch-diff.githubusercontent.com/p1c2u
Return to tophttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#top
Please reload this pagehttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
chris-young-12-nhs https://patch-diff.githubusercontent.com/chris-young-12-nhs
Apr 24, 2024 https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#discussion-6553488
https://digital.nhs.uk/restapi/oas/573240https://digital.nhs.uk/restapi/oas/573240
openapi-core/openapi_core/casting/schemas/casters.pyhttps://github.com/python-openapi/openapi-core/blob/99af2d31d844d5bc0b942e0ec0bc9b108e82ec79/openapi_core/casting/schemas/casters.py#L113-L114
99af2d3https://patch-diff.githubusercontent.com/python-openapi/openapi-core/commit/99af2d31d844d5bc0b942e0ec0bc9b108e82ec79
Give feedback.https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
p1c2u https://patch-diff.githubusercontent.com/p1c2u
Apr 25, 2024 https://github.com/python-openapi/openapi-core/discussions/838#discussioncomment-9220988
View full answer https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#discussioncomment-9220988
Oldest https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838?sort=old
Newest https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838?sort=new
Top https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838?sort=top
Please reload this pagehttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
p1c2u https://patch-diff.githubusercontent.com/p1c2u
Apr 24, 2024 https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#discussioncomment-9218065
@chris-young-12-nhshttps://github.com/chris-young-12-nhs
Give feedback.https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/chris-young-12-nhs
Please reload this pagehttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
Please reload this pagehttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
chris-young-12-nhshttps://patch-diff.githubusercontent.com/chris-young-12-nhs
Apr 24, 2024 https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#discussioncomment-9218931
@p1c2uhttps://github.com/p1c2u
https://www.hl7.org/fhir/overview.htmlhttps://www.hl7.org/fhir/overview.html
https://digital.nhs.uk/restapi/oas/573240https://digital.nhs.uk/restapi/oas/573240
https://openapi-core.readthedocs.io/en/latest/customizations/extra_media_type_deserializers.htmlhttps://openapi-core.readthedocs.io/en/latest/customizations/extra_media_type_deserializers.html
Give feedback.https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/p1c2u
Please reload this pagehttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
Please reload this pagehttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
p1c2uhttps://patch-diff.githubusercontent.com/p1c2u
Apr 25, 2024 https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#discussioncomment-9220988
Give feedback.https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
chris-young-12-nhshttps://patch-diff.githubusercontent.com/chris-young-12-nhs
https://patch-diff.githubusercontent.com/chris-young-12-nhs
Please reload this pagehttps://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
chris-young-12-nhshttps://patch-diff.githubusercontent.com/chris-young-12-nhs
Apr 25, 2024 https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838#discussioncomment-9221681
@p1c2uhttps://github.com/p1c2u
Give feedback.https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
Sign up for freehttps://patch-diff.githubusercontent.com/join?source=comment-repo
Sign in to commenthttps://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-openapi%2Fopenapi-core%2Fdiscussions%2F838
🙏 Q&A https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/categories/q-a
https://patch-diff.githubusercontent.com/chris-young-12-nhs
https://patch-diff.githubusercontent.com/p1c2u
https://patch-diff.githubusercontent.com/python-openapi/openapi-core/discussions/838
https://patch-diff.githubusercontent.com/settings/replies?return_to=1
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.