Skip to content

Disable Gson HTML escaping when prettifying generated API docs - #192

Open
mattiagualtieri wants to merge 1 commit into
springdoc:masterfrom
mattiagualtieri:fix/disable-html-escaping-in-generated-json
Open

mattiagualtieri wants to merge 1 commit into
springdoc:masterfrom
mattiagualtieri:fix/disable-html-escaping-in-generated-json

Conversation

@mattiagualtieri

Copy link
Copy Markdown

generateOpenApiDocs's prettifyJson() re-serializes the fetched API docs response through GsonBuilder().setPrettyPrinting().serializeNulls().create(). Gson's default HTML-safe escaping means characters like =, <, > get written as \u003d, \u003c, \u003e in the output file.

This plugin only ever writes the result to a file on disk — it's never embedded into an HTML page or <script> tag, which is the only reason Gson enables this escaping by default. It just makes the generated file harder to read and noisier to diff (e.g. application/vnd.oai.openapi;version=3.1.0 becomes application/vnd.oai.openapi;version\u003d3.1.0).

Change: adds .disableHtmlEscaping() to the same GsonBuilder chain.

Testing: added HtmlCharactersController (mirroring the existing NullValuesController fixture from #151) and a test not html-escaping special characters in the api docs, following the same pattern as keeping null values in the api docs. Confirmed it fails without the fix and passes with it. Full existing suite still passes.

Open question: I went with an unconditional change since there's no real scenario where HTML-escaped JSON is wanted in a file-output tool, and it matches how #151's serializeNulls() fix was also shipped unconditionally. But if you'd rather not change the default output for existing users, I'm happy to make it opt-in instead (e.g. a disableHtmlEscaping property on the openApi { } extension, defaulting to false) — let me know which you'd prefer and I'll update the PR.

generateOpenApiDocs always writes its output to a file, never embeds it
in HTML/JS, so Gson's default HTML-safe escaping only hurts readability
and diffability here: '=' becomes '\u003d', '<' becomes '\u003c', etc.

Adds disableHtmlEscaping() to the same GsonBuilder used in
prettifyJson(), plus a regression test mirroring the one added for the
sibling serializeNulls() fix (springdoc#151).

Signed-off-by: mattia.gualtieri <mattia.gualtieri@mollie.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant