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 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);