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

Commit 328b0ec

Browse files
committed
1 parent 8c301f3 commit 328b0ec

File tree

7 files changed

+89
-20
lines changed

7 files changed

+89
-20
lines changed

src/DotSwashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/JsonPropertyExtensions.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,5 @@ public static bool TryGetMemberInfo(this JsonProperty jsonProperty, out MemberIn
1414

1515
return (memberInfo != null);
1616
}
17-
18-
public static bool IsRequiredSpecified(this JsonProperty jsonProperty)
19-
{
20-
if (!jsonProperty.TryGetMemberInfo(out MemberInfo memberInfo))
21-
return false;
22-
23-
if (memberInfo.GetCustomAttribute<JsonRequiredAttribute>() != null)
24-
return true;
25-
26-
var jsonPropertyAttribute = memberInfo.GetCustomAttribute<JsonPropertyAttribute>();
27-
return jsonPropertyAttribute != null && jsonPropertyAttribute.Required != Required.Default;
28-
}
2917
}
3018
}

src/DotSwashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private IEnumerable<DataProperty> GetDataPropertiesFor(JsonObjectContract jsonOb
140140
{
141141
if (jsonProperty.Ignored) continue;
142142

143-
var required = jsonProperty.IsRequiredSpecified()
143+
var required = jsonProperty.Required != Required.Default
144144
? jsonProperty.Required
145145
: jsonObjectContract.ItemRequired ?? Required.Default;
146146

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace DotSwashbuckle.AspNetCore.Newtonsoft.Test
4+
{
5+
[DataContract(Name = "CustomNameFromDataContract")]
6+
public class DataMemberAnnotatedType
7+
{
8+
[DataMember(IsRequired = true)]
9+
public string StringWithDataMemberRequired { get; set; }
10+
11+
[DataMember(IsRequired = false)]
12+
public string StringWithDataMemberNonRequired { get; set; }
13+
14+
[DataMember(IsRequired = true, Name = "RequiredWithCustomNameFromDataMember")]
15+
public string StringWithDataMemberRequiredWithCustomName { get; set; }
16+
17+
[DataMember(IsRequired = false, Name = "NonRequiredWithCustomNameFromDataMember")]
18+
public string StringWithDataMemberNonRequiredWithCustomName { get; set; }
19+
}
20+
}

test/DotSwashbuckle.AspNetCore.Newtonsoft.Test/Fixtures/JsonObjectAnnotatedType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Newtonsoft.Json;
2+
using System.Runtime.Serialization;
23

34
namespace DotSwashbuckle.AspNetCore.Newtonsoft.Test
45
{
56
[JsonObject(ItemRequired = Required.Always)]
7+
[DataContract]
68
public class JsonObjectAnnotatedType
79
{
810
public string StringWithNoAnnotation { get; set; }
@@ -12,5 +14,8 @@ public class JsonObjectAnnotatedType
1214

1315
[JsonProperty(Required = Required.AllowNull)]
1416
public string StringWithRequiredAllowNull { get; set; }
17+
18+
[DataMember(IsRequired = false)]
19+
public string StringWithDataMemberRequiredFalse { get; set; }
1520
}
1621
}

test/DotSwashbuckle.AspNetCore.Newtonsoft.Test/Fixtures/JsonPropertyAnnotatedType.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using Newtonsoft.Json;
1+
using System.Runtime.Serialization;
2+
using Newtonsoft.Json;
23

34
namespace DotSwashbuckle.AspNetCore.Newtonsoft.Test
45
{
6+
[DataContract]
57
public class JsonPropertyAnnotatedType
68
{
79
[JsonProperty("string-with-json-property-name")]
@@ -21,5 +23,13 @@ public class JsonPropertyAnnotatedType
2123

2224
[JsonProperty(Required = Required.AllowNull)]
2325
public string StringWithRequiredAllowNull { get; set; }
26+
27+
[DataMember(IsRequired = false)] //As the support for DataMember has been implemented later, JsonRequired should take precedence
28+
[JsonProperty(Required = Required.Always)]
29+
public string StringWithRequiredAlwaysButConflictingDataMember { get; set; }
30+
31+
[DataMember(IsRequired = true)] //As the support for DataMember has been implemented later, JsonRequired should take precedence
32+
[JsonProperty(Required = Required.Default)]
33+
public string StringWithRequiredDefaultButConflictingDataMember { get; set; }
2434
}
2535
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
using Newtonsoft.Json;
1+
using System.Runtime.Serialization;
2+
using Newtonsoft.Json;
23

34
namespace DotSwashbuckle.AspNetCore.Newtonsoft.Test
45
{
56
public class JsonRequiredAnnotatedType
67
{
78
[JsonRequired]
89
public string StringWithJsonRequired { get; set; }
10+
11+
[DataMember(IsRequired = false)] //As the support for DataMember has been implemented later, JsonRequired should take precedence
12+
[JsonRequired]
13+
public string StringWithConflictingRequired { get; set; }
914
}
1015
}

test/DotSwashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonIgnore()
752752
public void GenerateSchema_HonorsSerializerAttribute_JsonProperty()
753753
{
754754
var schemaRepository = new SchemaRepository();
755-
756755
var referenceSchema = Subject().GenerateSchema(typeof(JsonPropertyAnnotatedType), schemaRepository);
757-
758756
var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
759757
Assert.Equal(
760758
new[]
@@ -765,14 +763,17 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonProperty()
765763
"StringWithRequiredDisallowNull",
766764
"StringWithRequiredAlways",
767765
"StringWithRequiredAllowNull",
766+
"StringWithRequiredAlwaysButConflictingDataMember",
767+
"StringWithRequiredDefaultButConflictingDataMember"
768768
},
769769
schema.Properties.Keys.ToArray()
770770
);
771771
Assert.Equal(
772772
new[]
773773
{
774774
"StringWithRequiredAllowNull",
775-
"StringWithRequiredAlways"
775+
"StringWithRequiredAlways",
776+
"StringWithRequiredAlwaysButConflictingDataMember"
776777
},
777778
schema.Required.ToArray()
778779
);
@@ -782,17 +783,18 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonProperty()
782783
Assert.False(schema.Properties["StringWithRequiredDisallowNull"].Nullable);
783784
Assert.False(schema.Properties["StringWithRequiredAlways"].Nullable);
784785
Assert.True(schema.Properties["StringWithRequiredAllowNull"].Nullable);
786+
Assert.False(schema.Properties["StringWithRequiredAlwaysButConflictingDataMember"].Nullable);
787+
Assert.True(schema.Properties["StringWithRequiredDefaultButConflictingDataMember"].Nullable);
785788
}
786789

787790
[Fact]
788791
public void GenerateSchema_HonorsSerializerAttribute_JsonRequired()
789792
{
790793
var schemaRepository = new SchemaRepository();
791-
792794
var referenceSchema = Subject().GenerateSchema(typeof(JsonRequiredAnnotatedType), schemaRepository);
793795

794796
var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
795-
Assert.Equal(new[] { "StringWithJsonRequired" }, schema.Required.ToArray());
797+
Assert.Equal(new[] { "StringWithConflictingRequired", "StringWithJsonRequired" }, schema.Required.ToArray());
796798
Assert.False(schema.Properties["StringWithJsonRequired"].Nullable);
797799
}
798800

@@ -807,6 +809,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonObject()
807809
Assert.Equal(
808810
new[]
809811
{
812+
"StringWithDataMemberRequiredFalse",
810813
"StringWithNoAnnotation",
811814
"StringWithRequiredAllowNull",
812815
"StringWithRequiredUnspecified"
@@ -816,6 +819,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonObject()
816819
Assert.False(schema.Properties["StringWithNoAnnotation"].Nullable);
817820
Assert.False(schema.Properties["StringWithRequiredUnspecified"].Nullable);
818821
Assert.True(schema.Properties["StringWithRequiredAllowNull"].Nullable);
822+
Assert.False(schema.Properties["StringWithDataMemberRequiredFalse"].Nullable);
819823
}
820824

821825
[Fact]
@@ -831,6 +835,43 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonExtensionData()
831835
Assert.Null(schema.AdditionalProperties.Type);
832836
}
833837

838+
[Fact]
839+
public void GenerateSchema_HonorsDataMemberAttribute()
840+
{
841+
var schemaRepository = new SchemaRepository();
842+
843+
var referenceSchema = Subject().GenerateSchema(typeof(DataMemberAnnotatedType), schemaRepository);
844+
845+
var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
846+
847+
848+
Assert.True(schema.Properties["StringWithDataMemberRequired"].Nullable);
849+
Assert.True(schema.Properties["StringWithDataMemberNonRequired"].Nullable);
850+
Assert.True(schema.Properties["RequiredWithCustomNameFromDataMember"].Nullable);
851+
Assert.True(schema.Properties["NonRequiredWithCustomNameFromDataMember"].Nullable);
852+
853+
Assert.Equal(
854+
new[]
855+
{
856+
857+
"StringWithDataMemberRequired",
858+
"StringWithDataMemberNonRequired",
859+
"RequiredWithCustomNameFromDataMember",
860+
"NonRequiredWithCustomNameFromDataMember"
861+
},
862+
schema.Properties.Keys.ToArray()
863+
);
864+
865+
Assert.Equal(
866+
new[]
867+
{
868+
"RequiredWithCustomNameFromDataMember",
869+
"StringWithDataMemberRequired"
870+
},
871+
schema.Required.ToArray()
872+
);
873+
}
874+
834875
[Theory]
835876
[InlineData(typeof(ProblemDetails))]
836877
[InlineData(typeof(ValidationProblemDetails))]

0 commit comments

Comments
 (0)