Skip to content

Commit 9864544

Browse files
Merge pull request #91 from developmentseed/IdAsStringOrNumber
allow Feature **id** to be either a String or a Number
2 parents 21001df + 567125c commit 9864544

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3939
- Do not validates arbitrary dictionaries. Make `Type` a mandatory key for objects (https://github.com/developmentseed/geojson-pydantic/pull/94)
4040
- Add Geometry discriminator when parsing geometry objects (author @eseglem, https://github.com/developmentseed/geojson-pydantic/pull/101)
4141
- Mixed Dimensionality WKTs (make sure the coordinates are either all 2D or 3D) (author @eseglem, https://github.com/developmentseed/geojson-pydantic/pull/107)
42+
- allow Feature's **id** to be either a String or a Number
4243

4344
### Removed
4445

geojson_pydantic/features.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import Any, Dict, Generic, Iterator, List, Literal, Optional, TypeVar, Union
44

5-
from pydantic import BaseModel, Field, validator
5+
from pydantic import BaseModel, Field, StrictInt, StrictStr, validator
66
from pydantic.generics import GenericModel
77

88
from geojson_pydantic.geo_interface import GeoInterfaceMixin
@@ -19,7 +19,7 @@ class Feature(GenericModel, Generic[Geom, Props], GeoInterfaceMixin):
1919
type: Literal["Feature"]
2020
geometry: Union[Geom, None] = Field(...)
2121
properties: Union[Props, None] = Field(...)
22-
id: Optional[str] = None
22+
id: Optional[Union[StrictInt, StrictStr]] = None
2323
bbox: Optional[BBox] = None
2424

2525
class Config:

tests/test_features.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,20 @@ def test_feature_collection_geo_interface_with_null_geometry():
190190
assert "bbox" in fc.__geo_interface__["features"][1]
191191

192192

193+
@pytest.mark.parametrize("id", ["a", 1, "1"])
194+
def test_feature_id(id):
195+
"""Test if a string stays a string and if an int stays an int."""
196+
feature = Feature(**test_feature, id=id)
197+
assert feature.id == id
198+
199+
200+
@pytest.mark.parametrize("id", [True, 1.0])
201+
def test_bad_feature_id(id):
202+
"""make sure it raises error."""
203+
with pytest.raises(ValidationError):
204+
Feature(**test_feature, id=id)
205+
206+
193207
def test_feature_validation():
194208
"""Test default."""
195209
assert Feature(type="Feature", properties=None, geometry=None)

0 commit comments

Comments
 (0)