Skip to content

Commit 68bcef7

Browse files
feat(api): use geojson-pydantic as request model
use Pydantic model from the `geojson-pydantic` library as request model for `bpolys`. Closes #704 (Closes #704)
1 parent 5b6135a commit 68bcef7

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

ohsome_quality_api/api/request_models.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import json
1+
from typing import Dict
22

33
import geojson
4-
from geojson import FeatureCollection
4+
from geojson_pydantic import Feature, FeatureCollection, MultiPolygon, Polygon
55
from pydantic import BaseModel, ConfigDict, Field, field_validator
66

77
from ohsome_quality_api.attributes.definitions import AttributeEnum
88
from ohsome_quality_api.topics.definitions import TopicEnum
99
from ohsome_quality_api.topics.models import TopicData
1010
from ohsome_quality_api.utils.helper import snake_to_lower_camel
11-
from ohsome_quality_api.utils.validators import validate_geojson
1211

1312

1413
class BaseConfig(BaseModel):
@@ -20,8 +19,11 @@ class BaseConfig(BaseModel):
2019
)
2120

2221

22+
FeatureCollection_ = FeatureCollection[Feature[Polygon | MultiPolygon, Dict]]
23+
24+
2325
class BaseBpolys(BaseConfig):
24-
bpolys: dict = Field(
26+
bpolys: FeatureCollection_ = Field(
2527
{
2628
"type": "FeatureCollection",
2729
"features": [
@@ -46,12 +48,11 @@ class BaseBpolys(BaseConfig):
4648

4749
@field_validator("bpolys")
4850
@classmethod
49-
def validate_bpolys(cls, value) -> FeatureCollection:
50-
obj = geojson.loads(json.dumps(value))
51-
if not isinstance(obj, FeatureCollection):
52-
raise ValueError("must be of type FeatureCollection")
53-
validate_geojson(obj) # Check if exceptions are raised
54-
return obj
51+
def transform(cls, value) -> geojson.FeatureCollection:
52+
# NOTE: `geojson_pydantic` library is used only for validation and openAPI-spec
53+
# generation. To avoid refactoring all code the FeatureCollection object of
54+
# the `geojson` library is still used every else.
55+
return geojson.loads(value.model_dump_json())
5556

5657

5758
class IndicatorRequest(BaseBpolys):

0 commit comments

Comments
 (0)