Skip to content

Commit

Permalink
feat(cloning): add option to clone input schema, add notice to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca committed Mar 6, 2024
1 parent 48445fb commit 5929a46
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ Example
import $RefParser from "@apidevtools/json-schema-ref-parser";

try {
let schema = await $RefParser.dereference(mySchema);
console.log(schema.definitions.person.properties.firstName);
await $RefParser.dereference(mySchema);
// note - by default, mySchema is modified in place, and the returned value is a reference to the same object
console.log(mySchema.definitions.person.properties.firstName);

// if you want to avoid modifying the original schema, you can disable the `mutateInputSchema` option
let clonedSchema = await $RefParser.dereference(mySchema, { mutateInputSchema: false });
console.log(clonedSchema.definitions.person.properties.firstName);
} catch (err) {
console.error(err);
}
Expand Down
38 changes: 9 additions & 29 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,17 @@ import {
import { ono } from "@jsdevtools/ono";
import maybe from "./util/maybe.js";
import type { ParserOptions } from "./options.js";
import type {
Plugin,
$RefsCallback,
JSONSchema,
SchemaCallback,
HTTPResolverOptions,
FileInfo,
ResolverOptions,
JSONSchemaObject,
} from "./types/index.js";
import type { $RefsCallback, JSONSchema, SchemaCallback } from "./types/index.js";

export {
JSONSchemaObject,
ResolverOptions,
ParserError,
UnmatchedResolverError,
ResolverError,
HTTPResolverOptions,
FileInfo,
UnmatchedParserError,
ParserOptions,
MissingPointerError,
InvalidPointerError,
JSONParserError,
Plugin,
JSONSchema,
$RefsCallback,
SchemaCallback,
};
export { JSONParserError };
export { InvalidPointerError };
export { MissingPointerError };
export { ResolverError };
export { ParserError };
export { UnmatchedParserError };
export { UnmatchedResolverError };

export type RefParserSchema = string | JSONSchema;
type RefParserSchema = string | JSONSchema;

/**
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,
Expand Down
5 changes: 5 additions & 0 deletions lib/normalize-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ function normalizeArgs(_args: Partial<IArguments>) {
console.log(e);
}

if (!options.mutateInputSchema) {
// Make a deep clone of the schema, so that we don't alter the original object
schema = JSON.parse(JSON.stringify(schema));
}

return {
path,
schema,
Expand Down
9 changes: 9 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ interface $RefParserOptions {
* Default: `relative`
*/
externalReferenceResolution?: "relative" | "root";

/**
* Whether to clone the schema before dereferencing it.
* This is useful when you want to dereference the same schema multiple times, but you don't want to modify the original schema.
* Default: `true` due to mutating the input being the default behavior historically
*/
mutateInputSchema?: boolean;
};
}

Expand Down Expand Up @@ -159,6 +166,8 @@ const getDefaults = () => {
excludedPathMatcher: () => false,
referenceResolution: "relative",
},

mutateInputSchema: true,
} as $RefParserOptions;
return defaults;
};
Expand Down

0 comments on commit 5929a46

Please sign in to comment.