From 5647d877f9988813538e088906f67478825de3b3 Mon Sep 17 00:00:00 2001 From: semcc Date: Fri, 29 Mar 2024 13:17:48 -0700 Subject: [PATCH 1/2] fix: fix issue with JSON ref resolver producing duplicate results --- packages/rest/__tests__/src/utils.spec.mts | 17 +++++++++++++++++ .../rest/src/runtime/openapi/openapi-router.mts | 2 +- packages/rest/src/runtime/utils.mts | 10 +--------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/rest/__tests__/src/utils.spec.mts b/packages/rest/__tests__/src/utils.spec.mts index 12b88c5..e2862d8 100644 --- a/packages/rest/__tests__/src/utils.spec.mts +++ b/packages/rest/__tests__/src/utils.spec.mts @@ -32,6 +32,23 @@ describe("utils", () => { expect(result).toEqual({name: 'test'}); }); + // Test case: When the current object is an array and each component does not have a $ref property + test("resolves simple array items", () => { + // Arrange + const items = [{ + name: "item1" + }, { + name: "item2" + }] as const; + const root = { items } as const; + // Act + const result = simpleSpecResolve(root); + // Assert + expect(result).toEqual({ + items: [{ name: "item1" }, { name: "item2" }], + }); + }) + // Test case: When the current object is an array test('resolves array items', () => { // Arrange diff --git a/packages/rest/src/runtime/openapi/openapi-router.mts b/packages/rest/src/runtime/openapi/openapi-router.mts index 9be2635..5dd362b 100644 --- a/packages/rest/src/runtime/openapi/openapi-router.mts +++ b/packages/rest/src/runtime/openapi/openapi-router.mts @@ -245,7 +245,7 @@ export class OpenAPIRouter< const trouterInstance = new Trouter< (input: Result, registry: AttachmentRegistry) => Promise> >(); - this.validateRoutes() + this.validateRoutes(); for (const {path, method, handler} of this.routes) { const chain = new Nornir() diff --git a/packages/rest/src/runtime/utils.mts b/packages/rest/src/runtime/utils.mts index d1780e1..dfb3cf4 100644 --- a/packages/rest/src/runtime/utils.mts +++ b/packages/rest/src/runtime/utils.mts @@ -73,14 +73,6 @@ export type DeeplyResolveAllRefsInJsonObject< [K in keyof Object]: DeeplyResolveAllRefsInJsonObject } : Object - -const root = { - "a" : { "$ref": "#/b" }, - "b" : { "$ref": "#/a" } -} as const; - -const test = simpleSpecResolve(root); - export function simpleSpecResolve< const Root, >(root: Root): DeeplyResolveAllRefsInJsonObject> { @@ -101,7 +93,7 @@ function simpleSpecResolveInternal< return simpleSpecResolveInternal(root, result, resolveRefInJsonObject(root, current["$ref"]), resolverCache.concat(current["$ref"])); } if (Array.isArray(current)) { - return current.map((value) => simpleSpecResolveInternal(root, result, value, resolverCache)); + return current.map((value) => simpleSpecResolveInternal(root, {}, value, resolverCache)); } for (const key in current) { result[key] = simpleSpecResolveInternal(root, result[key], current[key], resolverCache); From 345a030359acefaeb72c70560e7ded708a08eaa9 Mon Sep 17 00:00:00 2001 From: semcc Date: Fri, 29 Mar 2024 13:20:03 -0700 Subject: [PATCH 2/2] docs(changeset): Fix issue with JSON ref resolver producing duplicate results when root component is an array with subcomponents not containing references --- .changeset/lazy-kiwis-buy.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/lazy-kiwis-buy.md diff --git a/.changeset/lazy-kiwis-buy.md b/.changeset/lazy-kiwis-buy.md new file mode 100644 index 0000000..4496b1b --- /dev/null +++ b/.changeset/lazy-kiwis-buy.md @@ -0,0 +1,5 @@ +--- +"@nornir/rest": patch +--- + +Fix issue with JSON ref resolver producing duplicate results when root component is an array with subcomponents not containing references