diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs index a9cd594e0b..1135f91e4e 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs @@ -183,6 +183,10 @@ private OpenApiSchema GenerateSchemaForType(Type modelType, SchemaRepository sch if (schema.Reference == null) { ApplyFilters(schema, modelType, schemaRepository); + if (Nullable.GetUnderlyingType(modelType) != null) + { + schema.Nullable = true; + } } return schema; diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs index a064b0fdb5..c424c7d484 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs @@ -161,13 +161,17 @@ public void GenerateSchema_GeneratesReferencedDictionarySchema_IfDictionaryTypeI Assert.Equal(schema.AdditionalProperties.Reference.Id, referenceSchema.Reference.Id); // ref to self } - public static TheoryData EnumerableTypeData => new() - { - { typeof(int[]), JsonSchemaTypes.Integer, "int32" }, - { typeof(IEnumerable), JsonSchemaTypes.String, null }, - { typeof(DateTime?[]), JsonSchemaTypes.String, "date-time" }, - { typeof(int[][]), JsonSchemaTypes.Array, null }, - { typeof(IList), null, null }, + public static TheoryData EnumerableTypeData => new() + { + { typeof(int[]), JsonSchemaTypes.Integer, "int32", false }, + { typeof(int?[]), JsonSchemaTypes.Integer, "int32", true }, + { typeof(double[]), JsonSchemaTypes.Number, "double", false }, + { typeof(double?[]), JsonSchemaTypes.Number, "double", true }, + { typeof(DateTime[]), JsonSchemaTypes.String, "date-time", false }, + { typeof(DateTime?[]), JsonSchemaTypes.String, "date-time", true }, + { typeof(IEnumerable), JsonSchemaTypes.String, null, false }, + { typeof(int[][]), JsonSchemaTypes.Array, null, false }, + { typeof(IList), null, null, false }, }; [Theory] @@ -175,7 +179,8 @@ public void GenerateSchema_GeneratesReferencedDictionarySchema_IfDictionaryTypeI public void GenerateSchema_GeneratesArraySchema_IfEnumerableType( Type type, JsonSchemaType expectedItemsType, - string expectedItemsFormat) + string expectedItemsFormat, + bool expectedItemsNullable) { var schema = Subject().GenerateSchema(type, new SchemaRepository()); @@ -183,6 +188,7 @@ public void GenerateSchema_GeneratesArraySchema_IfEnumerableType( Assert.NotNull(schema.Items); Assert.Equal(expectedItemsType, schema.Items.Type); Assert.Equal(expectedItemsFormat, schema.Items.Format); + Assert.Equal(expectedItemsNullable, schema.Items.Nullable); } [Theory]