fix(json-schema-5-samples): unwrap schema example referencing components.examples#10884
Open
yogeshwaran-c wants to merge 1 commit into
Open
fix(json-schema-5-samples): unwrap schema example referencing components.examples#10884yogeshwaran-c wants to merge 1 commit into
yogeshwaran-c wants to merge 1 commit into
Conversation
…nts.examples
When a schema property's `example` keyword is `{$ref: '#/components/examples/X'}`,
swagger-client substitutes the resolved Example Object verbatim, leaving
`{value: <user data>, $$ref: '#/components/examples/X'}`. The sample generator
then rendered the literal `value:` wrapper instead of the actual example data.
Add `unwrapTopLevelExampleReference` which, when the top-level example object
has both a `$$ref` pointing into `#/components/examples/` and a `value` key,
returns the inner `value`. This is a no-op for any other shape (including
nested occurrences, user-authored objects that happen to have a `value` key,
or `$$ref`s pointing elsewhere), so it cannot affect normal sample generation.
Fixes swagger-api#5748
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.
Description
When a schema property's
examplekeyword is{$ref: '#/components/examples/X'}, the rendered example incorrectly includes avalue:wrapper key.Root cause
swagger-client's ref resolver (refs.js) substitutes the resolved Example Object verbatim — only response/requestBody/parameterexamples[*].valuepaths are on its skip list. So a schema-levelexample: { \$ref: '#/components/examples/X' }is replaced with{ value: <user data>, \$\$ref: '#/components/examples/X' }.sampleFromSchemaGenericinsrc/core/plugins/json-schema-5-samples/fn/index.jsthen reads that object as the literal sample, leaving thevalue:wrapper visible in the rendered output. The same example referenced from a response'sexamples:block does not hit this code path and renders correctly (the asymmetry described in the issue).Fix
A new helper
unwrapTopLevelExampleReferencechecks whether the top-level example object has both a\$\$refmatching#/components/examples/and an ownvaluekey. Only that exact shape (whichswagger-clientinjects deterministically for resolved Example Objects) is unwrapped; everything else passes through untouched.The fix is intentionally conservative:
\$\$refpointing into#/components/examples/) is present.sanitizeRefexactly as before, so existing tests attest/unit/core/plugins/json-schema-5-samples/fn/index.js:294and:324are unaffected.valuekey (no\$\$ref) or with a\$\$refpointing elsewhere are preserved verbatim.Motivation and Context
Fixes #5748.
How Has This Been Tested?
\$\$reftest intest/unit/core/plugins/json-schema-5-samples/fn/index.jsto expect the unwrapped shape, and added two new negative tests (no\$\$ref;\$\$refpointing outside#/components/examples/). Full file: 121 tests pass.test/e2e-cypress/static/documents/bugs/5748.yamlreproduces the issue's repro spec; new spectest/e2e-cypress/e2e/bugs/5748.cy.jsasserts the rendered example contains the inner keys (Individual,Variant) and does not contain the\"value\":wrapper.eslint --max-warnings 0clean on changed files.Checklist