Repository navigation
Description
With :examples:, a response whose schema is a bare type: string (for example an enum) produces an example body that isn't valid JSON, even though the block is emitted under Content-Type: application/json. Pygments then fails to lex it and Sphinx warns. Under sphinx-build -W that's fatal.
Minimal reproducer
spec.yml:
openapi: 3.0.3
info:
title: Minimal
version: 1.0.0
paths:
/thing:
get:
summary: Get thing
responses:
200:
description: A thing.
content:
application/json:
schema:
type: string
enum:
- PENDING
- DONEindex.rst:
Page
====
.. openapi:: spec.yml
:examples:Actual
$ sphinx-build -W -b html src out
<openapi>:1: WARNING: Lexing literal_block 'HTTP/1.1 200 OK\nContent-Type: application/json\n\nPENDING' as "http" resulted in an error at token: 'P'. Retrying in relaxed mode
The generated example body is the bare token PENDING:
HTTP/1.1 200 OK
Content-Type: application/json
PENDINGExpected
Either a JSON-encoded body, "PENDING", which is a valid JSON document and lexes cleanly, or no example emitted for a scalar schema.
Cause
Two pieces combine, both in sphinxcontrib/openapi/openapi30.py.
_parse_schema returns the raw first enum value, a Python str:
108: if 'enum' in schema:
109: # we only show the first one since we can't show everything
110: return schema['enum'][0]_example then skips JSON encoding for anything that's already a str:
208: for example in examples.values():
209: # According to OpenAPI v3 specs, string examples should be left unchanged
210: if not isinstance(example['value'], str):
211: example['value'] = json.dumps(
212: example['value'], indent=4, separators=(',', ': '))The "leave strings unchanged" rule is right for an author-supplied example: value, where the string is meant literally. But a schema-derived example for a type: string schema still needs JSON encoding to be a valid application/json document. The two paths converge on the same isinstance check, so the derived case inherits behavior intended for the literal case.
A fix would need to distinguish schema-derived examples from author-supplied ones, rather than switching on the value's Python type.
Possibly related
- Datetime example field result in build fail for Sphinx #168 is the inverse failure at the same line 211: a value that isn't a
str(adatetimefrom YAML) reachesjson.dumpsand raises. Same function, adjacent concern, different fix. - Unable to render examples with only readonly properties #160 reports an unserializable sentinel escaping
_parse_schemain the 3.1 path.
All three suggest the example-generation path would benefit from a single place that decides how a derived value becomes a rendered body.
Environment
- sphinxcontrib-openapi 0.9.0
- sphinxcontrib-httpdomain 2.0.0
- Sphinx 8.2.3
- Pygments 2.20.0
- docutils 0.21.2
- Python 3.12.13
Activity
dstrodtman commentedon Aug 13, 2026
I had a go at the "single place that decides how a derived value becomes a rendered body" idea from the issue, and it does close #168 and #160 as well. Sketching it here before opening a PR, since it touches shared behavior and there are a couple of judgment calls I'd rather you made.
The shape
Two small helpers, added to both
openapi30.pyandopenapi31.py:Then in
_example, a schema-derived example gets encoded at the point of derivation rather than falling through to the shared type check:and the existing loop becomes
example['value'] = _dumps(example['value']).The provenance distinction the issue asks for ends up being expressed by when encoding happens rather than by a flag. A derived value is encoded immediately, so by the time it reaches the
isinstance(..., str)check it's already a JSON document and is correctly left alone. An author-supplied value still hits that check, so "string examples are left unchanged" keeps applying to exactly the case it was written for.What each issue gets
type: stringschema now renders"PENDING"instead of a barePENDING. Same for a plaintype: stringwith no enum, which renders"string"and has the same lexer problem today.datetimeYAML hands back is encoded viaisoformat()instead of raising, so the body renders as{"TestDate": "2000-01-23T04:56:07+00:00"}._READONLY_PROPERTYsentinel is caught beforejson.dumpssees it, and the example is omitted rather than crashing the build.Verified with the reproducers from each issue: all three fail on
masterand build clean under-Wwith this applied. The existing suite passes unchanged — no committed expectation asserts the old unencoded output, so this doesn't silently rewrite anyone's fixtures.The judgment calls
One PR or three? This is one behavioral change in one function, so a single PR with three regression tests seems more reviewable than three PRs touching the same lines in sequence. Happy to split if you'd rather keep the issues independent.
str()as the catch-all. The last line of_json_defaultmeans an unexpected type renders as itsrepr-ish string instead of failing the build. For a docs extension I'd rather render something odd than crash, but it does mean a future unserializable value gets papered over instead of reported. ALOG.warningalongside it would be easy to add if you'd prefer that.Arrays of read-only items. A request body of
type: arraywhoseitemsarereadOnlycurrently produces[_READONLY_PROPERTY], which crashes the same way Unable to render examples with only readonly properties #160 does. With the sentinel mapped toNoneit renders[null]. That's valid JSON and no longer a crash, but[]or omitting the example entirely might read better._parse_schemaalready filters the sentinel out of object properties but not out of array items, so this is the one place the two disagree.Sub-second precision.
isoformat()on the value YAML parsed from2000-01-23T04:56:07.000+00:00gives2000-01-23T04:56:07+00:00— the trailing zeros are gone, because they were already gone once YAML parsed it. Faithful to the parsed value, not to the source text. I don't think it's worth chasing, but flagging it.The duplication.
openapi30.pyandopenapi31.pyare near-identical here, so the fix has to land twice. I've mirrored it rather than factoring out a shared module, on the assumption you'd want that as its own change if at all.If the shape looks right I'll open the PR. For context on where this came from: the Ray docs are moving their API reference onto this extension, and this is the one of the three issues I filed that blocks using
:examples:with an enum-typed response at all.[doc] Render the Jobs API spec with sphinxcontrib-openapi instead of …
[doc] Render the Jobs API spec with sphinxcontrib-openapi instead of …
dstrodtman commentedon Oct 13, 2026
A correction to my sketch above. It patches
openapi30.pyandopenapi31.py, which only the old renderer uses. The reproducer in this issue uses:examples:, so it goes through that path too, and the sketch is still the right fix for it.The new renderer has the same bug in a different place.
renderers/_httpdomain.pyskips JSON encoding for anystrexample, at the twoif not isinstance(example, str)checks. That's the path Ray actually uses, and #185 already fixes it there.So what's left here is the old-renderer fix, along with the old-renderer instances of #168 and #160, which come from the same lines. I haven't checked whether those two also reproduce on the new renderer. Is that fix still worth landing, given the old renderer is the legacy path? If yes, I'll open one PR with a commit per issue, each with its own regression test. If you'd rather leave the old renderer alone, I'll note that here and leave it.
[doc] Bump sphinxcontrib-openapi to 0.10.0 (#66957)