Skip to content

feat(style/oas2/oas3): autoclose xml empty tags in examples (#11055) - #11061

Open
adrianodpdiaz wants to merge 3 commits into
swagger-api:mainfrom
adrianodpdiaz:feat/xml-examples-autoclose-empty-tags
Open

adrianodpdiaz wants to merge 3 commits into
swagger-api:mainfrom
adrianodpdiaz:feat/xml-examples-autoclose-empty-tags

Conversation

@adrianodpdiaz

@adrianodpdiaz adrianodpdiaz commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

Fixes #11055

XML examples generated for schemas whose objects have only attributes (no child elements) were rendered as an open/close tag pair with an empty body (<c>\n</c>, <User id="123" name="bob">\n</User>, etc.), which is misleading — it looks like the element has content when it doesn't — and is not idiomatic XML. Idiomatic tooling renders these as self-closing tags (<c/>, <User id="123" name="bob"/>).

Now generated XML examples for elements with no child content (only attributes, or an empty array of attribute-only items) render as a self-closing tag instead of an empty tag pair with a stray blank line.

This affects both json-schema-2020-12-samples (OAS 3.1) and json-schema-5-samples (OAS 2.0/3.0) sample generators, so both were fixed identically to keep behavior consistent across spec versions, for all supported OAS versions: 2.0, 3.0.x, 3.1.x, and 3.2.x.

Changes

  • Added a normalizeXMLContent() helper in both src/core/plugins/json-schema-2020-12-samples/fn/main.js and src/core/plugins/json-schema-5-samples/fn/index.js that recursively walks the sample object before it's handed to the xml package:
    • An empty array collapses to null.
    • An array whose items are all _attr-only objects (no real content) is merged into a single { _attr: {...} } object, so the xml package emits a self-closing tag with attributes instead of an empty tag with children.
    • All other arrays/objects are recursed into unchanged.
  • createXMLExample() in both plugins now runs the generated sample through normalizeXMLContent() before passing it to XML(...).
  • Updated existing Cypress and Jest tests/fixtures that asserted the old <tag>\n</tag> empty-tag-pair output to expect the new self-closing <tag/> output.

How Has This Been Tested?

  • Updated and ran the existing regression tests covering this exact behavior:
    • test/e2e-cypress/e2e/bugs/4943.cy.js (XML example not rendered correctly with oneOf)
    • test/e2e-cypress/e2e/bugs/6627.cy.js (XML example when defined as an array)
    • test/e2e-cypress/e2e/features/oas3-xml.cy.js
    • test/unit/core/plugins/json-schema-2020-12-samples/fn.js
    • test/unit/core/plugins/json-schema-5-samples/fn/index.js
  • npm run test:unit passes locally.
  • Manually verified via npm run dev that XML examples for attribute-only objects and arrays of attribute-only objects now render as self-closing tags, while objects/arrays with real child content are unaffected.

Screenshots:

Before:
image

After:
image

Checklist

My PR contains...

  • No code changes (src/ is unmodified: changes to documentation, CI, metadata, etc.)
  • Dependency changes (any modification to dependencies in package.json)
  • [] Bug fixes (non-breaking change which fixes an issue)
  • Improvements (misc. changes to existing features)
  • Features (non-breaking change which adds functionality)

My changes...

  • are breaking changes to a public API (config options, System API, major UI change, etc).
  • are breaking changes to a private API (Redux, component props, utility functions, etc.).
  • are breaking changes to a developer API (npm script behavior changes, new dev system dependencies, etc).
  • are not breaking changes.

Documentation

  • My changes do not require a change to the project documentation.
  • My changes require a change to the project documentation.
  • If yes to above: I have updated the documentation accordingly.

Automated tests

  • My changes can not or do not need to be tested.
  • My changes can and should be tested by unit and/or integration tests.
  • If yes to above: I have added tests to cover my changes.
  • If yes to above: I have taken care to cover edge cases in my tests.
  • All new and existing tests passed.

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.

Swagger UI XML example generator does not self-close empty tags

1 participant