Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Feb 12, 2024
1 parent 8c301f3 commit 328b0ec
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +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 jsonPropertyAttribute = memberInfo.GetCustomAttribute<JsonPropertyAttribute>();
return jsonPropertyAttribute != null && jsonPropertyAttribute.Required != Required.Default;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private IEnumerable<DataProperty> GetDataPropertiesFor(JsonObjectContract jsonOb
{
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 DotSwashbuckle.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
@@ -1,8 +1,10 @@
using Newtonsoft.Json;
using System.Runtime.Serialization;

namespace DotSwashbuckle.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 DotSwashbuckle.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,15 @@
using Newtonsoft.Json;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace DotSwashbuckle.AspNetCore.Newtonsoft.Test
{
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 @@ -752,9 +752,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonIgnore()
public void GenerateSchema_HonorsSerializerAttribute_JsonProperty()
{
var schemaRepository = new SchemaRepository();

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

var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
Assert.Equal(
new[]
Expand All @@ -765,14 +763,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 @@ -782,17 +783,18 @@ 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]
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.Equal(new[] { "StringWithConflictingRequired", "StringWithJsonRequired" }, schema.Required.ToArray());
Assert.False(schema.Properties["StringWithJsonRequired"].Nullable);
}

Expand All @@ -807,6 +809,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonObject()
Assert.Equal(
new[]
{
"StringWithDataMemberRequiredFalse",
"StringWithNoAnnotation",
"StringWithRequiredAllowNull",
"StringWithRequiredUnspecified"
Expand All @@ -816,6 +819,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 All @@ -831,6 +835,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 328b0ec

Please sign in to comment.