Skip to content

Commit

Permalink
Merge pull request #29 from jfrconley/semc/array-resolver-error
Browse files Browse the repository at this point in the history
fix: fix issue with JSON ref resolver producing duplicate results
  • Loading branch information
jfrconley authored Mar 29, 2024
2 parents e65e37b + 345a030 commit 61e8469
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-kiwis-buy.md
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions packages/rest/__tests__/src/utils.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/runtime/openapi/openapi-router.mts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export class OpenAPIRouter<
const trouterInstance = new Trouter<
(input: Result<OpenAPIHttpRequest>, registry: AttachmentRegistry) => Promise<Result<OpenAPIHttpResponse>>
>();
this.validateRoutes()
this.validateRoutes();

for (const {path, method, handler} of this.routes) {
const chain = new Nornir<OpenAPIHttpRequest>()
Expand Down
10 changes: 1 addition & 9 deletions packages/rest/src/runtime/utils.mts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ export type DeeplyResolveAllRefsInJsonObject<
[K in keyof Object]: DeeplyResolveAllRefsInJsonObject<Root, Object[K], ResolverCache>
} : Object


const root = {
"a" : { "$ref": "#/b" },
"b" : { "$ref": "#/a" }
} as const;

const test = simpleSpecResolve(root);

export function simpleSpecResolve<
const Root,
>(root: Root): DeeplyResolveAllRefsInJsonObject<NoInfer<Root>> {
Expand All @@ -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);
Expand Down

0 comments on commit 61e8469

Please sign in to comment.