From fccf0d7d85d5625209790a99a9d1a47394e4d94a Mon Sep 17 00:00:00 2001 From: eisen0419 Date: Sat, 14 Nov 2026 20:21:48 +0800 Subject: [PATCH] rest: send Content-Type application/json for JSON bodies Since the v37 generator, create_* and delete_* operations whose OpenAPI consumes is */* serialize a JSON body and omit Content-Type. v36 set that header in rest.py. An intermediary that guesses a type for a bare string body, such as urllib3-future, then sends text/plain and the API server returns 415. When RESTClientObject JSON-serializes a body (body is not None) and no Content-Type is present in any casing, set application/json. Leave body-less requests unchanged: RFC 9110 section 8.3 only asks for the header when the message has content. Keep an explicit media type, including a lowercase header name, and copy the header map before inserting the default so request() does not mutate the caller's dict. Generated *_api.py serializers still ignore _content_type for */* operations; those files are regenerated, and _headers remains the override. kubernetes.aio already defaults the header. Refresh scripts/rest_client_patch.diff and move the client-go retry hunk so update-client.sh applies the stack with no offset. --- kubernetes/client/rest.py | 25 ++- kubernetes/utils/rest_test.py | 311 +++++++++++++++++++++++++++++ scripts/client_go_retry_patch.diff | 2 +- scripts/rest_client_patch.diff | 43 +++- 4 files changed, 372 insertions(+), 9 deletions(-) create mode 100644 kubernetes/utils/rest_test.py diff --git a/kubernetes/client/rest.py b/kubernetes/client/rest.py index 1a1015d75e..8dc155d34b 100644 --- a/kubernetes/client/rest.py +++ b/kubernetes/client/rest.py @@ -275,7 +275,15 @@ def read_request(check_retry_status=False): # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: - content_type = headers.get('Content-Type') + # Header names are case-insensitive. A second 'Content-Type' + # key next to 'content-type' would put two values on the wire. + content_type_key = None + content_type = None + for key, value in headers.items(): + if isinstance(key, str) and key.lower() == 'content-type': + content_type_key = key + content_type = value + break # JSON Patch is a list of operations; object patches for built-in # APIs use strategic merge: # https://kubernetes.io/docs/reference/using-api/api-concepts/#updates-to-existing-resources @@ -283,8 +291,8 @@ def read_request(check_retry_status=False): content_type == 'application/json-patch+json' and not isinstance(body, list) ): - headers['Content-Type'] = content_type = \ - 'application/strategic-merge-patch+json' + content_type = 'application/strategic-merge-patch+json' + headers[content_type_key] = content_type is_json = ( not content_type or contenttype_matches(content_type, 'application', 'json') @@ -305,6 +313,15 @@ def read_request(check_retry_status=False): request_body = None if body is not None: request_body = json.dumps(body) + # Operations whose OpenAPI consumes is */* omit + # Content-Type, but the body just serialized is JSON. + # RFC 9110 section 8.3: send Content-Type when the + # media type is known. A missing body is not content, + # so leave that request alone. Copy before inserting + # so request() does not add a key to the caller's map. + if content_type_key is None: + headers = dict(headers) + headers['Content-Type'] = 'application/json' r = self.pool_manager.request( method, url, @@ -327,7 +344,7 @@ def read_request(check_retry_status=False): # must del headers['Content-Type'], or the correct # Content-Type which generated by urllib3 will be # overwritten. - del headers['Content-Type'] + del headers[content_type_key] # Ensures that dict objects are serialized post_params = [(a, json.dumps(b)) if isinstance(b, dict) else (a,b) for a, b in post_params] r = self.pool_manager.request( diff --git a/kubernetes/utils/rest_test.py b/kubernetes/utils/rest_test.py new file mode 100644 index 0000000000..e2a43a04ab --- /dev/null +++ b/kubernetes/utils/rest_test.py @@ -0,0 +1,311 @@ +# Copyright 2026 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""RESTClientObject Content-Type defaults. + +Generated ``create_*`` and ``delete_*`` serializers accept ``_content_type`` +but do not emit a header when the OpenAPI operation consumes ``*/*``. Those +files are regenerated by ``scripts/update-client.sh``, so the default lives +in ``kubernetes/client/rest.py`` (and ``scripts/rest_client_patch.diff``). +``_headers`` is the supported override. ``kubernetes.aio`` already defaults +the header in its generated REST client and is not patched here. + +``kubernetes/test`` is the OpenAPI-generated suite. This file sits with the +other hand-written REST tests (``keepalive_test.py``, ``retry_test.py``) so +``tox -e py`` collects it. +""" + +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +import json +import threading +import unittest +from unittest import mock + +import urllib3 + +from kubernetes.client import ( + ApiClient, + BatchV1Api, + Configuration, + V1DeleteOptions, +) +from kubernetes.client.rest import RESTClientObject + + +JOB_BODY = { + 'apiVersion': 'batch/v1', + 'kind': 'Job', + 'metadata': {'name': 'x'}, + 'spec': { + 'template': { + 'spec': { + 'containers': [{'name': 'c', 'image': 'i'}], + }, + }, + }, +} + + +def _sent(rest_client): + kwargs = rest_client.pool_manager.request.call_args.kwargs + return kwargs.get('headers') or {}, kwargs.get('body') + + +class RESTClientObjectContentTypeTest(unittest.TestCase): + def setUp(self): + self.rest_client = RESTClientObject(Configuration()) + self.rest_client.pool_manager = mock.Mock() + response = urllib3.HTTPResponse( + body=b'{}', status=200, reason='OK') + self.rest_client.pool_manager.request.return_value = response + + def _request(self, method, headers=None, body=None, post_params=None): + self.rest_client.request( + method, + 'http://127.0.0.1/api', + headers=headers, + body=body, + post_params=post_params, + ) + return _sent(self.rest_client) + + def test_json_body_without_header_sets_application_json(self): + caller = {'Accept': 'application/json'} + + headers, body = self._request('POST', headers=caller, body={'a': 1}) + + self.assertEqual('application/json', headers['Content-Type']) + self.assertEqual('{"a": 1}', body) + self.assertEqual({'Accept': 'application/json'}, caller) + self.assertIsNot(headers, caller) + + def test_missing_body_does_not_set_content_type(self): + for method in ('POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE'): + with self.subTest(method=method): + self.rest_client.pool_manager.request.reset_mock() + headers, body = self._request( + method, headers={'Accept': 'json'}) + + self.assertNotIn('Content-Type', headers) + self.assertIsNone(body) + self.assertNotIn('content-type', headers) + + def test_get_and_head_do_not_set_content_type(self): + for method in ('GET', 'HEAD'): + with self.subTest(method=method): + self.rest_client.pool_manager.request.reset_mock() + headers, body = self._request( + method, headers={'Accept': 'json'}) + + self.assertNotIn('Content-Type', headers) + self.assertIsNone(body) + + def test_lowercase_content_type_is_not_duplicated(self): + caller = {'content-type': 'application/merge-patch+json'} + + headers, body = self._request('POST', headers=caller, body={'a': 1}) + + names = [key for key in headers if key.lower() == 'content-type'] + self.assertEqual(['content-type'], names) + self.assertEqual( + 'application/merge-patch+json', headers['content-type']) + self.assertEqual('{"a": 1}', body) + self.assertEqual(caller, headers) + + def test_explicit_json_content_type_is_preserved(self): + caller = {'Content-Type': 'application/json; charset=utf-8'} + + headers, body = self._request('PUT', headers=caller, body={'a': 1}) + + self.assertEqual( + 'application/json; charset=utf-8', headers['Content-Type']) + self.assertEqual('{"a": 1}', body) + self.assertIs(headers, caller) + + def test_json_patch_list_keeps_json_patch_media_type(self): + operations = [{'op': 'replace', 'path': '/a', 'value': 1}] + caller = {'Content-Type': 'application/json-patch+json'} + + headers, body = self._request('PATCH', headers=caller, body=operations) + + self.assertEqual( + 'application/json-patch+json', headers['Content-Type']) + self.assertEqual(operations, json.loads(body)) + + def test_object_patch_still_selects_strategic_merge(self): + caller = {'content-type': 'application/json-patch+json'} + + headers, body = self._request( + 'PATCH', headers=caller, body={'data': {'key': 'value'}}, + ) + + self.assertEqual( + 'application/strategic-merge-patch+json', + headers['content-type'], + ) + self.assertNotIn('Content-Type', headers) + self.assertEqual({'data': {'key': 'value'}}, json.loads(body)) + self.assertIs(headers, caller) + + def test_structured_yaml_keeps_its_media_type(self): + caller = {'Content-Type': 'application/apply-patch+yaml'} + + headers, body = self._request('PATCH', headers=caller, body={'a': 1}) + + self.assertEqual( + 'application/apply-patch+yaml', headers['Content-Type']) + self.assertEqual('{"a": 1}', body) + + def test_plain_text_body_is_not_relabeled_json(self): + caller = {'Content-Type': 'text/plain'} + + headers, body = self._request('POST', headers=caller, body='hello') + + self.assertEqual('text/plain', headers['Content-Type']) + self.assertEqual('hello', body) + + def test_untyped_string_body_is_json(self): + headers, body = self._request('POST', body='hello') + + self.assertEqual('application/json', headers['Content-Type']) + self.assertEqual('"hello"', body) + + def test_form_and_multipart_are_not_relabeled_json(self): + form = {'Content-Type': 'application/x-www-form-urlencoded'} + headers, body = self._request( + 'POST', headers=form, post_params=[('a', 'b')], + ) + self.assertEqual( + 'application/x-www-form-urlencoded', headers['Content-Type']) + self.assertIsNone(body) + + multipart = {'Content-Type': 'multipart/form-data'} + headers, body = self._request( + 'POST', headers=multipart, post_params=[('file', 'data')], + ) + self.assertNotIn('Content-Type', headers) + self.assertIsNone(body) + + +class _RecordingHandler(BaseHTTPRequestHandler): + def do_POST(self): + self._record() + + def do_DELETE(self): + self._record() + + def _record(self): + length = int(self.headers.get('Content-Length') or 0) + self.server.requests.append({ + 'method': self.command, + 'path': self.path, + 'content_type': self.headers.get('Content-Type'), + 'content_types': self.headers.get_all('Content-Type'), + 'body': self.rfile.read(length), + }) + payload = self.server.response_body + self.send_response(self.server.response_status) + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(payload))) + self.end_headers() + self.wfile.write(payload) + + def log_message(self, format, *args): + pass + + +class BatchJobContentTypeTest(unittest.TestCase): + def setUp(self): + self.server = ThreadingHTTPServer(('127.0.0.1', 0), _RecordingHandler) + self.server.requests = [] + self.server.response_body = b'{}' + self.server.response_status = 201 + self.thread = threading.Thread(target=self.server.serve_forever) + self.thread.start() + self.api_client = ApiClient(Configuration( + host='http://127.0.0.1:%d' % self.server.server_port, + proxy='', + no_proxy='', + )) + self.api = BatchV1Api(self.api_client) + + def tearDown(self): + self.server.shutdown() + self.thread.join() + self.server.server_close() + self.api_client.close() + + def test_create_namespaced_job_sends_json_content_type(self): + created = self.api.create_namespaced_job(namespace='n', body=JOB_BODY) + + self.assertIsNotNone(created) + request = self.server.requests[-1] + self.assertEqual('POST', request['method']) + self.assertEqual('/apis/batch/v1/namespaces/n/jobs', request['path']) + self.assertEqual(['application/json'], request['content_types']) + self.assertEqual(JOB_BODY, json.loads(request['body'])) + + def test_delete_namespaced_job_with_options_sends_json_content_type(self): + self.server.response_status = 200 + options = V1DeleteOptions( + propagation_policy='Background', + grace_period_seconds=5, + ) + + self.api.delete_namespaced_job( + name='x', namespace='n', body=options, + ) + + request = self.server.requests[-1] + self.assertEqual('DELETE', request['method']) + self.assertEqual( + '/apis/batch/v1/namespaces/n/jobs/x', request['path'], + ) + self.assertEqual(['application/json'], request['content_types']) + self.assertEqual( + {'propagationPolicy': 'Background', 'gracePeriodSeconds': 5}, + json.loads(request['body']), + ) + + def test_delete_without_body_omits_content_type(self): + self.server.response_status = 200 + + self.api.delete_namespaced_job(name='x', namespace='n') + + request = self.server.requests[-1] + self.assertEqual(b'', request['body']) + self.assertIsNone(request['content_type']) + self.assertIsNone(request['content_types']) + + def test_explicit_headers_override_the_json_default(self): + self.api.create_namespaced_job( + namespace='n', + body=JOB_BODY, + _headers={'Content-Type': 'application/merge-patch+json'}, + ) + + request = self.server.requests[-1] + self.assertEqual( + ['application/merge-patch+json'], request['content_types'], + ) + + def test_lowercase_header_is_a_single_content_type_on_the_wire(self): + self.api.create_namespaced_job( + namespace='n', + body=JOB_BODY, + _headers={'content-type': 'application/json'}, + ) + + request = self.server.requests[-1] + self.assertEqual(['application/json'], request['content_types']) diff --git a/scripts/client_go_retry_patch.diff b/scripts/client_go_retry_patch.diff index b3a23a9b3c..b9a0461276 100644 --- a/scripts/client_go_retry_patch.diff +++ b/scripts/client_go_retry_patch.diff @@ -83,7 +83,7 @@ diff --git a/kubernetes/client/rest.py b/kubernetes/client/rest.py try: # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: -@@ -331,16 +365,67 @@ +@@ -348,16 +382,67 @@ raise ApiException(status=0, reason=msg) # For `GET`, `HEAD` else: diff --git a/scripts/rest_client_patch.diff b/scripts/rest_client_patch.diff index c852f5b454..5b4feae915 100644 --- a/scripts/rest_client_patch.diff +++ b/scripts/rest_client_patch.diff @@ -1,10 +1,20 @@ diff --git a/kubernetes/client/rest.py b/kubernetes/client/rest.py --- a/kubernetes/client/rest.py +++ b/kubernetes/client/rest.py -@@ -239,6 +239,15 @@ +@@ -238,7 +238,24 @@ + # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: - content_type = headers.get('Content-Type') +- content_type = headers.get('Content-Type') ++ # Header names are case-insensitive. A second 'Content-Type' ++ # key next to 'content-type' would put two values on the wire. ++ content_type_key = None ++ content_type = None ++ for key, value in headers.items(): ++ if isinstance(key, str) and key.lower() == 'content-type': ++ content_type_key = key ++ content_type = value ++ break + # JSON Patch is a list of operations; object patches for built-in + # APIs use strategic merge: + # https://kubernetes.io/docs/reference/using-api/api-concepts/#updates-to-existing-resources @@ -12,8 +22,33 @@ diff --git a/kubernetes/client/rest.py b/kubernetes/client/rest.py + content_type == 'application/json-patch+json' + and not isinstance(body, list) + ): -+ headers['Content-Type'] = content_type = \ -+ 'application/strategic-merge-patch+json' ++ content_type = 'application/strategic-merge-patch+json' ++ headers[content_type_key] = content_type is_json = ( not content_type or contenttype_matches(content_type, 'application', 'json') +@@ -259,6 +276,15 @@ + request_body = None + if body is not None: + request_body = json.dumps(body) ++ # Operations whose OpenAPI consumes is */* omit ++ # Content-Type, but the body just serialized is JSON. ++ # RFC 9110 section 8.3: send Content-Type when the ++ # media type is known. A missing body is not content, ++ # so leave that request alone. Copy before inserting ++ # so request() does not add a key to the caller's map. ++ if content_type_key is None: ++ headers = dict(headers) ++ headers['Content-Type'] = 'application/json' + r = self.pool_manager.request( + method, + url, +@@ -281,7 +307,7 @@ + # must del headers['Content-Type'], or the correct + # Content-Type which generated by urllib3 will be + # overwritten. +- del headers['Content-Type'] ++ del headers[content_type_key] + # Ensures that dict objects are serialized + post_params = [(a, json.dumps(b)) if isinstance(b, dict) else (a,b) for a, b in post_params] + r = self.pool_manager.request(