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

Set Nullable=true for array of nullable type in generated open api (fix #2628) #2697

Open
wants to merge 5 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 @@ -189,28 +189,47 @@ private IEnumerable<DataProperty> GetDataPropertiesFor(JsonObjectContract jsonOb
private static readonly Dictionary<Type, Tuple<DataType, string>> PrimitiveTypesAndFormats = new Dictionary<Type, Tuple<DataType, string>>
{
[ typeof(bool) ] = Tuple.Create(DataType.Boolean, (string)null),
[ typeof(bool?) ] = Tuple.Create(DataType.Boolean, (string)null),
[ typeof(byte) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(byte?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(sbyte) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(sbyte?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(short) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(short?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(ushort) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(ushort?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(int) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(int?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(uint) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(uint?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(long) ] = Tuple.Create(DataType.Integer, "int64"),
[ typeof(long?) ] = Tuple.Create(DataType.Integer, "int64"),
[ typeof(ulong) ] = Tuple.Create(DataType.Integer, "int64"),
[ typeof(ulong?) ] = Tuple.Create(DataType.Integer, "int64"),
[ typeof(float) ] = Tuple.Create(DataType.Number, "float"),
[ typeof(float?) ] = Tuple.Create(DataType.Number, "float"),
[ typeof(double) ] = Tuple.Create(DataType.Number, "double"),
[ typeof(double?) ] = Tuple.Create(DataType.Number, "double"),
[ typeof(decimal) ] = Tuple.Create(DataType.Number, "double"),
[ typeof(decimal?) ] = Tuple.Create(DataType.Number, "double"),
[ typeof(byte[]) ] = Tuple.Create(DataType.String, "byte"),
[ typeof(string) ] = Tuple.Create(DataType.String, (string)null),
[ typeof(char) ] = Tuple.Create(DataType.String, (string)null),
[ typeof(char?) ] = Tuple.Create(DataType.String, (string)null),
[ typeof(DateTime) ] = Tuple.Create(DataType.String, "date-time"),
[ typeof(DateTime?) ] = Tuple.Create(DataType.String, "date-time"),
[ typeof(DateTimeOffset) ] = Tuple.Create(DataType.String, "date-time"),
[ typeof(DateTimeOffset?) ] = Tuple.Create(DataType.String, "date-time"),
[ typeof(Guid) ] = Tuple.Create(DataType.String, "uuid"),
[ typeof(Guid?) ] = Tuple.Create(DataType.String, "uuid"),
[ typeof(Uri) ] = Tuple.Create(DataType.String, "uri"),
[ typeof(TimeSpan) ] = Tuple.Create(DataType.String, "date-span"),
[ typeof(TimeSpan?) ] = Tuple.Create(DataType.String, "date-span"),
#if NET6_0_OR_GREATER
[ typeof(DateOnly) ] = Tuple.Create(DataType.String, "date"),
[ typeof(TimeOnly) ] = Tuple.Create(DataType.String, "time")
[ typeof(DateOnly?) ] = Tuple.Create(DataType.String, "date"),
[ typeof(TimeOnly) ] = Tuple.Create(DataType.String, "time"),
[ typeof(TimeOnly?) ] = Tuple.Create(DataType.String, "time")
#endif
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,45 @@ private IEnumerable<DataProperty> GetDataPropertiesFor(Type objectType, out Type
private static readonly Dictionary<Type, Tuple<DataType, string>> PrimitiveTypesAndFormats = new Dictionary<Type, Tuple<DataType, string>>
{
[ typeof(bool) ] = Tuple.Create(DataType.Boolean, (string)null),
[ typeof(bool?) ] = Tuple.Create(DataType.Boolean, (string)null),
[ typeof(byte) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(byte?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(sbyte) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(sbyte?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(short) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(short?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(ushort) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(ushort?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(int) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(int?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(uint) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(uint?) ] = Tuple.Create(DataType.Integer, "int32"),
[ typeof(long) ] = Tuple.Create(DataType.Integer, "int64"),
[ typeof(long?) ] = Tuple.Create(DataType.Integer, "int64"),
[ typeof(ulong) ] = Tuple.Create(DataType.Integer, "int64"),
[ typeof(ulong?) ] = Tuple.Create(DataType.Integer, "int64"),
[ typeof(float) ] = Tuple.Create(DataType.Number, "float"),
[ typeof(float?) ] = Tuple.Create(DataType.Number, "float"),
[ typeof(double) ] = Tuple.Create(DataType.Number, "double"),
[ typeof(double?) ] = Tuple.Create(DataType.Number, "double"),
[ typeof(decimal) ] = Tuple.Create(DataType.Number, "double"),
[ typeof(decimal?) ] = Tuple.Create(DataType.Number, "double"),
[ typeof(byte[]) ] = Tuple.Create(DataType.String, "byte"),
[ typeof(string) ] = Tuple.Create(DataType.String, (string)null),
[ typeof(char) ] = Tuple.Create(DataType.String, (string)null),
[ typeof(char?) ] = Tuple.Create(DataType.String, (string)null),
[ typeof(DateTime) ] = Tuple.Create(DataType.String, "date-time"),
[ typeof(DateTime?) ] = Tuple.Create(DataType.String, "date-time"),
[ typeof(DateTimeOffset) ] = Tuple.Create(DataType.String, "date-time"),
[ typeof(DateTimeOffset?) ] = Tuple.Create(DataType.String, "date-time"),
[ typeof(Guid) ] = Tuple.Create(DataType.String, "uuid"),
[ typeof(Guid?) ] = Tuple.Create(DataType.String, "uuid"),
[ typeof(Uri) ] = Tuple.Create(DataType.String, "uri"),
#if NET6_0_OR_GREATER
[ typeof(DateOnly) ] = Tuple.Create(DataType.String, "date"),
[ typeof(TimeOnly) ] = Tuple.Create(DataType.String, "time")
[ typeof(DateOnly?) ] = Tuple.Create(DataType.String, "date"),
[ typeof(TimeOnly) ] = Tuple.Create(DataType.String, "time"),
[ typeof(TimeOnly?) ] = Tuple.Create(DataType.String, "time")
#endif
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ public SchemaGenerator(SchemaGeneratorOptions generatorOptions, ISerializerDataC
SchemaRepository schemaRepository,
MemberInfo memberInfo = null,
ParameterInfo parameterInfo = null,
ApiParameterRouteInfo routeInfo = null)
ApiParameterRouteInfo routeInfo = null,
bool isEffectiveTypeNeeded = true)
{
if (memberInfo != null)
return GenerateSchemaForMember(modelType, schemaRepository, memberInfo);

if (parameterInfo != null)
return GenerateSchemaForParameter(modelType, schemaRepository, parameterInfo, routeInfo);

return GenerateSchemaForType(modelType, schemaRepository);
return GenerateSchemaForType(modelType, schemaRepository, isEffectiveTypeNeeded);
}

private OpenApiSchema GenerateSchemaForMember(
Expand Down Expand Up @@ -147,9 +148,9 @@ public SchemaGenerator(SchemaGeneratorOptions generatorOptions, ISerializerDataC
return schema;
}

private OpenApiSchema GenerateSchemaForType(Type modelType, SchemaRepository schemaRepository)
private OpenApiSchema GenerateSchemaForType(Type modelType, SchemaRepository schemaRepository, bool isEffectiveTypeNeeded = true)
{
var dataContract = GetDataContractFor(modelType);
var dataContract = GetDataContractFor(modelType, isEffectiveTypeNeeded);

var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts)
? GeneratePolymorphicSchema(dataContract, schemaRepository, knownTypesDataContracts)
Expand All @@ -163,9 +164,9 @@ private OpenApiSchema GenerateSchemaForType(Type modelType, SchemaRepository sch
return schema;
}

private DataContract GetDataContractFor(Type modelType)
private DataContract GetDataContractFor(Type modelType, bool isEffectiveTypeNeeded = true)
{
var effectiveType = Nullable.GetUnderlyingType(modelType) ?? modelType;
var effectiveType = isEffectiveTypeNeeded ? Nullable.GetUnderlyingType(modelType) ?? modelType : modelType;
return _serializerDataContractResolver.GetDataContractForType(effectiveType);
}

Expand Down Expand Up @@ -272,8 +273,9 @@ private OpenApiSchema CreatePrimitiveSchema(DataContract dataContract)
var schema = new OpenApiSchema
{
Type = dataContract.DataType.ToString().ToLower(CultureInfo.InvariantCulture),
Format = dataContract.DataFormat
};
Format = dataContract.DataFormat,
Nullable = Nullable.GetUnderlyingType(dataContract.UnderlyingType) != null
};

// For backcompat only - EnumValues is obsolete
if (dataContract.EnumValues != null)
Expand Down Expand Up @@ -308,7 +310,7 @@ private OpenApiSchema CreateArraySchema(DataContract dataContract, SchemaReposit
return new OpenApiSchema
{
Type = "array",
Items = GenerateSchema(dataContract.ArrayItemType, schemaRepository),
Items = GenerateSchema(dataContract.ArrayItemType, schemaRepository, isEffectiveTypeNeeded:false),
UniqueItems = hasUniqueItems ? (bool?)true : null
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface ISchemaGenerator
SchemaRepository schemaRepository,
MemberInfo memberInfo = null,
ParameterInfo parameterInfo = null,
ApiParameterRouteInfo routeInfo = null);
ApiParameterRouteInfo routeInfo = null,
bool isEffectiveTypeNeeded = true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<LangVersion>10</LangVersion>
<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>
<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>
<TargetFrameworks>net7.0</TargetFrameworks>
</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>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,26 @@ public void GenerateSchema_GeneratesReferencedDictionarySchema_IfDictionaryTypeI
}

[Theory]
[InlineData(typeof(int[]), "integer", "int32")]
[InlineData(typeof(IEnumerable<string>), "string", null)]
[InlineData(typeof(DateTime?[]), "string", "date-time")]
[InlineData(typeof(int[][]), "array", null)]
[InlineData(typeof(IList), null, null)]
[InlineData(typeof(int[]), "integer", "int32", false)]
[InlineData(typeof(int?[]), "integer", "int32", true)]
[InlineData(typeof(IEnumerable<string>), "string", null, false)]
[InlineData(typeof(DateTime[]), "string", "date-time", false)]
[InlineData(typeof(DateTime?[]), "string", "date-time", true)]
[InlineData(typeof(int[][]), "array", null, false)]
[InlineData(typeof(IList), null, null, false)]
public void GenerateSchema_GeneratesArraySchema_IfEnumerableType(
Type type,
string expectedItemsType,
string expectedItemsFormat)
string expectedItemsFormat,
bool expectedItemsNullable)
{
var schema = Subject().GenerateSchema(type, new SchemaRepository());

Assert.Equal("array", schema.Type);
Assert.NotNull(schema.Items);
Assert.Equal(expectedItemsType, schema.Items.Type);
Assert.Equal(expectedItemsFormat, schema.Items.Format);
Assert.Equal(expectedItemsNullable, schema.Items.Nullable);
}

[Theory]
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
Expand Up @@ -139,22 +139,28 @@ public void GenerateSchema_GeneratesReferencedDictionarySchema_IfDictionaryTypeI
}

[Theory]
[InlineData(typeof(int[]), "integer", "int32")]
[InlineData(typeof(IEnumerable<string>), "string", null)]
[InlineData(typeof(DateTime?[]), "string", "date-time")]
[InlineData(typeof(int[][]), "array", null)]
[InlineData(typeof(IList), null, null)]
[InlineData(typeof(int[]), "integer", "int32", false)]
[InlineData(typeof(int?[]), "integer", "int32", true)]
[InlineData(typeof(List<int?>), "integer", "int32", true)]
[InlineData(typeof(Nullable<int>[]), "integer", "int32", true)]
[InlineData(typeof(IEnumerable<string>), "string", null, false)]
[InlineData(typeof(DateTime[]), "string", "date-time", false)]
[InlineData(typeof(DateTime?[]), "string", "date-time", true)]
[InlineData(typeof(int[][]), "array", null, false)]
[InlineData(typeof(IList), null, null, false)]
public void GenerateSchema_GeneratesArraySchema_IfEnumerableType(
Type type,
string expectedItemsType,
string expectedItemsFormat)
string expectedItemsFormat,
bool expectedItemsNullable)
{
var schema = Subject().GenerateSchema(type, new SchemaRepository());

Assert.Equal("array", schema.Type);
Assert.NotNull(schema.Items);
Assert.Equal(expectedItemsType, schema.Items.Type);
Assert.Equal(expectedItemsFormat, schema.Items.Format);
Assert.Equal(expectedItemsNullable, schema.Items.Nullable);
}

[Theory]
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>
<TargetFrameworks>net7.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/Basic/Basic.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>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
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>
<TargetFrameworks>net7.0</TargetFrameworks>
<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>
<TargetFrameworks>net7.0</TargetFrameworks>
<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