Skip to content

Commit 6254b15

Browse files
committed
Fix conflicting required+nullable schema when only NonNullableReferenceTypesAsRequired is enabled
1 parent d22870e commit 6254b15

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private bool IsNullable(RequiredAttribute requiredAttribute, DataProperty dataPr
139139
{
140140
var nullable = dataProperty.IsNullable && requiredAttribute == null;
141141

142-
if (_generatorOptions.SupportNonNullableReferenceTypes)
142+
if (_generatorOptions.SupportNonNullableReferenceTypes || _generatorOptions.NonNullableReferenceTypesAsRequired)
143143
{
144144
nullable &= !memberInfo.IsNonNullableReferenceType();
145145
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,23 @@ public void GenerateSchema_SupportsOption_SuppressImplicitRequiredAttributeForNo
11261126
Assert.True(propertyIsRequired);
11271127
}
11281128

1129+
[Theory]
1130+
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithOneNonNullableContent), nameof(TypeWithNullableContextAnnotated.NonNullableString))]
1131+
[InlineData(typeof(TypeWithNullableContextNotAnnotated), nameof(TypeWithNullableContextNotAnnotated.SubTypeWithOneNonNullableContent), nameof(TypeWithNullableContextNotAnnotated.NonNullableString))]
1132+
public void GenerateSchema_NonNullableReferenceTypesAsRequired_DoesNotMarkPropertyAsNullable(
1133+
Type declaringType,
1134+
string subType,
1135+
string propertyName)
1136+
{
1137+
var subject = Subject(c => c.NonNullableReferenceTypesAsRequired = true);
1138+
var schemaRepository = new SchemaRepository();
1139+
1140+
subject.GenerateSchema(declaringType, schemaRepository);
1141+
1142+
var propertySchema = schemaRepository.Schemas[subType].Properties[propertyName];
1143+
AssertIsNullable(propertySchema, expected: false);
1144+
}
1145+
11291146
[Theory]
11301147
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NullableString), true)]
11311148
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NonNullableString), false)]

0 commit comments

Comments
 (0)