Skip to content
This repository was archived by the owner on Jul 13, 2023. It is now read-only.

Commit 3f96868

Browse files
authored
fix: support for windows file paths (#57)
* fix: support for windows file paths BREAKING CHANGE: authority is now baseUri, and is expected to be passed into resolve as a string. * test: add one to test relative path read count * feat: update terminology to align to spec
1 parent 26c8a23 commit 3f96868

File tree

11 files changed

+1421
-812
lines changed

11 files changed

+1421
-812
lines changed

README.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
[![Maintainability](https://api.codeclimate.com/v1/badges/0b1d841cc2445e29ef50/maintainability)](https://codeclimate.com/github/stoplightio/json-ref-resolver/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/0b1d841cc2445e29ef50/test_coverage)](https://codeclimate.com/github/stoplightio/json-ref-resolver/test_coverage)
44

5-
Recursively resolves JSON pointers and remote authorities.
5+
Dereference $ref values in JSON Schema, OpenAPI (Swagger), and any other objects with $ref values inside of them.
66

77
- View the changelog: [Releases](https://github.com/stoplightio/json-ref-resolver/releases)
88

99
### Features
1010

11-
- **Performant**: Hot paths are memoized, remote authorities are resolved concurrently, and the minimum surface area is crawled and resolved.
12-
- **Caching**: Results from remote authorities are cached.
13-
- **Immutable**: The original object is not changed, and structural sharing is used to only change relevant bits. [example test](src/__tests__/resolver.spec.ts#L139-L143)
14-
- **Reference equality:** Pointers to the same location will resolve to the same object in memory. [example test](src/__tests__/resolver.spec.ts#L145)
15-
- **Flexible:** Bring your own readers for `http://`, `file://`, `mongo://`, `custom://`... etc.
11+
- **Performant**: Hot paths are memoized, remote URIs are resolved concurrently, and the minimum surface area is crawled and resolved.
12+
- **Caching**: Results from remote URIs are cached.
13+
- **Immutable**: The original object is not changed, and structural sharing is used to only change relevant bits. [example test](src/__tests__/resolver.spec.ts#L182)
14+
- **Reference equality:** $refs to the same location will resolve to the same object in memory. [example test](src/__tests__/resolver.spec.ts#L329)
15+
- **Flexible:** Bring your own resolvers for `http://`, `file://`, `mongo://`, `custom://`... etc.
16+
- **Cross Platform:** Supports POSIX and Windows style file paths.
1617
- **Reliable:** Well tested to handle all sorts of circular reference edge cases.
1718

1819
### Installation
@@ -61,7 +62,9 @@ const resolver = new Resolver(globalOpts);
6162
const resolved = await resolver.resolve(sourceObj, resolveOpts);
6263
```
6364

64-
#### Example: Basic Local Resolution
65+
#### Example: Basic Inline Dereferencing
66+
67+
By default, only inline references will be dereferenced.
6568

6669
```ts
6770
import { Resolver } from "@stoplight/json-ref-resolver";
@@ -91,13 +94,13 @@ expect(resolved.result).toEqual({
9194
});
9295
```
9396

94-
#### Example: Resolve a Subset of the Source
97+
#### Example: Dereference a Subset of the Source
9598

96-
This will resolve the minimal number of references needed for the given target, and return the target.
99+
This will dereference the minimal number of references needed for the given target, and return the target.
97100

98-
In the example below, the address reference (`https://slow-website.com/definitions#/address`) will NOT be resolved, since
99-
it is not needed to resolve the `#/user` jsonPointer target we have specified. However, `#/models/user/card` IS resolved since
100-
it is needed in order to full resolve the `#/user` property.
101+
In the example below, the address reference (`https://slow-website.com/definitions#/address`) will NOT be dereferenced, since
102+
it is not needed to resolve the `#/user` jsonPointer target we have specified. However, `#/models/user/card` IS dereferenced since
103+
it is needed in order to full dereference the `#/user` property.
101104

102105
```ts
103106
import { Resolver } from "@stoplight/json-ref-resolver";
@@ -137,40 +140,40 @@ expect(resolved.result).toEqual({
137140
});
138141
```
139142

140-
#### Example: Resolving Remote References with Readers
143+
#### Example: Dereferencing Remote References with Resolvers
141144

142-
By default only local references (those that point to values inside of the original source) are resolved.
145+
By default only inline references (those that point to values inside of the original object) are dereferenced.
143146

144-
In order to resolve remote authorities (file, http, etc) you must provide readers for each authority scheme.
147+
In order to dereference remote URIs (file, http, etc) you must provide resolvers for each URI scheme.
145148

146-
Readers are keyed by scheme, receive the URI to fetch, and must return the fetched data.
149+
Resolvers are keyed by scheme, receive the URI to fetch, and must return the fetched data.
147150

148151
```ts
149152
import { Resolver } from "@stoplight/json-ref-resolver";
150153

151154
// some example http library
152155
import * as axios from "axios";
153156

154-
// if we're in node, we create a file reader with fs
157+
// if we're in node, we create a file resolver with fs
155158
import * as fs from "fs";
156159

157160
// create our resolver instance
158161
const resolver = new Resolver({
159-
// readers can do anything, so long as they define an async read function that resolves to a value
160-
readers: {
161-
// this reader will be invoked for refs with the https protocol
162+
// resolvers can do anything, so long as they define an async read function that resolves to a value
163+
resolvers: {
164+
// this resolver will be invoked for refs with the https protocol
162165
https: {
163-
async read(ref: uri.URI) {
166+
async resolve(ref: uri.URI) {
164167
return axios({
165168
method: "get",
166169
url: String(ref)
167170
});
168171
}
169172
},
170173

171-
// this reader will be invoked for refs with the file protocol
174+
// this resolver will be invoked for refs with the file protocol
172175
file: {
173-
async read(ref: uri.URI) {
176+
async resolve(ref: uri.URI) {
174177
return fs.read(String(ref));
175178
}
176179
}
@@ -201,10 +204,10 @@ expect(resolved.result).toEqual({
201204
});
202205
```
203206

204-
#### Example: Resolving Relative Remote References with the Authority Option
207+
#### Example: Dereferencing Relative Remote References with the baseUri Option
205208

206209
If there are relative remote references (for example, a relative file path `../model.json`), then the location of the source
207-
data must be specified via the `authority` resolve option. Relative references will be resolved against this authority.
210+
data must be specified via the `baseUri` option. Relative references will be dereferenced against this baseUri.
208211

209212
```ts
210213
import { Resolver } from "@stoplight/json-ref-resolver";
@@ -231,7 +234,7 @@ const sourceData = fs.readSync(sourcePath);
231234

232235
const resolved = await resolver.resolve(sourceData, {
233236
// Indicate where the `sourceData` being resolved lives, so that relative remote references can be fetched and resolved.
234-
authority: new URI(sourcePath)
237+
baseUri: new URI(sourcePath)
235238
});
236239

237240
expect(resolved.result).toEqual({
@@ -241,7 +244,7 @@ expect(resolved.result).toEqual({
241244
});
242245
```
243246

244-
In the above example, the user \$ref will resolve to `/models/user.json`, because `../models/user.json` is resolved against the authority of the current document (which was indicated at `/specs/api.json`). Relative references will not work if the source document has no authority set.
247+
In the above example, the user \$ref will resolve to `/models/user.json`, because `../models/user.json` is resolved against the baseUri of the current document (which was indicated at `/specs/api.json`). Relative references will not work if the source document has no baseUri set.
245248

246249
### Contributing
247250

package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,25 @@
4444
"test.watch": "yarn test --watch"
4545
},
4646
"dependencies": {
47-
"@stoplight/json": "2.1.x",
48-
"@types/urijs": "1.19.1",
47+
"@stoplight/json": "2.x.x",
48+
"@types/urijs": "1.x.x",
4949
"dependency-graph": "0.8.x",
5050
"fast-memoize": "2.x.x",
5151
"immer": "3.x.x",
5252
"lodash": "4.x.x",
53-
"urijs": "1.x.x"
53+
"urijs": "1.x.x",
54+
"vscode-uri": "2.x.x"
5455
},
5556
"devDependencies": {
56-
"@stoplight/scripts": "7.0.0",
57-
"@types/jest": "^24.0.13",
58-
"@types/lodash": "4.14.133",
57+
"@stoplight/scripts": "5.x.x",
58+
"@types/jest": "24.x.x",
59+
"@types/lodash": "4.x.x",
5960
"benchmark": "2.x.x",
60-
"jest": "^24.8.0",
61-
"ts-jest": "^24.0.2",
62-
"tslint": "^5.17.0",
63-
"tslint-config-stoplight": "^1.3.0",
64-
"typescript": "3.5.1"
61+
"jest": "24.8.x",
62+
"ts-jest": "24.0.x",
63+
"tslint": "5.17.x",
64+
"tslint-config-stoplight": "1.3.0",
65+
"typescript": "3.5.2"
6566
},
6667
"lint-staged": {
6768
"*.{ts,tsx}$": [

0 commit comments

Comments
 (0)