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: obs search reqs w/ spatial params should not return cached data #457

Merged
merged 3 commits into from
Sep 15, 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
8 changes: 2 additions & 6 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,16 +491,12 @@ const util = class util {
if ( queryDup.order_by === "" ) {
delete queryDup.order_by;
}
if ( _.isEmpty( queryDup.lat ) && _.isEmpty( queryDup.lat ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

I believe the root cause of the new symptom is that for v2, parameters are coerced into their defined data type and not all strings coming from the request parameter string as is the case with v1. The lodash _.isEmpty method considers numbers to be empty, resulting in v2 requests with lat/lng having their lat/lng ignored when generating a cache key. All else looks good, so I'll merge this but might restore this bit adding in an additional check of && !_.isNumber

delete queryDup.lat;
delete queryDup.lng;
delete queryDup.radius;
}

// only cacheable params are present, so generate a cache key
if ( _.isEmpty( queryDup ) && _.isEmpty( reqInatDup ) ) {
fileCacheKey = `${prefix}`;
_.each( cacheableParams, ( v, k ) => {
if ( v && !( k === "perPage" && options.ignorePagination ) ) {
if ( v !== null && v !== undefined && !( k === "perPage" && options.ignorePagination ) ) {
fileCacheKey += `-${k}-${v}`;
}
} );
Expand Down
25 changes: 22 additions & 3 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,19 @@ describe( "util", ( ) => {
} ) ).to.eq( "ObservationsController.search-returnBounds-true" );
} );

it( "allows queries with place_id to be cached for obs search", ( ) => {
function expectParamInCacheKey( paramKey, paramVal, paramCacheKey ) {
const req = {
query: {
place_id: 1
[paramKey]: paramVal
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.eq( "ObservationsController.search-placeID-1" );
} ) ).to.eq( `ObservationsController.search-${paramCacheKey}-${paramVal}` );
}

it( "allows queries with place_id to be cached for obs search", ( ) => {
expectParamInCacheKey( "place_id", 1, "placeID" );
} );

it( "does not allow queries with place_id to be cached for obs search when logged in", ( ) => {
Expand All @@ -203,6 +207,21 @@ describe( "util", ( ) => {
} ) ).to.be.null;
} );

function expectParamNotToGenerateCacheKey( paramKey, paramValue ) {
const req = { query: { [paramKey]: paramValue } };
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.be.null;
}

it( "should not generate a key if lat in params", ( ) => expectParamNotToGenerateCacheKey( "lat", 1 ) );
it( "should not generate a key if lng in params", ( ) => expectParamNotToGenerateCacheKey( "lng", 1 ) );
it( "should not generate a key if radius in params", ( ) => expectParamNotToGenerateCacheKey( "radius", 1 ) );
it( "should not generate a key if swlat in params", ( ) => expectParamNotToGenerateCacheKey( "swlat", 1 ) );
it( "should not generate a key if swlng in params", ( ) => expectParamNotToGenerateCacheKey( "swlng", 1 ) );
it( "should not generate a key if nelat in params", ( ) => expectParamNotToGenerateCacheKey( "nelat", 1 ) );
it( "should not generate a key if nelng in params", ( ) => expectParamNotToGenerateCacheKey( "nelng", 1 ) );

it( "includes locale in cache key for obs search by default", ( ) => {
const req = {
query: {
Expand Down
Loading