From 083c7c721be971b0781b630fd712b0388268665e Mon Sep 17 00:00:00 2001 From: Matthias Schaub Date: Thu, 26 Sep 2024 02:42:19 +0200 Subject: [PATCH] feat(api): use `geojson-pydantic` as request model use Pydantic model from the `geojson-pydantic` library as request model for `bpolys`. Closes #704 (Closes https://github.com/GIScience/ohsome-quality-api/pull/704) --- CHANGELOG.md | 4 ++-- ohsome_quality_api/api/request_models.py | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7809533a0..4ded02c7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,15 +6,15 @@ - report endpoint: has been deleted ([#818]) - ### Other Changes - a new regression test suite has been added to support safer deployments of new versions ([#820]) - +- use Pydantic model from the `geojson-pydantic` library as request model for `bpolys` ([#824]) [#818]: https://github.com/GIScience/ohsome-quality-api/pull/818 [#820]: https://github.com/GIScience/ohsome-quality-api/issues/820 +[#824]: https://github.com/GIScience/ohsome-quality-api/issues/824 ## Release 1.4.0 diff --git a/ohsome_quality_api/api/request_models.py b/ohsome_quality_api/api/request_models.py index c6977e78b..ec3690387 100644 --- a/ohsome_quality_api/api/request_models.py +++ b/ohsome_quality_api/api/request_models.py @@ -1,14 +1,13 @@ -import json +from typing import Dict import geojson -from geojson import FeatureCollection +from geojson_pydantic import Feature, FeatureCollection, MultiPolygon, Polygon from pydantic import BaseModel, ConfigDict, Field, field_validator from ohsome_quality_api.attributes.definitions import AttributeEnum from ohsome_quality_api.topics.definitions import TopicEnum from ohsome_quality_api.topics.models import TopicData from ohsome_quality_api.utils.helper import snake_to_lower_camel -from ohsome_quality_api.utils.validators import validate_geojson class BaseConfig(BaseModel): @@ -20,8 +19,11 @@ class BaseConfig(BaseModel): ) +FeatureCollection_ = FeatureCollection[Feature[Polygon | MultiPolygon, Dict]] + + class BaseBpolys(BaseConfig): - bpolys: dict = Field( + bpolys: FeatureCollection_ = Field( { "type": "FeatureCollection", "features": [ @@ -46,12 +48,11 @@ class BaseBpolys(BaseConfig): @field_validator("bpolys") @classmethod - def validate_bpolys(cls, value) -> FeatureCollection: - obj = geojson.loads(json.dumps(value)) - if not isinstance(obj, FeatureCollection): - raise ValueError("must be of type FeatureCollection") - validate_geojson(obj) # Check if exceptions are raised - return obj + def transform(cls, value) -> geojson.FeatureCollection: + # NOTE: `geojson_pydantic` library is used only for validation and openAPI-spec + # generation. To avoid refactoring all code the FeatureCollection object of + # the `geojson` library is still used every else. + return geojson.loads(value.model_dump_json()) class IndicatorRequest(BaseBpolys):