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

Add support for required keyword #2810

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -67,13 +67,22 @@ private OpenApiSchema GenerateSchemaForMember(
if (dataProperty != null)
{
var requiredAttribute = customAttributes.OfType<RequiredAttribute>().FirstOrDefault();
schema.Nullable = _generatorOptions.SupportNonNullableReferenceTypes
? dataProperty.IsNullable && requiredAttribute==null && !memberInfo.IsNonNullableReferenceType()
: dataProperty.IsNullable && requiredAttribute==null;

schema.ReadOnly = dataProperty.IsReadOnly;
schema.WriteOnly = dataProperty.IsWriteOnly;
schema.MinLength = modelType == typeof(string) && requiredAttribute is { AllowEmptyStrings: false } ? 1 : null;
keahpeters marked this conversation as resolved.
Show resolved Hide resolved
keahpeters marked this conversation as resolved.
Show resolved Hide resolved

#if NET7_0_OR_GREATER
var hasRequiredMemberAttribute = customAttributes.OfType<System.Runtime.CompilerServices.RequiredMemberAttribute>().Any();

schema.Nullable = _generatorOptions.SupportNonNullableReferenceTypes
? dataProperty.IsNullable && requiredAttribute == null && !hasRequiredMemberAttribute && !memberInfo.IsNonNullableReferenceType()
: dataProperty.IsNullable && requiredAttribute == null && !hasRequiredMemberAttribute;
#else
schema.Nullable = _generatorOptions.SupportNonNullableReferenceTypes
? dataProperty.IsNullable && requiredAttribute==null && !memberInfo.IsNonNullableReferenceType()
: dataProperty.IsNullable && requiredAttribute==null;
#endif
}

var defaultValueAttribute = customAttributes.OfType<DefaultValueAttribute>().FirstOrDefault();
Expand Down Expand Up @@ -392,7 +401,13 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
? GenerateSchemaForMember(dataProperty.MemberType, schemaRepository, dataProperty.MemberInfo, dataProperty)
: GenerateSchemaForType(dataProperty.MemberType, schemaRepository);

if ((dataProperty.IsRequired || customAttributes.OfType<RequiredAttribute>().Any())
if ((
dataProperty.IsRequired
|| customAttributes.OfType<RequiredAttribute>().Any()
martincostello marked this conversation as resolved.
Show resolved Hide resolved
#if NET7_0_OR_GREATER
|| customAttributes.OfType<System.Runtime.CompilerServices.RequiredMemberAttribute>().Any()
#endif
)
&& !schema.Required.Contains(dataProperty.Name))
{
schema.Required.Add(dataProperty.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Xunit;
using Swashbuckle.AspNetCore.TestSupport;
using Microsoft.OpenApi.Any;
using System.ComponentModel.DataAnnotations;
keahpeters marked this conversation as resolved.
Show resolved Hide resolved

namespace Swashbuckle.AspNetCore.SwaggerGen.Test
{
Expand Down Expand Up @@ -343,6 +344,42 @@ public void GenerateSchema_SetsReadOnlyAndWriteOnlyFlags_IfPropertyIsRestricted(
Assert.True(schema.Properties["WriteOnlyProperty"].WriteOnly);
}

#if NET7_0_OR_GREATER
public class TypeWithRequiredProperty
{
public required string RequiredProperty { get; set; }
}

public class TypeWithRequiredPropertyAndValidationAttribute
{
[MinLength(1)]
public required string RequiredProperty { get; set; }
}

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

var referenceSchema = Subject().GenerateSchema(typeof(TypeWithRequiredProperty), schemaRepository);

var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
Assert.Equal(new[] { "RequiredProperty" }, schema.Required.ToArray());
}

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

var referenceSchema = Subject().GenerateSchema(typeof(TypeWithRequiredPropertyAndValidationAttribute), schemaRepository);

var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
Assert.False(schema.Properties["RequiredProperty"].Nullable);
Assert.Equal(new[] { "RequiredProperty" }, schema.Required.ToArray());
}
#endif

[Theory]
[InlineData(typeof(TypeWithParameterizedConstructor), nameof(TypeWithParameterizedConstructor.Id), false)]
[InlineData(typeof(TypeWithParameterlessAndParameterizedConstructor), nameof(TypeWithParameterlessAndParameterizedConstructor.Id), true)]
Expand Down