fix(mimetype-content-wrapper): clone object per mimetype to avoid shared references#3945
Open
yogeshwaran-c wants to merge 1 commit into
Open
Conversation
…red references MimetypeContentWrapper#wrap reused the same object reference for every media type and passed the caller's object directly to removeUndefinedKeys. As a result, all media-type entries in the generated content object aliased a single instance, and the caller's source object was mutated. Mutating one media type's schema (or any later transform) silently affected the others. Clone the object per mimetype with lodash cloneDeep so each media-type entry is independent and the caller's source object is left untouched.
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.
Summary
MimetypeContentWrapper#wrapbuilds the OpenAPIcontentobject by mapping each media type toremoveUndefinedKeys(obj)— but it reuses the same object reference for every media type and passes the caller's object directly intoremoveUndefinedKeys(which mutates in place).Two consequences for responses/request bodies declared with multiple media types (e.g.
@ApiProduces('application/json', 'application/xml')or multiple@ApiConsumes):contentpoints to the same object instance, so mutating one media type's schema later silently mutates all of them.removeUndefinedKeysstrips keys from the caller's original object rather than a copy.Reproduction (current behavior):
The fix clones the object per media type with lodash
cloneDeep(already used elsewhere in the codebase) so each entry is independent and the caller's source object is left untouched.Test plan
Added
test/services/mimetype-content-wrapper.spec.tscovering:undefinedkeys