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

Fix to make required and nullable properties nullable in schema #2879

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change just reverting back completely to the old behaviour from 6.5.0 and undoing the support for required? Or am I misinterpreting the diff?

Copy link
Contributor Author

@keahpeters keahpeters May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only reverting the way we set Nullable and MinLength. This didn't need to be changed since the property can still be null or an empty string with the required keyword. The required keyword will still be supported with this change.

Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,13 @@ private OpenApiSchema GenerateSchemaForMember(
{
var requiredAttribute = customAttributes.OfType<RequiredAttribute>().FirstOrDefault();

schema.ReadOnly = dataProperty.IsReadOnly;
schema.WriteOnly = dataProperty.IsWriteOnly;

#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;

schema.MinLength = modelType == typeof(string) && (hasRequiredMemberAttribute || requiredAttribute is { AllowEmptyStrings: false }) ? 1 : null;
#else
schema.Nullable = _generatorOptions.SupportNonNullableReferenceTypes
? dataProperty.IsNullable && requiredAttribute==null && !memberInfo.IsNonNullableReferenceType()
: dataProperty.IsNullable && requiredAttribute==null;
? 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;
#endif
}

var defaultValueAttribute = customAttributes.OfType<DefaultValueAttribute>().FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,10 @@ public void GenerateSchema_SetsReadOnlyAndWriteOnlyFlags_IfPropertyIsRestricted(
}

#if NET7_0_OR_GREATER
public class TypeWithRequiredProperty
public class TypeWithRequiredProperties
martincostello marked this conversation as resolved.
Show resolved Hide resolved
{
public required string RequiredProperty { get; set; }
public required string RequiredString { get; set; }
public required int RequiredInt { get; set; }
}

public class TypeWithRequiredPropertyAndValidationAttribute
Expand All @@ -383,10 +384,13 @@ public void GenerateSchema_SetsRequired_IfPropertyHasRequiredKeyword()
{
var schemaRepository = new SchemaRepository();

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

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

[Fact]
Expand All @@ -398,7 +402,7 @@ public void GenerateSchema_SetsRequired_IfPropertyHasRequiredKeywordAndValidatio

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