Use urlreaders for http refs
Signed-off-by: Kiss Miklos <miklos@roadie.io>
This commit is contained in:
@@ -88,6 +88,33 @@ describe('bundleOpenApiSpecification', () => {
|
||||
resolveUrl,
|
||||
);
|
||||
|
||||
expect(result).toEqual(expectedResult.trimStart());
|
||||
});
|
||||
it('should use the urlreaders to fetch $refs', async () => {
|
||||
const spec = `
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: 1.0.0
|
||||
title: Swagger Petstore
|
||||
license:
|
||||
name: MIT
|
||||
servers:
|
||||
- url: http://petstore.swagger.io/v1
|
||||
paths:
|
||||
/pets:
|
||||
get:
|
||||
$ref: "https://foo.com/paths/pets/list.yaml"
|
||||
`;
|
||||
|
||||
read.mockResolvedValue(list);
|
||||
|
||||
const result = await bundleOpenApiSpecification(
|
||||
spec,
|
||||
'https://github.com/owner/repo/blob/main/catalog-info.yaml',
|
||||
read,
|
||||
resolveUrl,
|
||||
);
|
||||
|
||||
expect(result).toEqual(expectedResult.trimStart());
|
||||
});
|
||||
});
|
||||
|
||||
@@ -47,11 +47,21 @@ export async function bundleOpenApiSpecification(
|
||||
return await read(url);
|
||||
},
|
||||
};
|
||||
const httpUrlReaderResolver: SwaggerParser.ResolverOptions = {
|
||||
canRead: ref => {
|
||||
const protocol = getProtocol(ref.url);
|
||||
return protocol === 'http' || protocol === 'https';
|
||||
},
|
||||
read: async ref => {
|
||||
const url = resolveUrl(ref.url, baseUrl);
|
||||
return await read(url);
|
||||
},
|
||||
};
|
||||
|
||||
const options: SwaggerParser.Options = {
|
||||
resolve: {
|
||||
file: fileUrlReaderResolver,
|
||||
http: true,
|
||||
http: httpUrlReaderResolver,
|
||||
},
|
||||
};
|
||||
const specObject = parse(specification);
|
||||
|
||||
Reference in New Issue
Block a user