-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support [DataMember] IsRequired in NewtonsoftDataContractResolver (#2644
) * ✅ Add non-regression tests for DataMember required As DataMember implementation comes after the implementation for [JsonProperty] and [JsonObject], we want to be sure that the current behavior is not bypassed. * ✨Add support for [DataMember(IsRequired)] Add test cases for DataMember (IsRequired and Name). It seems that NewtonSoft's ResolveContract correctly set the Required, using [DataMember], [JsonProperpty] and [JsonRequired] so we can directly use it instead of the IsRequiredSpecifed extension method. * ✨ Keep IsRequiredSpecified() but use JsonProperty.Required internally Newtonsoft do all the work for us, handling [JsonProperty(Required = Required.Always)] or [JsonRequired] or [DataMember(IsRequired = true)] and filling the JsonProperty.Required field.
- Loading branch information
1 parent
8f9c079
commit 2c5ec58
Showing
6 changed files
with
92 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
test/Swashbuckle.AspNetCore.Newtonsoft.Test/Fixtures/DataMemberAnnotatedType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace Swashbuckle.AspNetCore.Newtonsoft.Test | ||
{ | ||
[DataContract(Name = "CustomNameFromDataContract")] | ||
public class DataMemberAnnotatedType | ||
{ | ||
[DataMember(IsRequired = true)] | ||
public string StringWithDataMemberRequired { get; set; } | ||
|
||
[DataMember(IsRequired = false)] | ||
public string StringWithDataMemberNonRequired { get; set; } | ||
|
||
[DataMember(IsRequired = true, Name = "RequiredWithCustomNameFromDataMember")] | ||
public string StringWithDataMemberRequiredWithCustomName { get; set; } | ||
|
||
[DataMember(IsRequired = false, Name = "NonRequiredWithCustomNameFromDataMember")] | ||
public string StringWithDataMemberNonRequiredWithCustomName { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
test/Swashbuckle.AspNetCore.Newtonsoft.Test/Fixtures/JsonRequiredAnnotatedType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters