Skip to content

Commit 083c7c7

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 083c7c7

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
- report endpoint: has been deleted ([#818])
88

9-
109
### Other Changes
1110

1211
- a new regression test suite has been added to support safer deployments of new versions ([#820])
13-
12+
- use Pydantic model from the `geojson-pydantic` library as request model for `bpolys` ([#824])
1413

1514

1615
[#818]: https://github.com/GIScience/ohsome-quality-api/pull/818
1716
[#820]: https://github.com/GIScience/ohsome-quality-api/issues/820
17+
[#824]: https://github.com/GIScience/ohsome-quality-api/issues/824
1818

1919

2020
## Release 1.4.0

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)