Make sure that PostgreSQLProvider.get_fields returns valid json schem… (#1312)

* Make sure that PostgreSQLProvider.get_fields returns valid json schema types

* Log warnings when postgres provider encounters unknown types

* Update postgresql.py

---------

Co-authored-by: Tom Kralidis <tomkralidis@gmail.com>
This commit is contained in:
totycro
2023-07-04 13:44:17 +02:00
committed by GitHub
parent fe0d0ed0bc
commit dda4d121e2
3 changed files with 44 additions and 18 deletions
+3 -1
View File
@@ -82,7 +82,9 @@ class BaseProvider:
"""
Get provider field information (names, types)
:returns: dict of fields
Example response: {'field1': 'string', 'field2': 'number'}}
:returns: dict of field names and their associated JSON Schema typess
"""
raise NotImplementedError()
+29 -5
View File
@@ -187,13 +187,37 @@ class PostgreSQLProvider(BaseProvider):
"""
LOGGER.debug('Get available fields/properties')
fields = {}
for column in self.table_model.__table__.columns:
fields[str(column.name)] = {'type': str(column.type)}
# sql-schema only allows these types, so we need to map from sqlalchemy
# string, number, integer, object, array, boolean, null,
# https://json-schema.org/understanding-json-schema/reference/type.html
column_type_map = {
str: 'string',
float: 'number',
int: 'integer',
bool: 'boolean',
}
default_value = 'string'
fields.pop(self.geom) # Exclude geometry column
def _column_type_to_json_schema_type(column_type):
try:
python_type = column_type.python_type
except NotImplementedError:
LOGGER.warning(f'Unsupported column type {column_type}')
return default_value
else:
try:
return column_type_map[python_type]
except KeyError:
LOGGER.warning(f'Unsupported column type {column_type}')
return default_value
return fields
return {
str(column.name): {
'type': _column_type_to_json_schema_type(column.type)
}
for column in self.table_model.__table__.columns
if column.name != self.geom # Exclude geometry column
}
def get(self, identifier, crs_transform_spec=None, **kwargs):
"""
+12 -12
View File
@@ -308,18 +308,18 @@ def test_query_cql_properties_bbox_filters(config):
def test_get_fields(config):
# Arrange
expected_fields = {
'blockage': {'type': 'VARCHAR(80)'},
'covered': {'type': 'VARCHAR(80)'},
'depth': {'type': 'VARCHAR(80)'},
'layer': {'type': 'VARCHAR(80)'},
'name': {'type': 'VARCHAR(80)'},
'natural': {'type': 'VARCHAR(80)'},
'osm_id': {'type': 'INTEGER'},
'tunnel': {'type': 'VARCHAR(80)'},
'water': {'type': 'VARCHAR(80)'},
'waterway': {'type': 'VARCHAR(80)'},
'width': {'type': 'VARCHAR(80)'},
'z_index': {'type': 'VARCHAR(80)'}
'blockage': {'type': 'string'},
'covered': {'type': 'string'},
'depth': {'type': 'string'},
'layer': {'type': 'string'},
'name': {'type': 'string'},
'natural': {'type': 'string'},
'osm_id': {'type': 'integer'},
'tunnel': {'type': 'string'},
'water': {'type': 'string'},
'waterway': {'type': 'string'},
'width': {'type': 'string'},
'z_index': {'type': 'string'}
}
# Act