feat(style/oas2/oas3): autoclose xml empty tags in examples (#11055) - #11061
Open
adrianodpdiaz wants to merge 3 commits into
Open
adrianodpdiaz wants to merge 3 commits into
adrianodpdiaz wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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) andjson-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
normalizeXMLContent()helper in bothsrc/core/plugins/json-schema-2020-12-samples/fn/main.jsandsrc/core/plugins/json-schema-5-samples/fn/index.jsthat recursively walks the sample object before it's handed to thexmlpackage:null._attr-only objects (no real content) is merged into a single{ _attr: {...} }object, so thexmlpackage emits a self-closing tag with attributes instead of an empty tag with children.createXMLExample()in both plugins now runs the generated sample throughnormalizeXMLContent()before passing it toXML(...).<tag>\n</tag>empty-tag-pair output to expect the new self-closing<tag/>output.How Has This Been Tested?
test/e2e-cypress/e2e/bugs/4943.cy.js(XML example not rendered correctly withoneOf)test/e2e-cypress/e2e/bugs/6627.cy.js(XML example when defined as an array)test/e2e-cypress/e2e/features/oas3-xml.cy.jstest/unit/core/plugins/json-schema-2020-12-samples/fn.jstest/unit/core/plugins/json-schema-5-samples/fn/index.jsnpm run test:unitpasses locally.npm run devthat 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:

After:

Checklist
My PR contains...
src/is unmodified: changes to documentation, CI, metadata, etc.)package.json)My changes...
Documentation
Automated tests