From 2b47e1937433e8be55e936837a7ea995149eab27 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Thu, 18 Apr 2024 18:55:33 +0800 Subject: [PATCH 1/3] fix datetime validator --- CHANGES.md | 4 ++++ stac_fastapi/types/stac_fastapi/types/rfc3339.py | 14 ++++++++++---- stac_fastapi/types/tests/test_rfc3339.py | 4 ++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6bf2c50b5..00552bf87 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Fixed + +* BaseSearchGetRequest datetime validator str_to_interval not allowing GET /search requests with datetime = None ([#661](https://github.com/stac-utils/stac-fastapi/pull/661)) + ## [2.5.1] - 2024-04-18 ### Fixed diff --git a/stac_fastapi/types/stac_fastapi/types/rfc3339.py b/stac_fastapi/types/stac_fastapi/types/rfc3339.py index 43baa8d53..e5a3cd7fb 100644 --- a/stac_fastapi/types/stac_fastapi/types/rfc3339.py +++ b/stac_fastapi/types/stac_fastapi/types/rfc3339.py @@ -46,7 +46,7 @@ def rfc3339_str_to_datetime(s: str) -> datetime: def str_to_interval( - interval: str, + interval: Optional[str] ) -> Optional[DateTimeType]: """Extract a tuple of datetimes from an interval string. @@ -56,12 +56,18 @@ def str_to_interval( or end (but not both) to be open-ended with '..' or ''. Args: - interval (str) : The interval string to convert to a :class:`datetime.datetime` - tuple. + interval (str or None): The interval string to convert to a tuple of datetime.datetime + objects, or None if no datetime is specified. + + Returns: + Optional[DateTimeType]: A tuple of datetime.datetime objects or None if input is None. Raises: - ValueError: If the string is not a valid interval string. + ValueError: If the string is not a valid interval string and not None. """ + if interval is None: + return None + if not interval: raise ValueError("Empty interval string is invalid.") diff --git a/stac_fastapi/types/tests/test_rfc3339.py b/stac_fastapi/types/tests/test_rfc3339.py index 0a402699a..9d86b6ff6 100644 --- a/stac_fastapi/types/tests/test_rfc3339.py +++ b/stac_fastapi/types/tests/test_rfc3339.py @@ -103,3 +103,7 @@ def test_now_functions() -> None: assert now1.tzinfo == timezone.utc rfc3339_str_to_datetime(now_to_rfc3339_str()) + +def test_str_to_interval_with_none(): + """Test that str_to_interval returns None when provided with None.""" + assert str_to_interval(None) is None, "str_to_interval should return None when input is None" \ No newline at end of file From 5a00118cb9e99376b6c6d87ff715e7856b8026d3 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Thu, 18 Apr 2024 18:57:38 +0800 Subject: [PATCH 2/3] lint code --- stac_fastapi/types/stac_fastapi/types/rfc3339.py | 13 ++++++------- stac_fastapi/types/tests/test_rfc3339.py | 5 ++++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/stac_fastapi/types/stac_fastapi/types/rfc3339.py b/stac_fastapi/types/stac_fastapi/types/rfc3339.py index e5a3cd7fb..b1d40999e 100644 --- a/stac_fastapi/types/stac_fastapi/types/rfc3339.py +++ b/stac_fastapi/types/stac_fastapi/types/rfc3339.py @@ -45,9 +45,7 @@ def rfc3339_str_to_datetime(s: str) -> datetime: return iso8601.parse_date(s) -def str_to_interval( - interval: Optional[str] -) -> Optional[DateTimeType]: +def str_to_interval(interval: Optional[str]) -> Optional[DateTimeType]: """Extract a tuple of datetimes from an interval string. Interval strings are defined by @@ -56,18 +54,19 @@ def str_to_interval( or end (but not both) to be open-ended with '..' or ''. Args: - interval (str or None): The interval string to convert to a tuple of datetime.datetime - objects, or None if no datetime is specified. + interval (str or None): The interval string to convert to a tuple of + datetime.datetime objects, or None if no datetime is specified. Returns: - Optional[DateTimeType]: A tuple of datetime.datetime objects or None if input is None. + Optional[DateTimeType]: A tuple of datetime.datetime objects or None if + input is None. Raises: ValueError: If the string is not a valid interval string and not None. """ if interval is None: return None - + if not interval: raise ValueError("Empty interval string is invalid.") diff --git a/stac_fastapi/types/tests/test_rfc3339.py b/stac_fastapi/types/tests/test_rfc3339.py index 9d86b6ff6..23f6242bc 100644 --- a/stac_fastapi/types/tests/test_rfc3339.py +++ b/stac_fastapi/types/tests/test_rfc3339.py @@ -104,6 +104,9 @@ def test_now_functions() -> None: rfc3339_str_to_datetime(now_to_rfc3339_str()) + def test_str_to_interval_with_none(): """Test that str_to_interval returns None when provided with None.""" - assert str_to_interval(None) is None, "str_to_interval should return None when input is None" \ No newline at end of file + assert ( + str_to_interval(None) is None + ), "str_to_interval should return None when input is None" From 9560b1fbf6354f118965624532faa9d23ffa6b2d Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Thu, 18 Apr 2024 18:59:32 +0800 Subject: [PATCH 3/3] fix pull request # --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 00552bf87..e3a91da62 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,7 +4,7 @@ ### Fixed -* BaseSearchGetRequest datetime validator str_to_interval not allowing GET /search requests with datetime = None ([#661](https://github.com/stac-utils/stac-fastapi/pull/661)) +* BaseSearchGetRequest datetime validator str_to_interval not allowing GET /search requests with datetime = None ([#662](https://github.com/stac-utils/stac-fastapi/pull/662)) ## [2.5.1] - 2024-04-18