Title: ResponseValidator: how to handle mime-type with character encoding details? · Issue #378 · python-openapi/openapi-core · GitHub
Open Graph Title: ResponseValidator: how to handle mime-type with character encoding details? · Issue #378 · python-openapi/openapi-core
X Title: ResponseValidator: how to handle mime-type with character encoding details? · Issue #378 · python-openapi/openapi-core
Description: While using ResponseValidator.validate I noticed validation is sensitive to the content-type header including character encoding details. In case an HTTP response includes content-type: application/json; charset=utf-8 validation fails: M...
Open Graph Description: While using ResponseValidator.validate I noticed validation is sensitive to the content-type header including character encoding details. In case an HTTP response includes content-type: application...
X Description: While using ResponseValidator.validate I noticed validation is sensitive to the content-type header including character encoding details. In case an HTTP response includes content-type: application...
Opengraph URL: https://github.com/python-openapi/openapi-core/issues/378
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ResponseValidator: how to handle mime-type with character encoding details?","articleBody":"While using `ResponseValidator.validate` I noticed validation is sensitive to the content-type header including character encoding details. \r\n\r\nIn case an HTTP response includes `content-type: application/json; charset=utf-8` validation fails: `MediaTypeNotFound(mimetype='application/json; charset=utf-8', availableMimetypes=['application/json'])`\r\n\r\n(As quick work I tried adding `application/json; charset=utf-8` to the openapi spec as additional mime-type but that results in vague errors such as raised in https://github.com/p1c2u/openapi-core/issues/128)\r\n\r\nI can manually overwrite `mimetype` of the response before validating but that feels a bit wrong .. one could reason that `application/json; charset=utf-8` should be accepted and treated equal as `application/json`. Any comment?\r\n\r\nSee below for code that reproduces this situation.\r\n\r\n```\r\nimport json\r\nfrom openapi_core import create_spec\r\nfrom openapi_core.validation.request.validators import RequestValidator\r\nfrom openapi_core.validation.response.validators import ResponseValidator\r\nfrom openapi_core.testing import MockRequest\r\nfrom openapi_core.testing import MockResponse\r\n\r\ndef validate(spec_dict, mime_type):\r\n spec = create_spec(spec_dict)\r\n\r\n openapi_request = MockRequest('localhost', 'get', '/v1/cars')\r\n validator = RequestValidator(spec)\r\n result = validator.validate(openapi_request)\r\n request_errors = result.errors\r\n\r\n data = json.dumps({\r\n 'cars' :[\r\n {\r\n 'name': 'car1',\r\n },\r\n {\r\n 'name': 'car2',\r\n }\r\n ]\r\n })\r\n openapi_response = MockResponse(data, mimetype=mime_type)\r\n\r\n # Workaround\r\n #if openapi_response.mimetype == 'application/json; charset=utf-8':\r\n # openapi_response.mimetype = 'application/json'\r\n\r\n validator = ResponseValidator(spec)\r\n result = validator.validate(openapi_request, openapi_response)\r\n response_errors = result.errors\r\n\r\n print('Request errors: {} Response errors: {}'.format(request_errors, response_errors))\r\n\r\n\r\nspec = {\r\n 'openapi': '3.0.0',\r\n 'info': {\r\n 'version': '0.1',\r\n 'title': 'List of objects',\r\n 'description': 'Test for list of objects'\r\n },\r\n 'paths': {\r\n '/v1/cars': {\r\n 'get': {\r\n 'description': 'Retrieve all cars',\r\n 'responses': {\r\n '200': {\r\n 'description': 'Successfully retrieved all cars',\r\n 'content': {\r\n 'application/json': {\r\n 'schema': {\r\n 'type': 'object',\r\n 'properties': {\r\n 'cars': {\r\n 'type': 'array',\r\n 'items': {\r\n '$ref': '#/components/schemas/Car'\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n },\r\n 'components': {\r\n 'schemas': {\r\n 'Car': {\r\n 'type': 'object',\r\n 'properties': {\r\n 'name': {\r\n 'type': 'string'\r\n }\r\n }\r\n }\r\n }\r\n }\r\n}\r\nvalidate(spec, 'application/json')\r\nvalidate(spec, 'application/json; charset=utf-8')\r\n```","author":{"url":"https://github.com/erikbos","@type":"Person","name":"erikbos"},"datePublished":"2021-10-31T13:22:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/378/openapi-core/issues/378"}
| 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:6c3773e0-3ace-cd2c-0cea-80233fa0ea97 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C350:18807F:35374C2:49A4DFE:6977C842 |
| html-safe-nonce | 8add001ae192ae914d59e9a86276e961b0ac0790b97ac1d189d595ea16f72f6a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzUwOjE4ODA3RjozNTM3NEMyOjQ5QTRERkU6Njk3N0M4NDIiLCJ2aXNpdG9yX2lkIjoiMjI2NzYyMTk0MjM5NzIyNTAyNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a21aaf46ee3f55d483585b8bcf7ac825890e0d53bc7e2cc2a00ba0afd52c1b7e |
| hovercard-subject-tag | issue:1040469019 |
| 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/378/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c67935369e77f9aa7ee0cfbbea4ed5c5987a5845f30467cfcf143496193ea9a8/python-openapi/openapi-core/issues/378 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c67935369e77f9aa7ee0cfbbea4ed5c5987a5845f30467cfcf143496193ea9a8/python-openapi/openapi-core/issues/378 |
| og:image:alt | While using ResponseValidator.validate I noticed validation is sensitive to the content-type header including character encoding details. In case an HTTP response includes content-type: application... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | erikbos |
| hostname | github.com |
| expected-hostname | github.com |
| None | 173f8c2eae2e017de550dd28a9ea88ad5c1e52c70df7ea05bcd820330b3b2fec |
| 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 | 7b19554cad55a536fac18eeedb416dd87c37b1f5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width