Skip to content

Commit

Permalink
Merge pull request #855 from SmRiley/release/6.x
Browse files Browse the repository at this point in the history
Use NullabilityInfoContext to replace Type's nullable detection in release 6.x
  • Loading branch information
mayuki authored Oct 16, 2024
2 parents f429c4d + 1c10756 commit bf5ec1f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
@in = "formData",
type = swaggerDataType,
description = parameterXmlComment,
required = !x.IsOptional && !x.ParameterType.IsNullable(), // OpenAPI 3 has a separate definition for nullable, but for OpenAPI 2, there is only required.
required = !x.IsOptional && !x.IsNullable(), // OpenAPI 3 has a separate definition for nullable, but for OpenAPI 2, there is only required.
@default = defaultObjectExample ?? ((x.IsOptional) ? defaultValue : null),
items = items,
@enum = enums,
Expand Down Expand Up @@ -488,4 +488,4 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ

return property;
}
}
}
11 changes: 0 additions & 11 deletions src/MagicOnion.Server.HttpGateway/Swagger/Utils.cs

This file was deleted.

10 changes: 8 additions & 2 deletions src/MagicOnion.Server.HttpGateway/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using System.Reflection;
using System.Reflection;

namespace MagicOnion;

internal static class Utils
{
private static readonly NullabilityInfoContext nullabilityInfoContext = new();
public static bool IsNullable(this ParameterInfo type)
{
return nullabilityInfoContext.Create(type).WriteState == NullabilityState.Nullable;
}

public static bool IsNullable(this Type type)
{
return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
}
}

0 comments on commit bf5ec1f

Please sign in to comment.