Skip to content

Commit

Permalink
✨Add support for [DataMember(IsRequired)]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ouvreboite authored and jb.muscat committed Aug 1, 2024
1 parent 3d17511 commit 5ec8ff2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,5 @@ public static bool TryGetMemberInfo(this JsonProperty jsonProperty, out MemberIn

return (memberInfo != null);
}

public static bool IsRequiredSpecified(this JsonProperty jsonProperty)
{
if (!jsonProperty.TryGetMemberInfo(out MemberInfo memberInfo))
return false;

if (memberInfo.GetCustomAttribute<JsonRequiredAttribute>() != null)
return true;

var jsonPropertyAttributeData = memberInfo.GetCustomAttributesData()
.FirstOrDefault(attrData => attrData.AttributeType == typeof(JsonPropertyAttribute));

return (jsonPropertyAttributeData != null) && jsonPropertyAttributeData.NamedArguments.Any(arg => arg.MemberName == "Required");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private List<DataProperty> GetDataPropertiesFor(JsonObjectContract jsonObjectCon
{
if (jsonProperty.Ignored) continue;

var required = jsonProperty.IsRequiredSpecified()
var required = jsonProperty.Required != Required.Default
? jsonProperty.Required
: jsonObjectContract.ItemRequired ?? Required.Default;

Expand Down
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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,43 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonExtensionData()
Assert.Null(schema.AdditionalProperties.Type);
}

[Fact]
public void GenerateSchema_HonorsDataMemberAttribute()
{
var schemaRepository = new SchemaRepository();

var referenceSchema = Subject().GenerateSchema(typeof(DataMemberAnnotatedType), schemaRepository);

var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];


Assert.True(schema.Properties["StringWithDataMemberRequired"].Nullable);
Assert.True(schema.Properties["StringWithDataMemberNonRequired"].Nullable);
Assert.True(schema.Properties["RequiredWithCustomNameFromDataMember"].Nullable);
Assert.True(schema.Properties["NonRequiredWithCustomNameFromDataMember"].Nullable);

Assert.Equal(
new[]
{

"StringWithDataMemberRequired",
"StringWithDataMemberNonRequired",
"RequiredWithCustomNameFromDataMember",
"NonRequiredWithCustomNameFromDataMember"
},
schema.Properties.Keys.ToArray()
);

Assert.Equal(
new[]
{
"RequiredWithCustomNameFromDataMember",
"StringWithDataMemberRequired"
},
schema.Required.ToArray()
);
}

[Theory]
[InlineData(typeof(ProblemDetails))]
[InlineData(typeof(ValidationProblemDetails))]
Expand Down

0 comments on commit 5ec8ff2

Please sign in to comment.