From 5f3f8b06c37676114a01fc40ee925cbf8bf8c0cd Mon Sep 17 00:00:00 2001 From: Kiss Miklos Date: Sat, 15 Oct 2022 14:59:44 +0200 Subject: [PATCH] Use urlreaders for http refs Signed-off-by: Kiss Miklos --- .../src/lib/bundle.test.ts | 27 +++++++++++++++++++ .../src/lib/bundle.ts | 12 ++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/plugins/catalog-backend-module-openapi/src/lib/bundle.test.ts b/plugins/catalog-backend-module-openapi/src/lib/bundle.test.ts index 0ecd9f7fd1..57ecd54f26 100644 --- a/plugins/catalog-backend-module-openapi/src/lib/bundle.test.ts +++ b/plugins/catalog-backend-module-openapi/src/lib/bundle.test.ts @@ -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()); }); }); diff --git a/plugins/catalog-backend-module-openapi/src/lib/bundle.ts b/plugins/catalog-backend-module-openapi/src/lib/bundle.ts index 6ac8d211e2..8dd880c234 100644 --- a/plugins/catalog-backend-module-openapi/src/lib/bundle.ts +++ b/plugins/catalog-backend-module-openapi/src/lib/bundle.ts @@ -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);