[python] serialize structured YAML bodies

Merged
TA
4mo ago
+100-15

The synchronous Python client only serializes structured request bodies
when Content-Type contains json. Kubernetes server-side apply sends
dictionary bodies as application/apply-patch+yaml, so those requests
reach the unsupported-content fallback. The downstream client has
carried a special case since
kubernetes-client/python 5529dedc.

JSON is valid YAML 1.2. Use the existing JSON serializer for
structured YAML bodies while preserving pass-through for serialized
str and bytes values. This covers both standard YAML media types and
+yaml structured suffixes.

Fixes #24083.


Summary by cubic

Serialize structured YAML request bodies in the Python REST client using the existing JSON encoder to avoid unsupported-content fallbacks. Supports application/yaml and +yaml types while preserving pass-through for str/bytes.

  • Bug Fixes
    • JSON-encode non-string bodies when Content-Type includes yaml (e.g., application/yaml, application/apply-patch+yaml; charset=utf-8), while keeping pass-through for pre-serialized YAML.
    • Updated rest.mustache and Python samples; added unit tests in the petstore client to verify JSON encoding for dicts and pass-through for string bodies.