-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e5553c
commit 7144359
Showing
6 changed files
with
233 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,81 @@ | ||
import { Resolver as SpectralResolver } from '@stoplight/json-ref-resolver'; | ||
import { Resolver as SpectralResolver } from '@stoplight/spectral-ref-resolver'; | ||
import { resolveFile, resolveHttp } from '@stoplight/json-ref-readers'; | ||
|
||
import type Uri from 'urijs'; | ||
|
||
export interface Resolver { | ||
schema: 'file' | 'http' | 'https' | string; | ||
order?: number; | ||
canRead?: boolean | ((input: ResolverInput) => boolean); | ||
read: (input: ResolverInput) => string | Buffer | Promise<string | Buffer>; | ||
canRead?: boolean | ((uri: Uri, ctx?: any) => boolean); | ||
read: (uri: Uri, ctx?: any) => string | undefined | Promise<string | undefined>; | ||
} | ||
|
||
export interface ResolverInput { | ||
url: string; | ||
extension: string; | ||
export interface ResolverOptions { | ||
resolvers?: Array<Resolver>; | ||
} | ||
|
||
interface ResolverOptions { | ||
resolvers: Array<Resolver>; | ||
} | ||
export function createResolver(options: ResolverOptions = {}): SpectralResolver { | ||
const availableResolvers: Array<Resolver> = [ | ||
...createDefaultResolvers(), | ||
...(options?.resolvers || []) | ||
].map(r => ({ | ||
...r, | ||
order: r.order || Number.MAX_SAFE_INTEGER, | ||
canRead: typeof r.canRead === 'undefined' ? true: r.canRead, | ||
})); | ||
const availableSchemas = [...new Set(availableResolvers.map(r => r.schema))]; | ||
const resolvers = availableSchemas.reduce((acc, schema) => { | ||
acc[schema] = { resolve: createSchemaResolver(schema, availableResolvers) }; | ||
return acc; | ||
}, {} as Record<string, { resolve: (uri: Uri, ctx?: any) => string | Promise<string> }>); | ||
|
||
export function createResolver(options?: ResolverOptions): SpectralResolver { | ||
return new SpectralResolver({ | ||
resolvers: { | ||
https: { resolve: resolveHttp }, | ||
http: { resolve: resolveHttp }, | ||
file: { resolve: resolveFile }, | ||
}, | ||
resolvers: resolvers as any, | ||
}); | ||
} | ||
|
||
export function createFileResolver() { | ||
function createDefaultResolvers(): Array<Resolver> { | ||
return [ | ||
{ | ||
schema: 'file', | ||
read: resolveFile as (input: Uri, ctx?: any) => string | Promise<string>, | ||
}, | ||
{ | ||
schema: 'https', | ||
read: resolveHttp as (input: Uri, ctx?: any) => string | Promise<string>, | ||
}, | ||
{ | ||
schema: 'http', | ||
read: resolveHttp as (input: Uri, ctx?: any) => string | Promise<string>, | ||
}, | ||
] | ||
} | ||
|
||
function createSchemaResolver(schema: string, allResolvers: Array<Resolver>): (uri: Uri, ctx?: any) => string | Promise<string> { | ||
const resolvers = allResolvers.filter(r => r.schema === schema).sort((a, b) => { return (a.order as number) - (b.order as number); }); | ||
return async (uri, ctx) => { | ||
let result: string | undefined = undefined; | ||
let lastError: Error | undefined; | ||
for (const resolver of resolvers) { | ||
try { | ||
if (!canRead(resolver, uri, ctx)) continue; | ||
|
||
result = await resolver.read(uri, ctx); | ||
if (typeof result === 'string') { | ||
break; | ||
} | ||
} catch(e: any) { | ||
lastError = e; | ||
continue; | ||
} | ||
} | ||
if (typeof result !== 'string') { | ||
throw lastError || new Error(`None of the available resolvers for "${schema}" can resolve the given reference.`); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
export function createHttpResolver() { | ||
|
||
function canRead(resolver: Resolver, uri: Uri, ctx?: any) { | ||
return typeof resolver.canRead === 'function' ? resolver.canRead(uri, ctx) : resolver.canRead; | ||
} |
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,5 @@ | ||
payload: | ||
type: object | ||
properties: | ||
someProperty: | ||
type: string |
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,150 @@ | ||
import { AsyncAPIDocumentV2 } from '../src/models'; | ||
import { Parser } from '../src/parser'; | ||
import { parse } from '../src/parse'; | ||
|
||
describe('custom resolver', function() { | ||
it('should resolve document references', async function() { | ||
const parser = new Parser(); | ||
|
||
const document = { | ||
asyncapi: '2.0.0', | ||
info: { | ||
title: 'Valid AsyncApi document', | ||
version: '1.0', | ||
}, | ||
channels: { | ||
channel: { | ||
publish: { | ||
operationId: 'publish', | ||
message: { | ||
$ref: '#/components/messages/message' | ||
} | ||
}, | ||
} | ||
}, | ||
components: { | ||
messages: { | ||
message: { | ||
payload: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
const { parsed } = await parse(parser, document); | ||
|
||
expect(parsed).toBeInstanceOf(AsyncAPIDocumentV2); | ||
const refMessage = parsed?.channels().get('channel')?.operations().get('publish')?.messages()[0]; | ||
expect(refMessage?.json()).not.toBeUndefined(); | ||
expect(refMessage?.json() === parsed?.components().messages().get('message')?.json()).toEqual(true); | ||
}); | ||
|
||
it('should resolve file references', async function() { | ||
const parser = new Parser(); | ||
|
||
const document = { | ||
asyncapi: '2.0.0', | ||
info: { | ||
title: 'Valid AsyncApi document', | ||
version: '1.0', | ||
}, | ||
channels: { | ||
channel: { | ||
publish: { | ||
operationId: 'publish', | ||
message: { | ||
$ref: './mocks/simple-message.yaml' | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
const { parsed } = await parse(parser, document, { validateOptions: { path: __filename } }); | ||
|
||
expect(parsed).toBeInstanceOf(AsyncAPIDocumentV2); | ||
const refMessage = parsed?.channels().get('channel')?.operations().get('publish')?.messages()[0]; | ||
expect(refMessage?.json()).not.toBeUndefined(); | ||
expect(refMessage?.json('$ref')).toBeUndefined(); | ||
}); | ||
|
||
it('should resolve http references', async function() { | ||
const parser = new Parser(); | ||
|
||
const document = { | ||
asyncapi: '2.0.0', | ||
info: { | ||
title: 'Valid AsyncApi document', | ||
version: '1.0', | ||
}, | ||
channels: { | ||
channel: { | ||
publish: { | ||
operationId: 'publish', | ||
message: { | ||
$ref: 'https://raw.githubusercontent.com/asyncapi/spec/v2.0.0/examples/2.0.0/streetlights.yml#/components/messages/lightMeasured' | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
const { parsed } = await parse(parser, document); | ||
|
||
expect(parsed).toBeInstanceOf(AsyncAPIDocumentV2); // we should have parsed document | ||
}); | ||
|
||
it('should resolve custom protocols', async function() { | ||
const parser = new Parser({ | ||
__unstableResolver: { | ||
resolvers: [ | ||
{ | ||
schema: 'customProtocol', | ||
read(uri) { | ||
if (uri.path() === '/someRef') { | ||
return '{"someRef": "value"}'; | ||
} | ||
return '{"anotherRef": "value"}'; | ||
}, | ||
} | ||
] | ||
} | ||
}); | ||
|
||
const document = { | ||
asyncapi: '2.0.0', | ||
info: { | ||
title: 'Valid AsyncApi document', | ||
version: '1.0', | ||
}, | ||
channels: { | ||
channel: { | ||
publish: { | ||
operationId: 'publish', | ||
message: { | ||
payload: { | ||
$ref: 'customProtocol:///someRef' | ||
} | ||
} | ||
}, | ||
subscribe: { | ||
operationId: 'subscribe', | ||
message: { | ||
payload: { | ||
$ref: 'customProtocol:///anotherRef' | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
const { parsed } = await parse(parser, document); | ||
|
||
expect(parsed).toBeInstanceOf(AsyncAPIDocumentV2); | ||
const someRef = parsed?.channels().get('channel')?.operations().get('publish')?.messages()[0]?.payload(); | ||
expect(someRef?.json()).toEqual({ someRef: 'value' }); | ||
expect(someRef?.json('$ref')).toBeUndefined(); | ||
const anotherRef = parsed?.channels().get('channel')?.operations().get('subscribe')?.messages()[0]?.payload(); | ||
expect(anotherRef?.json()).toEqual({ anotherRef: 'value' }); | ||
expect(anotherRef?.json('$ref')).toBeUndefined(); | ||
}); | ||
}); |