Skip to content

Commit

Permalink
Fix regression causing Newtonsoft JsonRequired to be ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorris committed Apr 29, 2020
1 parent 32505fb commit c4dcb1b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ public static bool TryGetMemberInfo(this JsonProperty jsonProperty, out MemberIn

public static bool IsRequiredSpecified(this JsonProperty jsonProperty)
{
var jsonPropertyAttributeData = jsonProperty.TryGetMemberInfo(out MemberInfo memberInfo)
? memberInfo.GetCustomAttributesData().FirstOrDefault(attrData => attrData.AttributeType == typeof(JsonPropertyAttribute))
: null;
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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Swashbuckle.AspNetCore.Newtonsoft.Test
{
public class JsonRequiredAnnotatedType
{
[JsonRequired]
public string StringWithJsonRequired { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@ public void Serialize()
[Fact]
public void Deserialize()
{
var dto = JsonConvert.DeserializeObject<Dictionary<JsonConverterAnnotatedEnum, string>>(
"{ \"value1\": \"foo\", \"value2\": \"bar\" }");
var dto = JsonConvert.DeserializeObject<TestDto>(
"{ \"jsonRequired\": \"foo\", \"jsonProperty\": null }");

Assert.Equal(new[] { JsonConverterAnnotatedEnum.Value1, JsonConverterAnnotatedEnum.Value2 }, dto.Keys);
Assert.Equal("foo", dto.jsonRequired);
Assert.Null(dto.jsonProperty);
}
}

public class TestDto
{
[JsonRequired]
public string jsonRequired;

[JsonProperty(Required = Required.AllowNull)]
public string jsonProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,18 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonProperty()
Assert.True(schema.Properties["StringWithRequiredAllowNull"].Nullable);
}

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

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

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

[Fact]
public void GenerateSchema_HonorsSerializerAttribute_JsonObject()
{
Expand Down

0 comments on commit c4dcb1b

Please sign in to comment.