-
Notifications
You must be signed in to change notification settings - Fork 0
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
[PM-6170] Explore making responses required and nullable #60
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
using Bit.Core.Models.Api; | ||
#nullable enable | ||
|
||
using Bit.Core.Models.Api; | ||
using Bit.Core.SecretsManager.Entities; | ||
|
||
namespace Bit.Api.SecretsManager.Models.Response; | ||
|
@@ -24,39 +26,31 @@ | |
Projects = secret.Projects?.Select(p => new SecretResponseInnerProject(p)); | ||
} | ||
|
||
public BaseSecretResponseModel(string objectName = _objectName) : base(objectName) | ||
{ | ||
} | ||
|
||
public BaseSecretResponseModel() : base(_objectName) | ||
{ | ||
} | ||
|
||
public Guid Id { get; set; } | ||
public Guid Id { get; } | ||
|
||
public Guid OrganizationId { get; set; } | ||
public Guid OrganizationId { get; } | ||
|
||
public string Key { get; set; } | ||
public string? Key { get; } | ||
|
||
public string Value { get; set; } | ||
public string? Value { get; } | ||
|
||
public string Note { get; set; } | ||
public string? Note { get; } | ||
|
||
public DateTime CreationDate { get; set; } | ||
public DateTime CreationDate { get; } | ||
|
||
public DateTime RevisionDate { get; set; } | ||
public DateTime RevisionDate { get; } | ||
|
||
public IEnumerable<SecretResponseInnerProject> Projects { get; set; } | ||
public IEnumerable<SecretResponseInnerProject>? Projects { get; init; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Using 'init' here might cause issues if Projects need to be modified after initialization |
||
|
||
public class SecretResponseInnerProject | ||
{ | ||
public SecretResponseInnerProject(Project project) | ||
Check warning on line 47 in src/Api/SecretsManager/Models/Response/BaseSecretResponseModel.cs GitHub Actions / Build artifacts (Api, ./src)
|
||
{ | ||
Id = project.Id; | ||
Name = project.Name; | ||
} | ||
|
||
public SecretResponseInnerProject() | ||
Check warning on line 53 in src/Api/SecretsManager/Models/Response/BaseSecretResponseModel.cs GitHub Actions / Build artifacts (Api, ./src)
|
||
{ | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Reflection; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace Bit.SharedWeb.Swagger; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Add a brief description of the class's purpose in the summary |
||
/// <remarks> | ||
/// Credits: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2036#issuecomment-894015122 | ||
/// </remarks> | ||
public class RequireNotNullableSchemaFilter : ISchemaFilter | ||
{ | ||
public void Apply(OpenApiSchema schema, SchemaFilterContext context) | ||
{ | ||
if (schema.Properties == null) | ||
{ | ||
return; | ||
} | ||
|
||
FixNullableProperties(schema, context); | ||
|
||
var notNullableProperties = schema | ||
.Properties | ||
.Where(x => !x.Value.Nullable && x.Value.Default == default && !schema.Required.Contains(x.Key)) | ||
.ToList(); | ||
Comment on lines
+24
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider using |
||
|
||
foreach (var property in notNullableProperties) | ||
{ | ||
schema.Required.Add(property.Key); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Option "SupportNonNullableReferenceTypes" not working with complex types ({ "type": "object" }), | ||
/// so they always have "Nullable = false", | ||
/// see method "SchemaGenerator.GenerateSchemaForMember" | ||
/// </summary> | ||
private static void FixNullableProperties(OpenApiSchema schema, SchemaFilterContext context) | ||
{ | ||
foreach (var property in schema.Properties) | ||
{ | ||
var field = context.Type | ||
.GetMembers(BindingFlags.Public | BindingFlags.Instance) | ||
.FirstOrDefault(x => | ||
string.Equals(x.Name, property.Key, StringComparison.InvariantCultureIgnoreCase)); | ||
|
||
if (field == null) | ||
{ | ||
continue; | ||
} | ||
|
||
var fieldType = field switch | ||
{ | ||
FieldInfo fieldInfo => fieldInfo.FieldType, | ||
PropertyInfo propertyInfo => propertyInfo.PropertyType, | ||
_ => throw new NotSupportedException(), | ||
}; | ||
Comment on lines
+54
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Handle potential |
||
|
||
property.Value.Nullable = fieldType.IsValueType | ||
? Nullable.GetUnderlyingType(fieldType) != null | ||
: !field.IsNonNullableReferenceType(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider if these fields should be nullable. If they are required, remove the '?'