Skip to content

Commit

Permalink
✅ Add non-regression tests for DataMember required
Browse files Browse the repository at this point in the history
As DataMember implementation comes after the implementation for [JsonProperty] and [JsonObject], we want to be sure that the current behavior is not bypassed.
  • Loading branch information
ouvreboite authored and jb.muscat committed Aug 1, 2024
1 parent 531c4c0 commit c6a4075
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Newtonsoft.Json;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace Swashbuckle.AspNetCore.Newtonsoft.Test
{
[JsonObject(ItemRequired = Required.Always)]
[DataContract]
public class JsonObjectAnnotatedType
{
public string StringWithNoAnnotation { get; set; }
Expand All @@ -12,5 +14,8 @@ public class JsonObjectAnnotatedType

[JsonProperty(Required = Required.AllowNull)]
public string StringWithRequiredAllowNull { get; set; }

[DataMember(IsRequired = false)]
public string StringWithDataMemberRequiredFalse { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Newtonsoft.Json;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace Swashbuckle.AspNetCore.Newtonsoft.Test
{
[DataContract]
public class JsonPropertyAnnotatedType
{
[JsonProperty("string-with-json-property-name")]
Expand All @@ -21,5 +23,13 @@ public class JsonPropertyAnnotatedType

[JsonProperty(Required = Required.AllowNull)]
public string StringWithRequiredAllowNull { get; set; }

[DataMember(IsRequired = false)] //As the support for DataMember has been implemented later, JsonRequired should take precedence
[JsonProperty(Required = Required.Always)]
public string StringWithRequiredAlwaysButConflictingDataMember { get; set; }

[DataMember(IsRequired = true)] //As the support for DataMember has been implemented later, JsonRequired should take precedence
[JsonProperty(Required = Required.Default)]
public string StringWithRequiredDefaultButConflictingDataMember { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using Newtonsoft.Json;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace Swashbuckle.AspNetCore.Newtonsoft.Test
{
[DataContract]
public class JsonRequiredAnnotatedType
{
[JsonRequired]
public string StringWithJsonRequired { get; set; }

[DataMember(IsRequired = false)] //As the support for DataMember has been implemented later, JsonRequired should take precedence
[JsonRequired]
public string StringWithConflictingRequired { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -755,14 +755,17 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonProperty()
"StringWithRequiredDisallowNull",
"StringWithRequiredAlways",
"StringWithRequiredAllowNull",
"StringWithRequiredAlwaysButConflictingDataMember",
"StringWithRequiredDefaultButConflictingDataMember"
},
schema.Properties.Keys.ToArray()
);
Assert.Equal(
new[]
{
"StringWithRequiredAllowNull",
"StringWithRequiredAlways"
"StringWithRequiredAlways",
"StringWithRequiredAlwaysButConflictingDataMember"
},
schema.Required.ToArray()
);
Expand All @@ -772,6 +775,8 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonProperty()
Assert.False(schema.Properties["StringWithRequiredDisallowNull"].Nullable);
Assert.False(schema.Properties["StringWithRequiredAlways"].Nullable);
Assert.True(schema.Properties["StringWithRequiredAllowNull"].Nullable);
Assert.False(schema.Properties["StringWithRequiredAlwaysButConflictingDataMember"].Nullable);
Assert.True(schema.Properties["StringWithRequiredDefaultButConflictingDataMember"].Nullable);
}

[Fact]
Expand All @@ -782,7 +787,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonRequired()
var referenceSchema = Subject().GenerateSchema(typeof(JsonRequiredAnnotatedType), schemaRepository);

var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
Assert.Equal(new[] { "StringWithJsonRequired" }, schema.Required.ToArray());
Assert.Equal(new[] { "StringWithConflictingRequired", "StringWithJsonRequired"}, schema.Required.ToArray());
Assert.False(schema.Properties["StringWithJsonRequired"].Nullable);
}

Expand All @@ -797,6 +802,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonObject()
Assert.Equal(
new[]
{
"StringWithDataMemberRequiredFalse",
"StringWithNoAnnotation",
"StringWithRequiredAllowNull",
"StringWithRequiredUnspecified"
Expand All @@ -806,6 +812,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonObject()
Assert.False(schema.Properties["StringWithNoAnnotation"].Nullable);
Assert.False(schema.Properties["StringWithRequiredUnspecified"].Nullable);
Assert.True(schema.Properties["StringWithRequiredAllowNull"].Nullable);
Assert.False(schema.Properties["StringWithDataMemberRequiredFalse"].Nullable);
}

[Fact]
Expand Down

0 comments on commit c6a4075

Please sign in to comment.