Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private bool IsNullable(RequiredAttribute requiredAttribute, DataProperty dataPr
{
var nullable = dataProperty.IsNullable && requiredAttribute == null;

if (_generatorOptions.SupportNonNullableReferenceTypes)
if (_generatorOptions.SupportNonNullableReferenceTypes || _generatorOptions.NonNullableReferenceTypesAsRequired)
{
nullable &= !memberInfo.IsNonNullableReferenceType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,32 @@ public void GenerateSchema_SetsRequiredAndNullable_IfPropertyHasRequiredKeywordA
AssertIsNullable(schema.Properties["RequiredNonNullableString"], false);
Assert.Contains("RequiredNonNullableString", schema.Required.ToArray());
}

[Fact]
public void GenerateSchema_NonNullableReferenceTypesAsRequired_PreservesNullable_IfPropertyHasRequiredKeywordAndIsNullable()
{
var schemaRepository = new SchemaRepository();

var referenceSchema = Subject(configureGenerator: (c) => c.NonNullableReferenceTypesAsRequired = true).GenerateSchema(typeof(TypeWithNullableReferenceTypes), schemaRepository);

var reference = Assert.IsType<OpenApiSchemaReference>(referenceSchema);

Assert.NotNull(reference);
Assert.NotNull(reference.Reference);
Assert.NotNull(reference.Reference.Id);

var schema = schemaRepository.Schemas[reference.Reference.Id];
Assert.NotNull(schema.Properties);
Assert.NotNull(schema.Required);

// required string? - must be present (required) AND may be null (nullable: true)
AssertIsNullable(schema.Properties["RequiredNullableString"]);
Assert.Contains("RequiredNullableString", schema.Required.ToArray());

// required string - must be present (required) AND must have a value (nullable: false)
AssertIsNullable(schema.Properties["RequiredNonNullableString"], false);
Assert.Contains("RequiredNonNullableString", schema.Required.ToArray());
}
#nullable disable

[Theory]
Expand Down Expand Up @@ -1126,6 +1152,23 @@ public void GenerateSchema_SupportsOption_SuppressImplicitRequiredAttributeForNo
Assert.True(propertyIsRequired);
}

[Theory]
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithOneNonNullableContent), nameof(TypeWithNullableContextAnnotated.NonNullableString))]
[InlineData(typeof(TypeWithNullableContextNotAnnotated), nameof(TypeWithNullableContextNotAnnotated.SubTypeWithOneNonNullableContent), nameof(TypeWithNullableContextNotAnnotated.NonNullableString))]
public void GenerateSchema_NonNullableReferenceTypesAsRequired_DoesNotMarkPropertyAsNullable(
Type declaringType,
string subType,
string propertyName)
{
var subject = Subject(c => c.NonNullableReferenceTypesAsRequired = true);
var schemaRepository = new SchemaRepository();

subject.GenerateSchema(declaringType, schemaRepository);

var propertySchema = schemaRepository.Schemas[subType].Properties[propertyName];
AssertIsNullable(propertySchema, expected: false);
}

[Theory]
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NullableString), true)]
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NonNullableString), false)]
Expand Down
Loading