Bug 37902 - Timezone ignored when passing rfc3339 formatted date (search 'q')
Summary: Timezone ignored when passing rfc3339 formatted date (search 'q')
Status: Passed QA
Alias: None
Product: Koha
Classification: Unclassified
Component: REST API (show other bugs)
Version: unspecified
Hardware: All All
: P5 - low enhancement
Assignee: Jonathan Druart
QA Contact: Kyle M Hall (khall)
URL:
Keywords:
Depends on: 37831
Blocks: 37903 37905 37952
  Show dependency treegraph
 
Reported: 2024-09-12 12:21 UTC by Jonathan Druart
Modified: 2024-09-27 15:58 UTC (History)
5 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments
Bug 37902: Add tests (3.44 KB, patch)
2024-09-12 12:56 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 37902: Make sure filtered_params are converted (2.86 KB, patch)
2024-09-12 12:56 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 37902: Make sure we loop over if a structure is passed (6.17 KB, patch)
2024-09-12 12:56 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 37902: TODOs (2.32 KB, patch)
2024-09-12 12:56 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 37902: Adjust timezone (1.37 KB, patch)
2024-09-12 14:24 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 37902: Adjust timezone (957 bytes, patch)
2024-09-12 14:27 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 37902: Do not convert a date if it has like markers (1.30 KB, patch)
2024-09-16 08:46 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 37902: Add tests (3.50 KB, patch)
2024-09-16 14:10 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 37902: Apply exact match for datetime (2.30 KB, patch)
2024-09-18 08:04 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 37902: Make sure filtered_params are converted (2.92 KB, patch)
2024-09-19 13:45 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 37902: Make sure we loop over if a structure is passed (6.22 KB, patch)
2024-09-19 13:45 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 37902: TODOs (2.37 KB, patch)
2024-09-19 13:45 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 37902: Adjust timezone (1014 bytes, patch)
2024-09-19 13:45 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 37902: Do not convert a date if it has like markers (1.36 KB, patch)
2024-09-19 13:45 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 37902: Add tests (3.55 KB, patch)
2024-09-19 13:45 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 37902: Apply exact match for datetime (2.35 KB, patch)
2024-09-19 13:45 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review
Bug 37902: Make sure filtered_params are converted (2.99 KB, patch)
2024-09-27 13:25 UTC, Kyle M Hall (khall)
Details | Diff | Splinter Review
Bug 37902: Make sure we loop over if a structure is passed (6.29 KB, patch)
2024-09-27 13:25 UTC, Kyle M Hall (khall)
Details | Diff | Splinter Review
Bug 37902: TODOs (2.44 KB, patch)
2024-09-27 13:25 UTC, Kyle M Hall (khall)
Details | Diff | Splinter Review
Bug 37902: Adjust timezone (1.06 KB, patch)
2024-09-27 13:25 UTC, Kyle M Hall (khall)
Details | Diff | Splinter Review
Bug 37902: Do not convert a date if it has like markers (1.43 KB, patch)
2024-09-27 13:25 UTC, Kyle M Hall (khall)
Details | Diff | Splinter Review
Bug 37902: Add tests (3.62 KB, patch)
2024-09-27 13:25 UTC, Kyle M Hall (khall)
Details | Diff | Splinter Review
Bug 37902: Apply exact match for datetime (2.42 KB, patch)
2024-09-27 13:25 UTC, Kyle M Hall (khall)
Details | Diff | Splinter Review
Bug 37902: (QA follow-up) Tidy Query.pm & Object.t (56.64 KB, patch)
2024-09-27 13:25 UTC, Kyle M Hall (khall)
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Druart 2024-09-12 12:21:35 UTC
2. GET /api/v1/jobs?q={"enqueued_on":{">":"2024-09-04T09:26:52-03:00"}}
is generating the following query

SELECT `me`.`id`, `me`.`status`, `me`.`progress`, `me`.`size`, `me`.`borrowernumber`, `me`.`type`, `me`.`queue`, `me`.`data`, `me`.`context`, `me`.`enqueued_on`, `me`.`started_on`, `me`.`ended_on` 
  FROM `background_jobs` `me` 
WHERE `enqueued_on` > '2024-09-04T09:26:52-03:00' 

But when you run it manually, you get a warning:
  | Warning | 1292 | Truncated incorrect datetime value: '2024-09-04T09:26:52-03:00' |
Comment 1 Jonathan Druart 2024-09-12 12:56:14 UTC
Created attachment 171389 [details] [review]
Bug 37902: Add tests
Comment 2 Jonathan Druart 2024-09-12 12:56:17 UTC
Created attachment 171390 [details] [review]
Bug 37902: Make sure filtered_params are converted

Previously we only adjusted the attributes that were passed directly,
not the ones in 'q' (which is the recommended way now)
Comment 3 Jonathan Druart 2024-09-12 12:56:19 UTC
Created attachment 171391 [details] [review]
Bug 37902: Make sure we loop over if a structure is passed
Comment 4 Jonathan Druart 2024-09-12 12:56:22 UTC
Created attachment 171392 [details] [review]
Bug 37902: TODOs

There are still different structures we won't handle properly.
This patch adds conditionals to prevent failures or warnings.

Should be done, but later.
Comment 5 Jonathan Druart 2024-09-12 13:08:20 UTC
Note that t/db_dependent/api/v1/checkouts.t is failing if the timezone in conf is not empty. It was failing before and still after after. The test is wrong (it does not mock the config).
Comment 6 Jonathan Druart 2024-09-12 13:40:56 UTC
No, it does not work...
Comment 7 Jonathan Druart 2024-09-12 14:24:53 UTC
Created attachment 171402 [details] [review]
Bug 37902: Adjust timezone

We didn't take into account the server's timezone.

This does not feel right, we are not supposed to deal with time zone
outside of Koha::DateUtils.
Comment 8 Jonathan Druart 2024-09-12 14:26:02 UTC
(In reply to Jonathan Druart from comment #7)
> Created attachment 171402 [details] [review] [review]
> Bug 37902: Adjust timezone
> 
> We didn't take into account the server's timezone.
> 
> This does not feel right, we are not supposed to deal with time zone
> outside of Koha::DateUtils.

After retesting bug 37905 I noticed that the patch was wrong, and so something was missing here. However I don't understand why the tests were passing without!
Comment 9 Jonathan Druart 2024-09-12 14:27:22 UTC
Created attachment 171403 [details] [review]
Bug 37902: Adjust timezone

We didn't take into account the server's timezone.

This does not feel right, we are not supposed to deal with time zone
outside of Koha::DateUtils.
Comment 10 Jonathan Druart 2024-09-12 14:45:16 UTC
There are failures

t/db_dependent/api/v1/patrons.t .......... 
            #   Failed test '200 OK'
            #   at t/db_dependent/api/v1/patrons.t line 173.
            #          got: '500'
            #     expected: '200'

            #   Failed test 'Filtering by date works'
            #   at t/db_dependent/api/v1/patrons.t line 173.
            #          got: undef
            #     expected: '179'

            #   Failed test '200 OK'
            #   at t/db_dependent/api/v1/patrons.t line 177.
            #          got: '500'
            #     expected: '200'

            #   Failed test 'Filtering by date-time works'
            #   at t/db_dependent/api/v1/patrons.t line 177.
            #          got: undef
            #     expected: '179'
            # Looks like you failed 4 tests of 12.
Comment 11 Jonathan Druart 2024-09-12 14:53:10 UTC
(In reply to Jonathan Druart from comment #10)
> There are failures
> 
> t/db_dependent/api/v1/patrons.t .......... 
>             #   Failed test '200 OK'
>             #   at t/db_dependent/api/v1/patrons.t line 173.
>             #          got: '500'
>             #     expected: '200'

This is a bad one.

Invalid date format: %1980-06-18% at /kohadevbox/koha/Koha/REST/Plugin/Objects.pm line 288

Because we are now hitting
Koha/REST/Plugin/Query.pm
195             my $match = $reserved_params->{_match} // 'contains';                                     
196                                                                                                       
197             foreach my $param ( keys %{$filtered_params} ) {                                          
198                 if ( $match eq 'contains' ) {                                                         
199                     $params->{$param} =                                                               
200                       { like => '%' . $filtered_params->{$param} . '%' };
Comment 12 Jonathan Druart 2024-09-16 08:46:20 UTC
Created attachment 171515 [details] [review]
Bug 37902: Do not convert a date if it has like markers

If we pass a datetime parameter we are adding "like" operator and % at
the beginning and ending of the attribute value.
For instance:
  attributes=2024-09-16 10:11:12
  attributes:{ like => '%2024-09-16 10:11:12%' }

We do not want to reach the fixup code and raise an exception.

However I don't think we should add the like for datetime attributes
actually. But can we modify this behaviour now?
Comment 13 Jonathan Druart 2024-09-16 08:47:36 UTC
There is still (at least) one failing test:

            #   Failed test 'Filtering by date-time works'
            #   at t/db_dependent/api/v1/patrons.t line 177.
            #          got: undef
            #     expected: '401'
            # Looks like you failed 1 test of 12.

I don't think we are processing the values correctly. Why do we add like operator for dates? Do we want to keep this behaviour?

I really need feedback and review on those patches before continuing working on this.
Comment 14 Jonathan Druart 2024-09-16 14:10:52 UTC
Created attachment 171551 [details] [review]
Bug 37902: Add tests
Comment 15 Jonathan Druart 2024-09-16 14:12:33 UTC
(In reply to Jonathan Druart from comment #14)
> Created attachment 171551 [details] [review] [review]
> Bug 37902: Add tests

This is fixing Martin's failure (401 unauthorized), I had this set in my custom.sql. Sorry!
And kudos to Tomas who found this!
Comment 16 Nick Clemens (kidclamp) 2024-09-16 14:13:57 UTC
(In reply to Jonathan Druart from comment #13)
> There is still (at least) one failing test:
> 
>             #   Failed test 'Filtering by date-time works'
>             #   at t/db_dependent/api/v1/patrons.t line 177.
>             #          got: undef
>             #     expected: '401'
>             # Looks like you failed 1 test of 12.
> 
> I don't think we are processing the values correctly. Why do we add like
> operator for dates? Do we want to keep this behaviour?
> 
> I really need feedback and review on those patches before continuing working
> on this.

I think the like search for dates would only be used for partial date search in a column - i.e. "2024-07" - but we could probably replace that with year/month/day dropdowns - or just a datepicker and have reports for more complicated date searching?
Comment 17 Jonathan Druart 2024-09-16 14:22:25 UTC
(In reply to Nick Clemens (kidclamp) from comment #16)
> (In reply to Jonathan Druart from comment #13)
> > There is still (at least) one failing test:
> > 
> >             #   Failed test 'Filtering by date-time works'
> >             #   at t/db_dependent/api/v1/patrons.t line 177.
> >             #          got: undef
> >             #     expected: '401'
> >             # Looks like you failed 1 test of 12.
> > 
> > I don't think we are processing the values correctly. Why do we add like
> > operator for dates? Do we want to keep this behaviour?
> > 
> > I really need feedback and review on those patches before continuing working
> > on this.
> 
> I think the like search for dates would only be used for partial date search
> in a column - i.e. "2024-07" - but we could probably replace that with
> year/month/day dropdowns - or just a datepicker and have reports for more
> complicated date searching?

Yes, but this could be done client-side. If we are passing an invalid/incomplete date then we can build the "like" part ourselves.
Comment 18 Jonathan Druart 2024-09-16 14:23:15 UTC
(In reply to Nick Clemens (kidclamp) from comment #16)
> (In reply to Jonathan Druart from comment #13)
> > There is still (at least) one failing test:
> > 
> >             #   Failed test 'Filtering by date-time works'
> >             #   at t/db_dependent/api/v1/patrons.t line 177.
> >             #          got: undef
> >             #     expected: '401'
> >             # Looks like you failed 1 test of 12.
> > 
> > I don't think we are processing the values correctly. Why do we add like
> > operator for dates? Do we want to keep this behaviour?
> > 
> > I really need feedback and review on those patches before continuing working
> > on this.
> 
> I think the like search for dates would only be used for partial date search
> in a column - i.e. "2024-07" - but we could probably replace that with
> year/month/day dropdowns - or just a datepicker and have reports for more
> complicated date searching?

Flagging this comment as important, it's actually a TODO for some DT searches.
Comment 19 Nick Clemens (kidclamp) 2024-09-16 14:42:36 UTC
(In reply to Jonathan Druart from comment #17)
> Yes, but this could be done client-side. If we are passing an
> invalid/incomplete date then we can build the "like" part ourselves.

Good point, we could skip autotruncation for date fields and have the user add them if needed
Comment 20 Jonathan Druart 2024-09-18 08:04:51 UTC
Created attachment 171652 [details] [review]
Bug 37902: Apply exact match for datetime

We do not want to apply "like" and do a "contains" search if a correctly
formatted date is passed (ie. starting with "YYYY-MM-DD HH:MM:SS")

It causes underlying problems if we add '%' characters to this string as
it will then become an invalid date.

There are several ways of dealing with this problem. This patch is
suggesting the easiest path: Apply an exact search (ie. do not add '%')
if the value appears to be a datetime.

Certainly not the best looking patch but it seems to be quite effective:
  * no need to change the client
  * no need to rework build_query_params, merge_q_params, attributes_from_api

We could (to confirm) pass the result set, but it seems a lot of additional processing
(that is done later already, in attributes_from_api)
Comment 21 Nick Clemens (kidclamp) 2024-09-19 13:45:09 UTC
Created attachment 171762 [details] [review]
Bug 37902: Make sure filtered_params are converted

Previously we only adjusted the attributes that were passed directly,
not the ones in 'q' (which is the recommended way now)

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 22 Nick Clemens (kidclamp) 2024-09-19 13:45:11 UTC
Created attachment 171763 [details] [review]
Bug 37902: Make sure we loop over if a structure is passed

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 23 Nick Clemens (kidclamp) 2024-09-19 13:45:13 UTC
Created attachment 171764 [details] [review]
Bug 37902: TODOs

There are still different structures we won't handle properly.
This patch adds conditionals to prevent failures or warnings.

Should be done, but later.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 24 Nick Clemens (kidclamp) 2024-09-19 13:45:15 UTC
Created attachment 171765 [details] [review]
Bug 37902: Adjust timezone

We didn't take into account the server's timezone.

This does not feel right, we are not supposed to deal with time zone
outside of Koha::DateUtils.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 25 Nick Clemens (kidclamp) 2024-09-19 13:45:18 UTC
Created attachment 171766 [details] [review]
Bug 37902: Do not convert a date if it has like markers

If we pass a datetime parameter we are adding "like" operator and % at
the beginning and ending of the attribute value.
For instance:
  attributes=2024-09-16 10:11:12
  attributes:{ like => '%2024-09-16 10:11:12%' }

We do not want to reach the fixup code and raise an exception.

However I don't think we should add the like for datetime attributes
actually. But can we modify this behaviour now?

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 26 Nick Clemens (kidclamp) 2024-09-19 13:45:20 UTC
Created attachment 171767 [details] [review]
Bug 37902: Add tests

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 27 Nick Clemens (kidclamp) 2024-09-19 13:45:22 UTC
Created attachment 171768 [details] [review]
Bug 37902: Apply exact match for datetime

We do not want to apply "like" and do a "contains" search if a correctly
formatted date is passed (ie. starting with "YYYY-MM-DD HH:MM:SS")

It causes underlying problems if we add '%' characters to this string as
it will then become an invalid date.

There are several ways of dealing with this problem. This patch is
suggesting the easiest path: Apply an exact search (ie. do not add '%')
if the value appears to be a datetime.

Certainly not the best looking patch but it seems to be quite effective:
  * no need to change the client
  * no need to rework build_query_params, merge_q_params, attributes_from_api

We could (to confirm) pass the result set, but it seems a lot of additional processing
(that is done later already, in attributes_from_api)

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 28 Nick Clemens (kidclamp) 2024-09-19 13:46:04 UTC
Makes sense, tests pass, there are tidiness complaints from QA tools
Comment 29 David Cook 2024-09-26 20:55:41 UTC
So bug 36217 stopped us from passing a UTC timestamp like 2024-09-26T18:41:24.268Z to the API. This timestamp was generated by Date().toISOString().

Bug 37905 wants to use a RFC3339 timestamp like 2024-09-26T14:45:36-04:00 via dayjs().format()... and bug 37902 (this one) allows us to use RFC3339...

If we're going to convert the incoming timestamp from the client... do we want to use RFC3339 or why not keep using ISO? 

Anyway, I'll look at this more later. Doing things in person with humans today and now the day is over...
Comment 30 Jonathan Druart 2024-09-27 12:29:35 UTC
David, if you are asking what's the next step then have a look at bug 37952.
Comment 31 Kyle M Hall (khall) 2024-09-27 13:25:26 UTC
Created attachment 172105 [details] [review]
Bug 37902: Make sure filtered_params are converted

Previously we only adjusted the attributes that were passed directly,
not the ones in 'q' (which is the recommended way now)

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 32 Kyle M Hall (khall) 2024-09-27 13:25:37 UTC
Created attachment 172106 [details] [review]
Bug 37902: Make sure we loop over if a structure is passed

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 33 Kyle M Hall (khall) 2024-09-27 13:25:39 UTC
Created attachment 172107 [details] [review]
Bug 37902: TODOs

There are still different structures we won't handle properly.
This patch adds conditionals to prevent failures or warnings.

Should be done, but later.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 34 Kyle M Hall (khall) 2024-09-27 13:25:42 UTC
Created attachment 172108 [details] [review]
Bug 37902: Adjust timezone

We didn't take into account the server's timezone.

This does not feel right, we are not supposed to deal with time zone
outside of Koha::DateUtils.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 35 Kyle M Hall (khall) 2024-09-27 13:25:44 UTC
Created attachment 172109 [details] [review]
Bug 37902: Do not convert a date if it has like markers

If we pass a datetime parameter we are adding "like" operator and % at
the beginning and ending of the attribute value.
For instance:
  attributes=2024-09-16 10:11:12
  attributes:{ like => '%2024-09-16 10:11:12%' }

We do not want to reach the fixup code and raise an exception.

However I don't think we should add the like for datetime attributes
actually. But can we modify this behaviour now?

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 36 Kyle M Hall (khall) 2024-09-27 13:25:46 UTC
Created attachment 172110 [details] [review]
Bug 37902: Add tests

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 37 Kyle M Hall (khall) 2024-09-27 13:25:48 UTC
Created attachment 172111 [details] [review]
Bug 37902: Apply exact match for datetime

We do not want to apply "like" and do a "contains" search if a correctly
formatted date is passed (ie. starting with "YYYY-MM-DD HH:MM:SS")

It causes underlying problems if we add '%' characters to this string as
it will then become an invalid date.

There are several ways of dealing with this problem. This patch is
suggesting the easiest path: Apply an exact search (ie. do not add '%')
if the value appears to be a datetime.

Certainly not the best looking patch but it seems to be quite effective:
  * no need to change the client
  * no need to rework build_query_params, merge_q_params, attributes_from_api

We could (to confirm) pass the result set, but it seems a lot of additional processing
(that is done later already, in attributes_from_api)

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 38 Kyle M Hall (khall) 2024-09-27 13:25:51 UTC
Created attachment 172112 [details] [review]
Bug 37902: (QA follow-up) Tidy Query.pm & Object.t

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 39 David Cook 2024-09-27 15:58:34 UTC
(In reply to Jonathan Druart from comment #30)
> David, if you are asking what's the next step then have a look at bug 37952.

No, that's not what I meant, but you can ignore my earlier question, as I've done more digging and I think I understand better today than yesterday.

If I understand correctly, Koha::DateTime::Format::RFC3339->parse_datetime() should correctly parse both 2024-09-26T18:41:24.268Z and 2024-09-26T14:45:36-04:00 and set the time_zone appropriately. 

I looked back through your patches and Koha::Object->attributes_from_api() pre-patch, so I better understand what bug you're trying to fix here. I haven't thoroughly reviewed and understood every line of code, but I get the general idea. 

--

I was wondering if there would be any negative unintended consequences of fixing this bug... but we should be OK anywhere that dayjs() with the RFC3339 output or Date().toISOString() is used. 

If no timezone is provided, it's just assumed that the server timezone is used, right? 

--

The % for the -like operator is still going to be a problem, but... I think that's a future problem, which will be part of bug 37952 anyway, as you say Jonathan, so I'll comment more there...
Comment 40 David Cook 2024-09-27 15:58:56 UTC
If it's tl;dr, ignore the above comment and just know that I think it's all good here :)