Skip to content

Commit 13e07af

Browse files
committed
Fix automatic converstion of float values to int
1 parent 0085835 commit 13e07af

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
### Fixed
1313

1414
- Corrected the closing of client connections in ES index management functions [#132](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/132)
15+
- Corrected the automatic converstion of float values to int when building Filter Clauses [#135](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/135)
1516

1617
## [v0.3.0]
1718

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/extensions/filter.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ def to_es(self):
114114
return self.date.isoformat()
115115

116116

117+
class FloatInt(float):
118+
"""Representation of Float/Int."""
119+
@classmethod
120+
def __get_validators__(cls):
121+
"""Return validator to use."""
122+
yield cls.validate
123+
124+
@classmethod
125+
def validate(cls, v):
126+
"""Validate input value."""
127+
if isinstance(v, float):
128+
return v
129+
else:
130+
return int(v)
131+
132+
117133
Arg = Union[
118134
"Clause",
119135
PropertyReference,
@@ -126,8 +142,7 @@ def to_es(self):
126142
Polygon,
127143
MultiPolygon,
128144
GeometryCollection,
129-
int,
130-
float,
145+
FloatInt,
131146
str,
132147
bool,
133148
]

stac_fastapi/elasticsearch/tests/extensions/test_filter.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,34 @@ async def test_search_filter_ext_and(app_client, ctx):
7878

7979
assert resp.status_code == 200
8080
assert len(resp.json()["features"]) == 1
81+
82+
83+
async def test_search_filter_extension_floats(app_client, ctx):
84+
sun_elevation = ctx.item["properties"]["view:sun_elevation"]
85+
86+
params = {
87+
"filter": {
88+
"op": "and",
89+
"args": [
90+
{"op": "=", "args": [{"property": "id"}, ctx.item["id"]]},
91+
{
92+
"op": ">",
93+
"args": [
94+
{"property": "properties.view:sun_elevation"},
95+
sun_elevation - 0.01,
96+
],
97+
},
98+
{
99+
"op": "<",
100+
"args": [
101+
{"property": "properties.view:sun_elevation"},
102+
sun_elevation + 0.01,
103+
],
104+
},
105+
],
106+
}
107+
}
108+
resp = await app_client.post("/search", json=params)
109+
110+
assert resp.status_code == 200
111+
assert len(resp.json()["features"]) == 1

0 commit comments

Comments
 (0)