From d1f33a8bb14e71200ec7026c71939c74b656fc06 Mon Sep 17 00:00:00 2001 From: Douglas Strodtman Date: Thu, 13 Aug 2026 12:34:50 -0400 Subject: [PATCH] Fix #178 Handle non-object request bodies in the 3.0 renderer cca8fc3 branched on the schema type before dumping a request body, so a schema that is not an object gets dumped whole rather than having its absent 'properties' key indexed. That landed in the 3.1 renderer only, and the two renderers carry separate copies of this path, so the same spec crashed with KeyError: 'properties' on 3.0 and rendered on 3.1. Ported verbatim for parity. The regression test asserts on the rendered body rather than the whole markup block, since the block's trailing blank line is the subject of a separate change. Signed-off-by: Douglas Strodtman --- sphinxcontrib/openapi/openapi30.py | 10 ++++++++-- tests/test_openapi.py | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/sphinxcontrib/openapi/openapi30.py b/sphinxcontrib/openapi/openapi30.py index 3b47277..6ef3ce9e 100644 --- a/sphinxcontrib/openapi/openapi30.py +++ b/sphinxcontrib/openapi/openapi30.py @@ -304,8 +304,14 @@ def _httpresource(endpoint, method, properties, convert, render_examples, request_content = properties.get('requestBody', {}).get('content', {}) if request_content and 'application/json' in request_content: schema = request_content['application/json']['schema'] - req_properties = json.dumps(schema['properties'], indent=2, - separators=(',', ':')) + if schema['type'] == 'object': + # if it's an object, focus on the properties of that object + req_properties = json.dumps(schema['properties'], indent=2, + separators=(',', ':')) + else: + # if it's another type, dump the whole thing + req_properties = json.dumps(schema, indent=2, + separators=(',', ':')) yield '{indent}**Request body:**'.format(**locals()) yield '' yield '{indent}.. sourcecode:: json'.format(**locals()) diff --git a/tests/test_openapi.py b/tests/test_openapi.py index d254572..d8fdfe4 100644 --- a/tests/test_openapi.py +++ b/tests/test_openapi.py @@ -1693,6 +1693,33 @@ def test_method_option(self): ok ''').lstrip() + def test_request_body_non_object(self): + renderer = renderers.HttpdomainOldRenderer(None, {'request': True}) + text = '\n'.join(renderer.render_restructuredtext_markup({ + 'openapi': '3.0.0', + 'paths': { + '/things': { + 'post': { + 'summary': 'Create Thing', + 'requestBody': { + 'content': { + 'application/json': { + 'schema': {'type': 'string'}, + }, + }, + }, + 'responses': { + '200': { + 'description': 'A thing created.', + }, + }, + }, + }, + }, + })) + assert ' **Request body:**' in text + assert '"type":"string"' in text + class TestResolveRefs(object):