Skip to content

Commit dc8ca43

Browse files
authored
Make Feature.geometry optional (#179)
* make Feature.geometry optional * add two test cases * deserializing a feature with NULL geometry * deserializing a feature without any geometry * the resulting feature object is identical for both cases * amend .gitignore * GHA: fix some deprecation warnings
1 parent 36be431 commit dc8ca43

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
linuxBuild:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v2
16+
- uses: actions/checkout@v4
1717
with:
1818
fetch-depth: 0
1919
- name: Setup .NET Core
@@ -32,7 +32,7 @@ jobs:
3232
winBuild:
3333
runs-on: windows-latest
3434
steps:
35-
- uses: actions/checkout@v2
35+
- uses: actions/checkout@v4
3636
with:
3737
fetch-depth: 0
3838
- name: Setup .NET Core

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ node_modules
88
*.csproj.user
99
/TestResults
1010
.vscode
11+
.mono
1112
/GoCompare.CustomerPreference/CustomerPreferenceService/Service.xml
1213
/GoCompare.CustomerPreference/CustomerPreferenceService/Properties/PublishProfiles/CustomerPreferenceService - Web Deploy.pubxml
1314
/GoCompare.CustomerPreference/CustomerPreferenceService/Properties/PublishProfiles/CustomerPreferenceService - Web Deploy.pubxml.user

src/GeoJSON.Net.Tests/Feature/FeatureTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ public void Can_Deserialize_Feature_Without_Props()
4343
Assert.AreEqual(GeoJSONObjectType.Polygon, feature.Geometry.Type);
4444
}
4545

46+
[Test]
47+
public void Can_Deserialize_Feature_With_Null_Geometry()
48+
{
49+
var json = GetExpectedJson();
50+
51+
var feature = JsonConvert.DeserializeObject<Net.Feature.Feature>(json);
52+
53+
Assert.IsNotNull(feature);
54+
Assert.IsNotNull(feature.Properties);
55+
Assert.IsTrue(feature.Properties.Any());
56+
Assert.IsTrue(feature.Properties.ContainsKey("name"));
57+
Assert.AreEqual(feature.Properties["name"], "Unlocalized Feature");
58+
59+
Assert.IsNull(feature.Geometry);
60+
}
61+
62+
[Test]
63+
public void Can_Deserialize_Feature_Without_Geometry()
64+
{
65+
var json = GetExpectedJson();
66+
67+
var feature = JsonConvert.DeserializeObject<Net.Feature.Feature>(json);
68+
69+
Assert.IsNotNull(feature);
70+
Assert.IsNotNull(feature.Properties);
71+
Assert.IsTrue(feature.Properties.Any());
72+
Assert.IsTrue(feature.Properties.ContainsKey("name"));
73+
Assert.AreEqual(feature.Properties["name"], "Unlocalized Feature");
74+
75+
Assert.IsNull(feature.Geometry);
76+
}
77+
4678
[Test]
4779
public void Can_Serialize_LineString_Feature()
4880
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "Feature",
3+
"geometry": null,
4+
"properties": {
5+
"name": "Unlocalized Feature"
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "Feature",
3+
"properties": {
4+
"name": "Unlocalized Feature"
5+
}
6+
}

src/GeoJSON.Net/Feature/Feature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Feature(TGeometry geometry, TProps properties, string id = null)
3535
[JsonProperty(PropertyName = "id", NullValueHandling = NullValueHandling.Ignore)]
3636
public string Id { get; }
3737

38-
[JsonProperty(PropertyName = "geometry", Required = Required.AllowNull)]
38+
[JsonProperty(PropertyName = "geometry", Required = Required.Default)]
3939
[JsonConverter(typeof(GeometryConverter))]
4040
public TGeometry Geometry { get; }
4141

0 commit comments

Comments
 (0)