Skip to content

feat: switch to json-schema-ref-parser #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@

# misc
.idea

coverage/
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ yarn add @stoplight/json-ref-readers

## Usage

The library exports two functions: `resolveHttp` and `resolveFile`. Both take `uri.URI` and resolve to a string containing requested resource.

```ts
import { Resolver } from '@stoplight/json-ref-resolver';
import { resolveFile, resolveHttp } from '@stoplight/json-ref-readers';
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { jsonParser, yamlParser } from '@stoplight/json-ref-readers';

const parser = new $RefParser();

const httpAndFileResolver = new Resolver({
resolvers: {
https: { resolve: resolveHttp },
http: { resolve: resolveHttp },
file: { resolve: resolveFile },
parser.dereference(filePath, {
parse: {
json: jsonParser,
yaml: yamlParser,
},
});
```
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@
"test.prod": "yarn lint && yarn test --coverage --no-cache",
"test.update": "yarn test --updateSnapshot"
},
"peerDependencies": {
"@apidevtools/json-schema-ref-parser": ">=9.0.2"
},
"dependencies": {
"@stoplight/json": "^3.7.1",
"@stoplight/path": "^1.3.1",
"@stoplight/yaml": "^3.8.1",
"node-fetch": "^2.6.0"
},
"devDependencies": {
"@apidevtools/json-schema-ref-parser": "https://github.com/stoplightio/json-schema-ref-parser#03462047af4e62e889038f5e4ce53c552fbefaee",
"@stoplight/scripts": "^7.0.3",
"@stoplight/types": "^11.6.0",
"@types/jest": "^24.0.18",
"@types/nock": "^11.1.0",
"@types/node-fetch": "^2.5.2",
"@types/urijs": "^1.19.4",
"jest": "^24.9.0",
"nock": "^11.7.0",
"ts-jest": "^24.1.0",
"tslint": "^5.20.0",
"tslint-config-stoplight": "^1.3.0",
"typescript": "^3.6.3",
"urijs": "^1.19.2"
"typescript": "^3.8.3"
}
}
16 changes: 0 additions & 16 deletions src/__tests__/file.spec.ts

This file was deleted.

56 changes: 0 additions & 56 deletions src/__tests__/http.spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/file.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/http.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { createResolveHttp, resolveHttp, NetworkError, OpenError } from './http';
export { resolveFile } from './file';
export * from './parsers';
export * from './resolvers';
87 changes: 87 additions & 0 deletions src/parsers/__tests__/json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { readFile } from 'fs';
import $RefParser = require('@apidevtools/json-schema-ref-parser');
import { jsonParser } from '../index';
import { resolveFile } from '../../resolvers';

jest.mock('fs');

describe('JSON Parser', () => {
afterEach(() => {
jest.resetAllMocks();
});

it('works', async () => {
((readFile as unknown) as jest.Mock).mockImplementation((_path, _opts, cb) => cb(null, '{"foo":true}'));

const parser = new $RefParser();
const deref = parser.dereference({ $ref: './baz.json' }, {
resolve: {
file: resolveFile,
},
parse: {
json: jsonParser,
yaml: false,
}
});

return expect(deref).resolves.toStrictEqual({ foo: true });
});

it('retains the order of keys', async () => {
((readFile as unknown) as jest.Mock).mockImplementation((_path, _opts, cb) => cb(null, '{"foo":true,"0": false}'));

const parser = new $RefParser();
const deref = await parser.dereference({ $ref: './baz.json' }, {
resolve: {
file: resolveFile,
},
parse: {
json: jsonParser,
yaml: false,
}
});

expect(Object.keys(deref)).toEqual(['foo', '0'])
});

it('handles failures', async () => {
((readFile as unknown) as jest.Mock).mockImplementation((_path, _opts, cb) => cb(null, '{bar zx;cxz"":true}'));

const parser = new $RefParser();
const deref = parser.dereference({ $ref: './baz.json' }, {
resolve: {
file: resolveFile,
},
parse: {
json: jsonParser,
yaml: false,
}
});

return expect(deref).rejects.toThrow($RefParser.ParserError);
});

it('integrates with continueOnError', () => {
((readFile as unknown) as jest.Mock).mockImplementation((_path, _opts, cb) => cb(null, '{"foo": true,}'));

const parser = new $RefParser();
const deref = parser.dereference({ $ref: './baz.json' }, {
resolve: {
file: resolveFile,
},
parse: {
json: jsonParser,
yaml: false,
},
continueOnError: true,
});

return Promise.all([
expect(deref).rejects.toThrow($RefParser.JSONParserErrorGroup),
expect(deref).rejects.toHaveProperty('errors', [
expect.objectContaining({ name: 'ParserError', message: 'PropertyNameExpected' }),
expect.objectContaining({ name: 'ParserError', message: 'ValueExpected' }),
]),
]);
})
});
83 changes: 83 additions & 0 deletions src/parsers/__tests__/yaml.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { readFile } from 'fs';
import $RefParser = require('@apidevtools/json-schema-ref-parser');
import { yamlParser } from '../index';
import { resolveFile } from '../../resolvers';

jest.mock('fs');

describe('YAML Parser', () => {
afterEach(() => {
jest.resetAllMocks();
});

it('works', async () => {
((readFile as unknown) as jest.Mock).mockImplementation((_path, _opts, cb) => cb(null, '{"foo":true}'));

const parser = new $RefParser();
const deref = parser.dereference({ $ref: './baz.yaml' }, {
resolve: {
file: resolveFile,
},
parse: {
yaml: yamlParser,
}
});

return expect(deref).resolves.toStrictEqual({ foo: true });
});

it('retains the order of keys', async () => {
((readFile as unknown) as jest.Mock).mockImplementation((_path, _opts, cb) => cb(null, '{"foo":true,"0": false}'));

const parser = new $RefParser();
const deref = await parser.dereference({ $ref: './baz.yaml' }, {
resolve: {
file: resolveFile,
},
parse: {
yaml: yamlParser,
}
});

expect(Object.keys(deref)).toEqual(['foo', '0'])
});

it('handles failures', async () => {
((readFile as unknown) as jest.Mock).mockImplementation((_path, _opts, cb) => cb(null, 'foo:\n d\na'));

const parser = new $RefParser();
const deref = parser.dereference({ $ref: './baz.yaml' }, {
resolve: {
file: resolveFile,
},
parse: {
yaml: yamlParser,
}
});

return expect(deref).rejects.toThrow($RefParser.ParserError);
});

it('integrates with continueOnError', () => {
((readFile as unknown) as jest.Mock).mockImplementation((_path, _opts, cb) => cb(null, 'foo:\n d\na'));

const parser = new $RefParser();
const deref = parser.dereference({ $ref: './baz.yaml' }, {
resolve: {
file: resolveFile,
},
parse: {
yaml: yamlParser,
},
continueOnError: true,
});

return Promise.all([
expect(deref).rejects.toThrow($RefParser.JSONParserErrorGroup),
expect(deref).rejects.toHaveProperty('errors', [
expect.objectContaining({ name: 'ParserError', message: 'can not read a block mapping entry; a multiline key may not be an implicit key' }),
expect.objectContaining({ name: 'ParserError', message: 'can not read an implicit mapping pair; a colon is missed' }),
]),
]);
})
});
Loading