Title: [Bug]: No casting applied if type is object · Issue #800 · python-openapi/openapi-core · GitHub
Open Graph Title: [Bug]: No casting applied if type is object · Issue #800 · python-openapi/openapi-core
X Title: [Bug]: No casting applied if type is object · Issue #800 · python-openapi/openapi-core
Description: Actual Behavior When validating a request, I get a different validation error depending on the structure of the schema. If I have the following schema: Pet: type: object # <-- this causes the difference properties: id: type: integer Pet2...
Open Graph Description: Actual Behavior When validating a request, I get a different validation error depending on the structure of the schema. If I have the following schema: Pet: type: object # <-- this causes the diffe...
X Description: Actual Behavior When validating a request, I get a different validation error depending on the structure of the schema. If I have the following schema: Pet: type: object # <-- this causes the di...
Opengraph URL: https://github.com/python-openapi/openapi-core/issues/800
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: No casting applied if type is object","articleBody":"### Actual Behavior\n\nWhen validating a request, I get a different validation error depending on the structure of the schema.\r\n\r\nIf I have the following schema:\r\n```yaml\r\nPet:\r\n type: object # \u003c-- this causes the difference\r\n properties:\r\n id:\r\n type: integer\r\nPet2:\r\n allOf:\r\n - properties:\r\n id:\r\n type: integer\r\n```\r\nand send a request with body `{\"id\": \"hello\"}`,\r\n\r\nwith the first schema I get:\r\n```\r\nopenapi_core.casting.schemas.exceptions.CastError: Failed to cast value to integer type: hello\r\n```\r\nwhereas in the second one I get:\r\n```\r\nopenapi_core.validation.schemas.exceptions.InvalidSchemaValue: Value {'id': 'hello'} not valid for schema of type any: (\u003cValidationError: \"'hello' is not of type 'integer'\"\u003e,)\r\n```\n\n### Expected Behavior\n\nThese schemas are equivalent, and as such I expected to get the same error. Specifically, I expected the validation error in both cases rather than the casting error as I asked for request validation.\r\nAlso, removing the `type: object` in the first schema seems to bypass casting entirely and leads to the same validation error as the second schema.\n\n### Steps to Reproduce\n\n\u003cdetails\u003e\r\n\u003csummary\u003eschema.yaml\u003c/summary\u003e\r\n\r\n```yaml\r\nopenapi: \"3.0.0\"\r\ninfo:\r\n title: \"Sample API\"\r\n version: \"1.0.0\"\r\nservers:\r\n - url: /\r\npaths:\r\n /pets:\r\n post:\r\n summary: Create a pet\r\n operationId: createPet\r\n requestBody:\r\n content:\r\n application/json:\r\n schema:\r\n $ref: \"#/components/schemas/Pet\"\r\n responses:\r\n '201':\r\n description: Created\r\n content:\r\n application/json:\r\n schema:\r\n $ref: \"#/components/schemas/Pet\"\r\n /pets2:\r\n post:\r\n summary: Create a pet\r\n operationId: createPet2\r\n requestBody:\r\n content:\r\n application/json:\r\n schema:\r\n $ref: \"#/components/schemas/Pet2\"\r\n responses:\r\n '201':\r\n description: Created\r\n content:\r\n application/json:\r\n schema:\r\n $ref: \"#/components/schemas/Pet\"\r\ncomponents:\r\n schemas:\r\n Pet:\r\n type: object # \u003c-- this causes the difference\r\n properties:\r\n id:\r\n type: integer\r\n Pet2:\r\n allOf:\r\n - properties:\r\n id:\r\n type: integer\r\n```\r\n\r\n\u003c/details\u003e\r\n\r\n```python\r\nimport json\r\nimport traceback\r\nimport openapi_core\r\nfrom openapi_core.contrib.starlette import StarletteOpenAPIRequest\r\nfrom starlette.requests import Request\r\n\r\n\r\ndef starlette_request(path: str):\r\n return Request({\r\n \"type\": \"http\",\r\n \"method\": \"POST\",\r\n \"path\": path,\r\n \"headers\": [(b\"content-type\", b\"application/json\")],\r\n \"query_string\": \"\",\r\n })\r\n\r\n\r\nbody = json.dumps({\"id\": \"hello\"})\r\n\r\nopenapi = openapi_core.OpenAPI.from_file_path(\"schema.yaml\")\r\ntry:\r\n openapi.validate_request(\r\n StarletteOpenAPIRequest(starlette_request(\"/pets\"), body=body)\r\n )\r\nexcept openapi_core.exceptions.OpenAPIError:\r\n traceback.print_exc()\r\n\r\ntry:\r\n openapi.validate_request(\r\n StarletteOpenAPIRequest(starlette_request(\"/pets2\"), body=body)\r\n )\r\nexcept openapi_core.exceptions.OpenAPIError:\r\n traceback.print_exc()\r\n```\n\n### OpenAPI Core Version\n\n0.19.0\n\n### OpenAPI Core Integration\n\nstarlette\n\n### Affected Area(s)\n\ncasting, validation\n\n### References\n\n_No response_\n\n### Anything else we need to know?\n\n_No response_\n\n### Would you like to implement a fix?\n\nNone","author":{"url":"https://github.com/jonathanberthias","@type":"Person","name":"jonathanberthias"},"datePublished":"2024-03-01T09:44:35.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/800/openapi-core/issues/800"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:747ba6f1-8d97-32bc-bf81-62ed4be93393 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 96D2:11BAEC:D419BA:1231F4E:6977659B |
| html-safe-nonce | 5d19cea83a8a07a78d0ae4f55ab02337dd5c58215abede4935ba8598ba69d885 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NkQyOjExQkFFQzpENDE5QkE6MTIzMUY0RTo2OTc3NjU5QiIsInZpc2l0b3JfaWQiOiI2MjE5MTMwMTk1NjEzODAzOTMxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 847d68b02610c65e2da6a0cafcc1157721e743d3a65e6a6de56cc0720eaff7ff |
| hovercard-subject-tag | issue:2163009736 |
| github-keyboard-shortcuts | repository,issues,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/issues/show/python-openapi/openapi-core/800/issue_layout |
| twitter:image | https://opengraph.githubassets.com/64d70ab82377e05f46257abc6699b5373a8e597e8815bc4285035e63f94c4f70/python-openapi/openapi-core/issues/800 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/64d70ab82377e05f46257abc6699b5373a8e597e8815bc4285035e63f94c4f70/python-openapi/openapi-core/issues/800 |
| og:image:alt | Actual Behavior When validating a request, I get a different validation error depending on the structure of the schema. If I have the following schema: Pet: type: object # <-- this causes the diffe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jonathanberthias |
| hostname | github.com |
| expected-hostname | github.com |
| None | 425384cc392ce2ddd1a7a0c1e7043ab4192d4c04452a2ed7f2f38d2fa9293b5a |
| 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 | 72de26f7ca9bdb0559ebf1e63e6c685ee2c520ce |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width