Skip to content

Commit fb39098

Browse files
authored
Feature property fixes (#1932)
1 parent cafddc8 commit fb39098

5 files changed

Lines changed: 40 additions & 10 deletions

File tree

pygeoapi/provider/csv_.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Authors: Tom Kralidis <tomkralidis@gmail.com>
44
#
5-
# Copyright (c) 2022 Tom Kralidis
5+
# Copyright (c) 2025 Tom Kralidis
66
#
77
# Permission is hereby granted, free of charge, to any person
88
# obtaining a copy of this software and associated documentation
@@ -32,8 +32,9 @@
3232
import itertools
3333
import logging
3434

35-
from pygeoapi.provider.base import (BaseProvider, ProviderQueryError,
36-
ProviderItemNotFoundError)
35+
from pygeoapi.provider.base import (BaseProvider, ProviderInvalidQueryError,
36+
ProviderItemNotFoundError,
37+
ProviderQueryError)
3738
from pygeoapi.util import get_typed_value, crs_transform
3839

3940
LOGGER = logging.getLogger(__name__)
@@ -120,6 +121,12 @@ def _load(self, offset=0, limit=10, resulttype='results',
120121
LOGGER.debug('Serializing DictReader')
121122
data_ = csv.DictReader(ff)
122123
if properties:
124+
for prop in properties:
125+
if prop[0] not in data_.fieldnames:
126+
msg = 'Invalid fieldname'
127+
LOGGER.error(msg)
128+
raise ProviderInvalidQueryError(msg)
129+
123130
data_ = filter(
124131
lambda p: all(
125132
[p[prop[0]] == prop[1] for prop in properties]), data_)
@@ -139,6 +146,7 @@ def _load(self, offset=0, limit=10, resulttype='results',
139146
msg = f'Skipping row with invalid geometry: {row.get(self.id_field)}' # noqa
140147
LOGGER.error(msg)
141148
continue
149+
142150
feature = {'type': 'Feature'}
143151
feature['id'] = row.pop(self.id_field)
144152
if not skip_geometry:

pygeoapi/provider/tinydb_.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Authors: Tom Kralidis <tomkralidis@gmail.com>
44
#
5-
# Copyright (c) 2024 Tom Kralidis
5+
# Copyright (c) 2025 Tom Kralidis
66
#
77
# Permission is hereby granted, free of charge, to any person
88
# obtaining a copy of this software and associated documentation
@@ -37,6 +37,7 @@
3737
from tinydb import TinyDB, Query, where
3838

3939
from pygeoapi.provider.base import (BaseProvider, ProviderConnectionError,
40+
ProviderInvalidQueryError,
4041
ProviderItemNotFoundError)
4142
from pygeoapi.util import crs_transform, get_typed_value
4243

@@ -197,7 +198,12 @@ def query(self, offset=0, limit=10, resulttype='results',
197198
LOGGER.debug('querying database')
198199
if len(QUERY) > 0:
199200
LOGGER.debug(f'running eval on {SEARCH_STRING}')
200-
results = eval(SEARCH_STRING)
201+
try:
202+
results = eval(SEARCH_STRING)
203+
except SyntaxError as err:
204+
msg = 'Invalid query'
205+
LOGGER.error(f'{msg}: {err}')
206+
raise ProviderInvalidQueryError(msg)
201207
else:
202208
results = self.db.all()
203209

pygeoapi/templates/exception.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
{% block body %}
44
<section id="exception">
55
<h2>{% trans %}Exception{% endtrans %}</h2>
6-
<p>{{ data['description'] }}</p>
6+
<p>{{ data['description'] | striptags }}</p>
77
</section>
88
{% endblock %}

tests/test_csv__provider.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Authors: Tom Kralidis <tomkralidis@gmail.com>
44
#
5-
# Copyright (c) 2021 Tom Kralidis
5+
# Copyright (c) 2025 Tom Kralidis
66
#
77
# Permission is hereby granted, free of charge, to any person
88
# obtaining a copy of this software and associated documentation
@@ -29,7 +29,8 @@
2929

3030
import pytest
3131

32-
from pygeoapi.provider.base import ProviderItemNotFoundError
32+
from pygeoapi.provider.base import (ProviderItemNotFoundError,
33+
ProviderInvalidQueryError)
3334
from pygeoapi.provider.csv_ import CSVProvider
3435

3536
from .util import get_test_file_path
@@ -117,6 +118,13 @@ def test_query(config):
117118
assert len(results['features'][0]['properties']) == 2
118119

119120

121+
def test_get_invalid_property(config):
122+
"""Testing query for an invalid property name"""
123+
p = CSVProvider(config)
124+
with pytest.raises(ProviderInvalidQueryError):
125+
p.query(properties=[('foo', 'bar')])
126+
127+
120128
def test_get(config):
121129
p = CSVProvider(config)
122130

tests/test_tinydb_provider.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Authors: Tom Kralidis <tomkralidis@gmail.com>
44
#
5-
# Copyright (c) 2024 Tom Kralidis
5+
# Copyright (c) 2025 Tom Kralidis
66
#
77
# Permission is hereby granted, free of charge, to any person
88
# obtaining a copy of this software and associated documentation
@@ -32,7 +32,8 @@
3232

3333
import pytest
3434

35-
from pygeoapi.provider.base import ProviderItemNotFoundError
35+
from pygeoapi.provider.base import (ProviderItemNotFoundError,
36+
ProviderInvalidQueryError)
3637
from pygeoapi.provider.tinydb_ import TinyDBProvider
3738

3839
from .util import get_test_file_path
@@ -156,6 +157,13 @@ def test_query(config):
156157
assert results['features'][0]['id'] == '02HC003.2017-05-27'
157158

158159

160+
def test_get_invalid_property(config):
161+
"""Testing query for an invalid property name"""
162+
p = TinyDBProvider(config)
163+
with pytest.raises(ProviderInvalidQueryError):
164+
p.query(properties=[('\'foo', 'bar')])
165+
166+
159167
def test_get(config):
160168
p = TinyDBProvider(config)
161169

0 commit comments

Comments
 (0)