Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix issue with JSON ref resolver producing duplicate results #29

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading