-
- Notifications
You must be signed in to change notification settings - Fork 239
feat: dereference.preservedProperties for preserving data during dereferencing #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jonluca merged 3 commits into APIDevTools:main from erunion:feat/preserving-dereferenced-properties Jan 28, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -123,7 +123,27 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse | |
| circular = dereferenced.circular; | ||
| // Avoid pointless mutations; breaks frozen objects to no profit | ||
| if (obj[key] !== dereferenced.value) { | ||
| // If we have properties we want to preserve from our derefernced schema then we need | ||
| // to copy them over to our new object. | ||
| const preserved: Map<string, unknown> = new Map(); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can we wrap this logic and this object allocation inside a check for if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely! | ||
| if (typeof obj[key] === "object" && !Array.isArray(obj[key])) { | ||
| (derefOptions?.preservedProperties || []).forEach((prop) => { | ||
| if (prop in obj[key]) { | ||
| preserved.set(prop, obj[key][prop]); | ||
| } | ||
| }); | ||
| } | ||
| | ||
| obj[key] = dereferenced.value; | ||
| | ||
| // If we have data to preserve and our dereferenced object is still an object then | ||
| // we need copy back our preserved data into our dereferenced schema. | ||
| if (preserved.size && typeof obj[key] === "object" && !Array.isArray(obj[key])) { | ||
| preserved.forEach((value, prop) => { | ||
| obj[key][prop] = value; | ||
| }); | ||
| } | ||
| | ||
| derefOptions?.onDereference?.(value.$ref, obj[key], obj, key); | ||
| } | ||
| } else { | ||
| | ||
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
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
41 changes: 41 additions & 0 deletions 41 test/specs/dereference-preservedProperties/dereference-preservedProperties.spec.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { describe, it } from "vitest"; | ||
| import $RefParser from "../../../lib/index.js"; | ||
| import pathUtils from "../../utils/path.js"; | ||
| | ||
| import { expect } from "vitest"; | ||
| import type { Options } from "../../../lib/options"; | ||
| | ||
| describe("dereference.preservedProperties", () => { | ||
| it("should preserve properties", async () => { | ||
| const parser = new $RefParser(); | ||
| const schema = pathUtils.rel("test/specs/dereference-preservedProperties/dereference-preservedProperties.yaml"); | ||
| const options = { | ||
| dereference: { | ||
| preservedProperties: ["description"], | ||
| }, | ||
| } as Options; | ||
| const res = await parser.dereference(schema, options); | ||
| | ||
| expect(res).to.deep.equal({ | ||
| title: "Person", | ||
| required: ["name"], | ||
| type: "object", | ||
| definitions: { | ||
| name: { | ||
| type: "string", | ||
| description: "Someone's name", | ||
| }, | ||
| }, | ||
| properties: { | ||
| name: { | ||
| type: "string", | ||
| description: "Someone's name", | ||
| }, | ||
| secretName: { | ||
| type: "string", | ||
| description: "Someone's secret name", | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
16 changes: 16 additions & 0 deletions 16 test/specs/dereference-preservedProperties/dereference-preservedProperties.yaml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| title: Person | ||
| required: | ||
| - name | ||
| type: object | ||
| definitions: | ||
| name: | ||
| type: string | ||
| description: Someone's name | ||
| properties: | ||
| name: | ||
| $ref: "#/definitions/name" | ||
| secretName: | ||
| $ref: "#/definitions/name" | ||
| # Despite "Someone's name" being the description of the referenced `name` schema our overwritten | ||
| # description should be preserved instead. | ||
| description: Someone's secret name |
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.
Uh oh!
There was an error while loading. Please reload this page.