diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index 48ee847..e5f62c1 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -18,7 +18,7 @@ jobs: - name: node uses: actions/setup-node@v4 with: - node-version: 20.9.0 + node-version: 22 - name: Deps run: | diff --git a/__tests__/patch.remove-defns-adv2.test.ts b/__tests__/patch.remove-defns-adv2.test.ts index c247485..391c120 100644 --- a/__tests__/patch.remove-defns-adv2.test.ts +++ b/__tests__/patch.remove-defns-adv2.test.ts @@ -39,3 +39,41 @@ describe('JSONSchemaPatch - Remove Complex allOf References', () => { }); }); }); + +describe('JSONSchemaPatch - Remove definitions from compositions with other rules', () => { + it('keeps a property that still has other rules and its required entry', () => { + const schema = { + $defs: { asset: { type: "object" } }, + properties: { + item: { description: "an item", allOf: [{ $ref: "#/$defs/asset" }] } + }, + required: ["item"] + }; + new JSONSchemaPatch(schema).removeDefinition('asset'); + expect(schema).toEqual({ + $defs: {}, + properties: { item: { description: "an item" } }, + required: ["item"] + }); + }); + + it('handles several references to the same definition on one property', () => { + const schema = { + $defs: { asset: { type: "object" } }, + properties: { + item: { + allOf: [{ $ref: "#/$defs/asset" }], + anyOf: [{ $ref: "#/$defs/asset" }, { $ref: "#/$defs/asset" }] + }, + name: { type: "string" } + }, + required: ["item", "name"] + }; + new JSONSchemaPatch(schema).removeDefinition('asset'); + expect(schema).toEqual({ + $defs: {}, + properties: { name: { type: "string" } }, + required: ["name"] + }); + }); +}); diff --git a/src/index.ts b/src/index.ts index 55f1f6c..841835e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -135,7 +135,11 @@ export class JSONSchemaPatch { // Helper method to remove properties that reference a specific definition private removePropertiesUsingDefinition(defName: string): void { const findPaths = (obj: any, currentPath: string) => { - Object.entries(obj).forEach(([key, value]) => { + const entries = Object.entries(obj); + // Walk arrays from the end so removing an item doesn't shift the ones still to visit + if (Array.isArray(obj)) entries.reverse(); + entries.forEach(([key, value]) => { + if (getValueAtPath(this.schema, currentPath) !== obj) return; if (typeof value === 'object' && value !== null) { // Check if the object has a $ref that matches the definition //@ts-ignore @@ -151,6 +155,7 @@ export class JSONSchemaPatch { const fullPath = currentPath + '/' + key; const op: Operation = { op: 'remove', path: fullPath }; applyPatch(this.schema, [op]); + this.removePropertyWithEmptyComposition(currentPath); } } // Recurse further into the object @@ -163,6 +168,21 @@ export class JSONSchemaPatch { findPaths(this.schema, ''); } + // Drop an allOf/anyOf/oneOf list emptied by a definition removal; a property left with no rules is removed + private removePropertyWithEmptyComposition(compositionPath: string): void { + const match = compositionPath.match(/^(.*)\/(allOf|anyOf|oneOf)$/); + if (!match) return; + const composition = getValueAtPath(this.schema, compositionPath); + if (!Array.isArray(composition) || composition.length > 0) return; + const owner = getValueAtPath(this.schema, match[1]); + const property = match[1].match(/^(.*)\/properties\/([^/]+)$/); + if (property && Object.keys(owner).length === 1) { + this.removeProperty(property[1], property[2]); + } else { + applyPatch(this.schema, [{ op: 'remove', path: compositionPath }]); + } + } + // Add a property at a specified path addProperty(path: string, propertyName: string, property: any): void { path = path.trim() === '/' ? '' : path; diff --git a/tsconfig.json b/tsconfig.json index 1f63d59..1bb8ddc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "baseUrl": ".", "rootDir": "src", "skipLibCheck": true, "declaration": true, diff --git a/types/index.d.ts b/types/index.d.ts index 5423f06..9c8f62d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -15,6 +15,7 @@ export declare class JSONSchemaPatch { addDefinition(name: string, definition: any): void; removeDefinition(name: string): void; private removePropertiesUsingDefinition; + private removePropertyWithEmptyComposition; addProperty(path: string, propertyName: string, property: any): void; removeProperty(path: string, propertyName: string): void; private removeRequiredFromProperty;