From ef038ee21e099f58e44e4b3fff389166f9f116b4 Mon Sep 17 00:00:00 2001 From: Jason Walton Date: Tue, 14 Jul 2020 14:11:06 -0400 Subject: [PATCH] fix: Upgrade json-ptr to 1.3.1. json-ptr has breaking changes between 1.2.0 and 1.3.x. :( fixes #146 --- @types/json-ptr/index.d.ts | 5 ----- package-lock.json | 26 +++++++++++++------------- package.json | 4 ++-- src/oas3/Oas3CompileContext.ts | 13 +++++++++---- src/utils/json-schema-resolve-ref.ts | 2 +- src/utils/jsonPaths.ts | 4 ++-- src/utils/jsonSchema.ts | 4 ++-- test/fixtures/index.ts | 2 +- test/oas3/oas3ControllersTest.ts | 4 ++-- 9 files changed, 32 insertions(+), 32 deletions(-) delete mode 100644 @types/json-ptr/index.d.ts diff --git a/@types/json-ptr/index.d.ts b/@types/json-ptr/index.d.ts deleted file mode 100644 index 0a8ea1b4..00000000 --- a/@types/json-ptr/index.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -declare module 'json-ptr' { - export function encodePointer(path: string[]): string; - export function decode(pointer: string): string[]; - export function encodeUriFragmentIdentifier(path: string[]): string; -} diff --git a/package-lock.json b/package-lock.json index f9ebbd34..0b1cf0ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -783,11 +783,11 @@ } }, "ajv": { - "version": "6.10.2", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.2.tgz", - "integrity": "sha512-TXtUUEYHuaTEbLZWIKUr5pmBuhDLy+8KYtPYdcV8qC+pOZL+NKqYwvWSRrVXHn+ZmRRAu8vJTAznH7Oag6RVRw==", + "version": "6.12.2", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.2.tgz", + "integrity": "sha512-k+V+hzjm5q/Mr8ef/1Y9goCmlsK4I6Sm74teeyGvFk1XrOsbsKLjEdrvny42CZ+a8sXbk8KWpY/bDwS+FLL2UQ==", "requires": { - "fast-deep-equal": "^2.0.1", + "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", "json-schema-traverse": "^0.4.1", "uri-js": "^4.2.2" @@ -1866,9 +1866,9 @@ "dev": true }, "fast-deep-equal": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", - "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=" + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.1.tgz", + "integrity": "sha512-8UEa58QDLauDNfpbrX55Q9jrGHThw2ZMdOky5Gl1CDtVeJDPVrG4Jxx1N8jw2gkWaff5UUuX1KJd+9zGe2B+ZA==" }, "fast-glob": { "version": "3.1.1", @@ -1884,9 +1884,9 @@ } }, "fast-json-stable-stringify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", - "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=" + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", + "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==" }, "fastq": { "version": "1.6.0", @@ -2996,9 +2996,9 @@ "dev": true }, "json-ptr": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/json-ptr/-/json-ptr-1.2.0.tgz", - "integrity": "sha512-wkqozzmmyu+vtazHF11uvIDbLsgkE4juY41QoXgKYSy166KQalyc72PqveII21f8UWKtYN7btUXg16vya+9xnw==" + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/json-ptr/-/json-ptr-1.3.1.tgz", + "integrity": "sha512-RoTHczqQoT2UsJINUvyMEHAHxGDq6c4/xXHwA29iArQMGIv6pFqLsOJMeagtnY78vsp5k/+9JcvmKKLsbuDRBQ==" }, "json-schema": { "version": "0.2.3", diff --git a/package.json b/package.json index 3f801798..64f3da13 100644 --- a/package.json +++ b/package.json @@ -78,13 +78,13 @@ "typescript": "^3.3.3" }, "dependencies": { - "ajv": "^6.9.1", + "ajv": "^6.12.2", "body-parser": "^1.18.3", "content-type": "^1.0.4", "deep-freeze": "0.0.1", "events-listener": "^1.1.0", "glob": "^7.1.3", - "json-ptr": "^1.1.1", + "json-ptr": "^1.3.1", "json-schema-ref-parser": "^7.0.0", "json-schema-traverse": "^0.4.1", "lodash": "^4.17.11", diff --git a/src/oas3/Oas3CompileContext.ts b/src/oas3/Oas3CompileContext.ts index 0ba5919e..2b771931 100644 --- a/src/oas3/Oas3CompileContext.ts +++ b/src/oas3/Oas3CompileContext.ts @@ -10,6 +10,7 @@ import { ExegesisCompiledOptions } from '../options'; * A path to an object within a JSON document. */ export type JsonPath = string[]; +export type ReadOnlyJsonPath = readonly string[]; /** * This has common stuff that we want to pass all the way down through the OAS @@ -29,9 +30,13 @@ export default class Oas3CompileContext { * @param path - The path to the object represented by this context. * @param options - Options. */ - constructor(openApiDoc: oas3.OpenAPIObject, path: JsonPath, options: ExegesisCompiledOptions); - constructor(parent: Oas3CompileContext, relativePath: JsonPath); - constructor(a: any, path: JsonPath, options?: ExegesisCompiledOptions) { + constructor( + openApiDoc: oas3.OpenAPIObject, + path: ReadOnlyJsonPath, + options: ExegesisCompiledOptions + ); + constructor(parent: Oas3CompileContext, relativePath: ReadOnlyJsonPath); + constructor(a: any, path: ReadOnlyJsonPath, options?: ExegesisCompiledOptions) { if (a instanceof Oas3CompileContext) { // TODO: Could make this WAY more efficient with Object.create(). const parent = a; @@ -39,7 +44,7 @@ export default class Oas3CompileContext { this.openApiDoc = parent.openApiDoc; this.options = parent.options; } else if (options) { - this.path = path; + this.path = path.slice(); this.openApiDoc = a; this.options = options; } else { diff --git a/src/utils/json-schema-resolve-ref.ts b/src/utils/json-schema-resolve-ref.ts index 3470575e..a91b67c5 100644 --- a/src/utils/json-schema-resolve-ref.ts +++ b/src/utils/json-schema-resolve-ref.ts @@ -5,7 +5,7 @@ function resolveRefPriv(document: any, ref: string): any { throw new Error(`Cannot resolve non-local ref ${ref}`); } - const path = jsonPtr.decode(ref); + const path = jsonPtr.JsonPointer.decode(ref).slice(); let currentDoc = document; while (path.length > 0) { const pathname = path.shift() as string; diff --git a/src/utils/jsonPaths.ts b/src/utils/jsonPaths.ts index 75e2fd2b..acc68f33 100644 --- a/src/utils/jsonPaths.ts +++ b/src/utils/jsonPaths.ts @@ -1,11 +1,11 @@ import * as jsonPtr from 'json-ptr'; function normalize(path: string): string { - return jsonPtr.encodePointer(jsonPtr.decode(path)); + return jsonPtr.encodePointer(jsonPtr.JsonPointer.decode(path)); } export function toUriFragment(path: string) { - return jsonPtr.encodeUriFragmentIdentifier(jsonPtr.decode(path)); + return jsonPtr.encodeUriFragmentIdentifier(jsonPtr.JsonPointer.decode(path)); } export function jsonPointerStartsWith(path: string, prefix: string): boolean { diff --git a/src/utils/jsonSchema.ts b/src/utils/jsonSchema.ts index 79c4fc47..a3ecf7db 100644 --- a/src/utils/jsonSchema.ts +++ b/src/utils/jsonSchema.ts @@ -62,9 +62,9 @@ function extractSchemaPriv( // all the `newRefSuffix`es we pick in `ctx.replacements`, and // then we can make sure this doesn't happen. const origRef = schema.$ref; - const jsonPath = jsonPtr.decode(schema.$ref); + const jsonPath = jsonPtr.JsonPointer.decode(schema.$ref); let newRefSuffix: string | undefined = - jsonPath.length > 0 ? jsonPath[jsonPath.length - 1] : undefined; + jsonPath.length > 0 ? `${jsonPath[jsonPath.length - 1]}` : undefined; while ( !newRefSuffix || ctx.result.definitions[newRefSuffix] || diff --git a/test/fixtures/index.ts b/test/fixtures/index.ts index b123cd80..47fb0304 100644 --- a/test/fixtures/index.ts +++ b/test/fixtures/index.ts @@ -23,7 +23,7 @@ export function makeContext( ) { return new Oas3CompileContext( openApiDoc, - jsonPtr.decode(jsonPointer), + jsonPtr.JsonPointer.decode(jsonPointer).map(x => `${x}`), Object.assign({}, defaultCompiledOptions, options || {}) ); } diff --git a/test/oas3/oas3ControllersTest.ts b/test/oas3/oas3ControllersTest.ts index b0a767e6..b19a24ed 100644 --- a/test/oas3/oas3ControllersTest.ts +++ b/test/oas3/oas3ControllersTest.ts @@ -68,8 +68,8 @@ async function findControllerTest( ) { const context = new FakeExegesisContext(); const openApiDoc = generateOpenApi(); - ld.set(openApiDoc, jsonPtr.decode(controllerLocation), 'myController'); - ld.set(openApiDoc, jsonPtr.decode(operationLocation), 'op'); + ld.set(openApiDoc, jsonPtr.JsonPointer.decode(controllerLocation), 'myController'); + ld.set(openApiDoc, jsonPtr.JsonPointer.decode(operationLocation), 'op'); const openApi = new OpenApi(openApiDoc, options);