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 datetime validator #662

Merged
merged 3 commits into from
Apr 18, 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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

* 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

### Fixed
Expand Down
17 changes: 11 additions & 6 deletions stac_fastapi/types/stac_fastapi/types/rfc3339.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def rfc3339_str_to_datetime(s: str) -> datetime:
return iso8601.parse_date(s)


def str_to_interval(
interval: 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
Expand All @@ -56,12 +54,19 @@ 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.")

Expand Down
7 changes: 7 additions & 0 deletions stac_fastapi/types/tests/test_rfc3339.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,10 @@ 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"