-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add reference resolution option to allow root level dereferenci…
…ng (#305) * feat: add reference resolution option to allow root level dereferencing * feat: add reference resolution option to allow root level dereferencing * skip if browser * convert path to posix for url first
- Loading branch information
Showing
15 changed files
with
274 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,66 @@ | ||
import { afterAll, beforeAll, describe, it } from "vitest"; | ||
import $RefParser, { JSONParserError } from "../../../lib/index.js"; | ||
import path from "../../utils/path.js"; | ||
|
||
import { expect, vi } from "vitest"; | ||
import helper from "../../utils/helper"; | ||
|
||
describe.skipIf(process.env.BROWSER)("Schemas with imports in relative and absolute locations work", () => { | ||
describe("Schemas with relative imports that should be resolved from the root", () => { | ||
beforeAll(() => { | ||
vi.spyOn(process, "cwd").mockImplementation(() => { | ||
return __dirname; | ||
}); | ||
}); | ||
afterAll(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
it("should not parse successfully when set to resolve relative (default)", async () => { | ||
const parser = new $RefParser(); | ||
try { | ||
await parser.dereference(path.rel("schemas/accountList.json")); | ||
helper.shouldNotGetCalled(); | ||
} catch (err) { | ||
expect(err).to.be.an.instanceOf(JSONParserError); | ||
} | ||
}); | ||
|
||
it("should parse successfully when set to resolve relative (default)", async () => { | ||
const parser = new $RefParser(); | ||
const schema = await parser.dereference(path.rel("schemas/accountList.json"), { | ||
dereference: { externalReferenceResolution: "root" }, | ||
}); | ||
expect(schema).to.eql(parser.schema); | ||
}); | ||
}); | ||
|
||
describe("Schemas with relative imports that should be resolved relatively", () => { | ||
beforeAll(() => { | ||
vi.spyOn(process, "cwd").mockImplementation(() => { | ||
return __dirname; | ||
}); | ||
}); | ||
afterAll(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
it("should parse successfully when set to resolve relative (default)", async () => { | ||
const parser = new $RefParser(); | ||
const schema = await parser.dereference(path.rel("schemas-relative/accountList.json"), { | ||
dereference: { externalReferenceResolution: "relative" }, | ||
}); | ||
expect(schema).to.eql(parser.schema); | ||
}); | ||
|
||
it("should not parse successfully when set to resolve relative (default)", async () => { | ||
const parser = new $RefParser(); | ||
try { | ||
await parser.dereference(path.rel("schemas-relative/accountList.json"), { | ||
dereference: { externalReferenceResolution: "root" }, | ||
}); | ||
helper.shouldNotGetCalled(); | ||
} catch (err) { | ||
expect(err).to.be.an.instanceOf(JSONParserError); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains 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,26 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"title": "Account", | ||
"$id": "account.json", | ||
"type": "object", | ||
"description": "An account.", | ||
"additionalProperties": false, | ||
"required": [ | ||
"accountOwner", | ||
"accountId" | ||
], | ||
"properties": { | ||
"accountOwner": { | ||
"$ref": "user.json" | ||
}, | ||
"accountId": { | ||
"$id": "#/properties/accountId", | ||
"type": "string", | ||
"description": "An explanation about the purpose of this instance.", | ||
"default": "", | ||
"examples": [ | ||
"186383568343" | ||
] | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
test/specs/relative-path/schemas-relative/accountList.json
This file contains 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,30 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"title": "AccountList", | ||
"$id": "accountList.json", | ||
"type": "object", | ||
"description": "An account list result.", | ||
"additionalProperties": false, | ||
"required": [ | ||
"data", | ||
"total", | ||
"pages" | ||
], | ||
"properties": { | ||
"data": { | ||
"type": "array", | ||
"default": [], | ||
"items": { | ||
"$ref": "account.json" | ||
} | ||
}, | ||
"total": { | ||
"type": "integer", | ||
"description": "The number of total items found." | ||
}, | ||
"pages": { | ||
"type": "integer", | ||
"description": "The number of pages found" | ||
} | ||
} | ||
} |
This file contains 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,34 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"$id": "user.json", | ||
"type": "object", | ||
"title": "User", | ||
"description": "A User", | ||
"default": {}, | ||
"additionalProperties": false, | ||
"required": [ | ||
"id", | ||
"name", | ||
"email" | ||
], | ||
"properties": { | ||
"id": { | ||
"$id": "#/user/properties/id", | ||
"type": "string", | ||
"description": "The users id.", | ||
"default": "" | ||
}, | ||
"name": { | ||
"$id": "#/user/properties/name", | ||
"type": "string", | ||
"description": "The users full name with id.", | ||
"default": "" | ||
}, | ||
"email": { | ||
"$id": "#/user/properties/email", | ||
"type": "string", | ||
"description": "The users email address.", | ||
"default": "" | ||
} | ||
} | ||
} |
This file contains 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,25 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"title": "Account", | ||
"type": "object", | ||
"description": "An account.", | ||
"additionalProperties": false, | ||
"required": [ | ||
"accountOwner", | ||
"accountId" | ||
], | ||
"properties": { | ||
"accountOwner": { | ||
"$ref": "schemas/user.json" | ||
}, | ||
"accountId": { | ||
"$id": "#/properties/accountId", | ||
"type": "string", | ||
"description": "An explanation about the purpose of this instance.", | ||
"default": "", | ||
"examples": [ | ||
"186383568343" | ||
] | ||
} | ||
} | ||
} |
This file contains 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,29 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"title": "AccountList", | ||
"type": "object", | ||
"description": "An account list result.", | ||
"additionalProperties": false, | ||
"required": [ | ||
"data", | ||
"total", | ||
"pages" | ||
], | ||
"properties": { | ||
"data": { | ||
"type": "array", | ||
"default": [], | ||
"items": { | ||
"$ref": "schemas/account.json" | ||
} | ||
}, | ||
"total": { | ||
"type": "integer", | ||
"description": "The number of total items found." | ||
}, | ||
"pages": { | ||
"type": "integer", | ||
"description": "The number of pages found" | ||
} | ||
} | ||
} |
This file contains 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,33 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"type": "object", | ||
"title": "User", | ||
"description": "A User", | ||
"default": {}, | ||
"additionalProperties": false, | ||
"required": [ | ||
"id", | ||
"name", | ||
"email" | ||
], | ||
"properties": { | ||
"id": { | ||
"$id": "#/user/properties/id", | ||
"type": "string", | ||
"description": "The users id.", | ||
"default": "" | ||
}, | ||
"name": { | ||
"$id": "#/user/properties/name", | ||
"type": "string", | ||
"description": "The users full name with id.", | ||
"default": "" | ||
}, | ||
"email": { | ||
"$id": "#/user/properties/email", | ||
"type": "string", | ||
"description": "The users email address.", | ||
"default": "" | ||
} | ||
} | ||
} |