Skip to content

Commit

Permalink
fix: fix issue with JSON ref resolver producing duplicate results
Browse files Browse the repository at this point in the history
  • Loading branch information
semcc committed Mar 29, 2024
1 parent 6f0a855 commit 5647d87
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
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 5647d87

Please sign in to comment.