Skip to content

fix: fixes caching for nested objects and adds tests #3873

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

konstantinabl
Copy link
Contributor

@konstantinabl konstantinabl commented Jun 23, 2025

Description:

Updated the generateCacheKey function to properly serialize nested objects using JSON.stringify()

Related issue(s):

Fixes #3872

Testing

  • Added comprehensive unit tests covering:
  • Basic nested object serialization
  • Differentiation between different nested values
  • Complex nested structures with arrays
  • Deep nesting scenarios

Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
@konstantinabl konstantinabl requested review from a team as code owners June 23, 2025 18:20
@konstantinabl konstantinabl requested a review from acuarica June 23, 2025 18:20
@konstantinabl konstantinabl linked an issue Jun 23, 2025 that may be closed by this pull request
@lfdt-bot
Copy link

lfdt-bot commented Jun 23, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

Copy link

github-actions bot commented Jun 23, 2025

Test Results

 20 files  ±0  256 suites  ±0   18m 10s ⏱️ +3s
666 tests ±0  661 ✅ ±0  5 💤 ±0  0 ❌ ±0 
682 runs  ±0  677 ✅ ±0  5 💤 ±0  0 ❌ ±0 

Results for commit 02a5f93. ± Comparison against base commit 928f5c7.

♻️ This comment has been updated with latest results.

@konstantinabl konstantinabl self-assigned this Jun 23, 2025
@konstantinabl konstantinabl added the bug Something isn't working label Jun 23, 2025
@konstantinabl konstantinabl added this to the 0.70.0 milestone Jun 23, 2025
Copy link
Contributor

@natanasow natanasow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@simzzz simzzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can improve the type of generateCacheKey by using unknown

const generateCacheKey = (methodName: string, args: IArgument[]) => {
const generateCacheKey = (methodName: string, args: unknown[]) => {

Please note that this is not strictly about the issue mentioned in the PR. However, given that in the tests (see below) we type casting to use this function, maybe we can change it here.

Also, we can change the outer loop from

  for (const [, value] of Object.entries(args)) {

to

  for (const value of args) {

right?

@@ -208,21 +208,21 @@ describe('cache decorator', () => {

describe('generateCacheKey', () => {
it('should return only the method name when args are empty', () => {
const args = [] as unknown as IArguments;
const args = [] as unknown as IArguments[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we apply the suggestion left in cache.decorator.ts comment, we can avoid this casts.

@@ -123,7 +123,9 @@ const generateCacheKey = (methodName: string, args: IArgument[]) => {
if (value?.constructor?.name != 'RequestDetails') {
if (value && typeof value === 'object') {
for (const [key, innerValue] of Object.entries(value)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, now that we are using JSON.stringify, this loop might not be needed, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Objects in params are not serialized when added to cache key
5 participants