Use urlreaders for http refs

Signed-off-by: Kiss Miklos <miklos@roadie.io>
This commit is contained in:
Kiss Miklos
2022-10-15 14:59:44 +02:00
parent 862f251718
commit 5f3f8b06c3
2 changed files with 38 additions and 1 deletions
@@ -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);