Skip to content

Commit

Permalink
Make Feature.geometry optional (#179)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
janusw authored Jul 8, 2024
1 parent 36be431 commit dc8ca43
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
linuxBuild:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup .NET Core
Expand All @@ -32,7 +32,7 @@ jobs:
winBuild:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup .NET Core
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ node_modules
*.csproj.user
/TestResults
.vscode
.mono
/GoCompare.CustomerPreference/CustomerPreferenceService/Service.xml
/GoCompare.CustomerPreference/CustomerPreferenceService/Properties/PublishProfiles/CustomerPreferenceService - Web Deploy.pubxml
/GoCompare.CustomerPreference/CustomerPreferenceService/Properties/PublishProfiles/CustomerPreferenceService - Web Deploy.pubxml.user
Expand Down
32 changes: 32 additions & 0 deletions src/GeoJSON.Net.Tests/Feature/FeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,38 @@ public void Can_Deserialize_Feature_Without_Props()
Assert.AreEqual(GeoJSONObjectType.Polygon, feature.Geometry.Type);
}

[Test]
public void Can_Deserialize_Feature_With_Null_Geometry()
{
var json = GetExpectedJson();

var feature = JsonConvert.DeserializeObject<Net.Feature.Feature>(json);

Assert.IsNotNull(feature);
Assert.IsNotNull(feature.Properties);
Assert.IsTrue(feature.Properties.Any());
Assert.IsTrue(feature.Properties.ContainsKey("name"));
Assert.AreEqual(feature.Properties["name"], "Unlocalized Feature");

Assert.IsNull(feature.Geometry);
}

[Test]
public void Can_Deserialize_Feature_Without_Geometry()
{
var json = GetExpectedJson();

var feature = JsonConvert.DeserializeObject<Net.Feature.Feature>(json);

Assert.IsNotNull(feature);
Assert.IsNotNull(feature.Properties);
Assert.IsTrue(feature.Properties.Any());
Assert.IsTrue(feature.Properties.ContainsKey("name"));
Assert.AreEqual(feature.Properties["name"], "Unlocalized Feature");

Assert.IsNull(feature.Geometry);
}

[Test]
public void Can_Serialize_LineString_Feature()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "Feature",
"geometry": null,
"properties": {
"name": "Unlocalized Feature"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "Feature",
"properties": {
"name": "Unlocalized Feature"
}
}
2 changes: 1 addition & 1 deletion src/GeoJSON.Net/Feature/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Feature(TGeometry geometry, TProps properties, string id = null)
[JsonProperty(PropertyName = "id", NullValueHandling = NullValueHandling.Ignore)]
public string Id { get; }

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

Expand Down

0 comments on commit dc8ca43

Please sign in to comment.