Skip to content

Commit 356c595

Browse files
Scarecrow7250wolf7250martincostello
authored
Fix collections of nullables not being set as nullable (#3364)
* Update to fix Lists/Arrays of nullables not getting marked as nullable * Updated unit test to cover whether items in a array are correctly marked as nullable or not * Update src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs Co-authored-by: Martin Costello <martin@martincostello.com> * Added some more test cases to unit tests --------- Co-authored-by: e.kent <e.kent@omcinternational.com> Co-authored-by: Martin Costello <martin@martincostello.com>
1 parent a7112c2 commit 356c595

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ private OpenApiSchema GenerateSchemaForType(Type modelType, SchemaRepository sch
183183
if (schema.Reference == null)
184184
{
185185
ApplyFilters(schema, modelType, schemaRepository);
186+
if (Nullable.GetUnderlyingType(modelType) != null)
187+
{
188+
schema.Nullable = true;
189+
}
186190
}
187191

188192
return schema;

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,28 +161,34 @@ public void GenerateSchema_GeneratesReferencedDictionarySchema_IfDictionaryTypeI
161161
Assert.Equal(schema.AdditionalProperties.Reference.Id, referenceSchema.Reference.Id); // ref to self
162162
}
163163

164-
public static TheoryData<Type, JsonSchemaType, string> EnumerableTypeData => new()
165-
{
166-
{ typeof(int[]), JsonSchemaTypes.Integer, "int32" },
167-
{ typeof(IEnumerable<string>), JsonSchemaTypes.String, null },
168-
{ typeof(DateTime?[]), JsonSchemaTypes.String, "date-time" },
169-
{ typeof(int[][]), JsonSchemaTypes.Array, null },
170-
{ typeof(IList), null, null },
164+
public static TheoryData<Type, JsonSchemaType, string, bool> EnumerableTypeData => new()
165+
{
166+
{ typeof(int[]), JsonSchemaTypes.Integer, "int32", false },
167+
{ typeof(int?[]), JsonSchemaTypes.Integer, "int32", true },
168+
{ typeof(double[]), JsonSchemaTypes.Number, "double", false },
169+
{ typeof(double?[]), JsonSchemaTypes.Number, "double", true },
170+
{ typeof(DateTime[]), JsonSchemaTypes.String, "date-time", false },
171+
{ typeof(DateTime?[]), JsonSchemaTypes.String, "date-time", true },
172+
{ typeof(IEnumerable<string>), JsonSchemaTypes.String, null, false },
173+
{ typeof(int[][]), JsonSchemaTypes.Array, null, false },
174+
{ typeof(IList), null, null, false },
171175
};
172176

173177
[Theory]
174178
[MemberData(nameof(EnumerableTypeData))]
175179
public void GenerateSchema_GeneratesArraySchema_IfEnumerableType(
176180
Type type,
177181
JsonSchemaType expectedItemsType,
178-
string expectedItemsFormat)
182+
string expectedItemsFormat,
183+
bool expectedItemsNullable)
179184
{
180185
var schema = Subject().GenerateSchema(type, new SchemaRepository());
181186

182187
Assert.Equal(JsonSchemaTypes.Array, schema.Type);
183188
Assert.NotNull(schema.Items);
184189
Assert.Equal(expectedItemsType, schema.Items.Type);
185190
Assert.Equal(expectedItemsFormat, schema.Items.Format);
191+
Assert.Equal(expectedItemsNullable, schema.Items.Nullable);
186192
}
187193

188194
[Theory]

0 commit comments

Comments
 (0)