Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support [DataMember] IsRequired in NewtonsoftDataContractResolver #2644

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<LangVersion>10</LangVersion>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyOriginatorKeyFile>Swashbuckle.AspNetCore.IntegrationTests.snk</AssemblyOriginatorKeyFile>
<NoWarn>$(NoWarn);8002</NoWarn>
<SignAssembly>true</SignAssembly>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
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
@@ -1,8 +1,10 @@
using Newtonsoft.Json;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace Swashbuckle.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 Swashbuckle.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,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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -725,14 +725,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 @@ -742,6 +745,8 @@ 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]
Expand All @@ -752,7 +757,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonRequired()
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 @@ -767,6 +772,7 @@ public void GenerateSchema_HonorsSerializerAttribute_JsonObject()
Assert.Equal(
new[]
{
"StringWithDataMemberRequiredFalse",
"StringWithNoAnnotation",
"StringWithRequiredAllowNull",
"StringWithRequiredUnspecified"
Expand All @@ -776,6 +782,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 @@ -791,6 +798,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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<DocumentationFile>Swashbuckle.AspNetCore.SwaggerGen.Test.xml</DocumentationFile>
<NoWarn>1701;1702;1591</NoWarn>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/CliExample/CliExample.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
<DotNetSwaggerPath>$([System.IO.Path]::Combine("..", "..", "..", "src", "Swashbuckle.AspNetCore.Cli", "bin", $(Configuration), $(TargetFramework), "dotnet-swagger.dll"))</DotNetSwaggerPath>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
<DotNetSwaggerPath>$([System.IO.Path]::Combine("..", "..", "..", "src", "Swashbuckle.AspNetCore.Cli", "bin", $(Configuration), $(TargetFramework), "dotnet-swagger.dll"))</DotNetSwaggerPath>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/ConfigFromFile/ConfigFromFile.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/CustomUIConfig/CustomUIConfig.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/CustomUIIndex/CustomUIIndex.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/GenericControllers/GenericControllers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/MinimalApp/MinimalApp.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DotNetSwaggerPath>$([System.IO.Path]::Combine("..", "..", "..", "src", "Swashbuckle.AspNetCore.Cli", "bin", $(Configuration), $(TargetFramework), "dotnet-swagger"))</DotNetSwaggerPath>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/MultipleVersions/MultipleVersions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/NswagClientExample/NswagClientExample.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<DotNetSwaggerPath>$([System.IO.Path]::Combine("..", "..", "..", "src", "Swashbuckle.AspNetCore.Cli", "bin", $(Configuration), $(TargetFramework), "dotnet-swagger.dll"))</DotNetSwaggerPath>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/OAuth2Integration/OAuth2Integration.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/ReDoc/ReDoc.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/TestFirst/TestFirst.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down