Skip to content

Commit

Permalink
fix missing attribute on nullable number
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed Mar 25, 2024
1 parent e39211b commit 4cf5eaf
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ExclusiveMaximumAttribute(double value)

void IAttributeHandler.AddConstraints(SchemaGenerationContextBase context, Attribute attribute)
{
if (!context.Type.IsNumber()) return;
if (!context.Type.IsNumber() && !context.Type.IsNullableNumber()) return;

context.Intents.Add(new ExclusiveMaximumIntent(Value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ExclusiveMinimumAttribute(double value)

void IAttributeHandler.AddConstraints(SchemaGenerationContextBase context, Attribute attribute)
{
if (!context.Type.IsNumber()) return;
if (!context.Type.IsNumber() && !context.Type.IsNullableNumber()) return;

context.Intents.Add(new ExclusiveMinimumIntent(Value));
}
Expand Down
2 changes: 1 addition & 1 deletion JsonSchema.Generation/Attributes/IfMaxAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public IfMaxAttribute(string propertyName, double value, object? group)
{
if (PropertyType == null) return null;

if (PropertyType.IsNumber())
if (!PropertyType.IsNumber() && !PropertyType.IsNullableNumber())
{
if (IsExclusive) return new ExclusiveMaximumIntent(Value.ClampToDecimal());
return new MaximumIntent(Value.ClampToDecimal());
Expand Down
2 changes: 1 addition & 1 deletion JsonSchema.Generation/Attributes/IfMinAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public IfMinAttribute(string propertyName, double value, object? group)
{
if (PropertyType == null) return null;

if (PropertyType.IsNumber())
if (!PropertyType.IsNumber() && !PropertyType.IsNullableNumber())
{
if (IsExclusive) return new ExclusiveMinimumIntent(Value.ClampToDecimal());
return new MinimumIntent(Value.ClampToDecimal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void AddConstraints(SchemaGenerationContextBase context, Attribute attrib
var handlingAttribute = attribute as JsonNumberHandlingAttribute;
if (handlingAttribute == null) return;

if (!context.Type.IsNumber()) return;
if (!context.Type.IsNumber() && !context.Type.IsNullableNumber()) return;

var typeIntent = context.Intents.OfType<TypeIntent>().FirstOrDefault();

Expand Down
2 changes: 1 addition & 1 deletion JsonSchema.Generation/Attributes/MaximumAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MaximumAttribute(double value)

void IAttributeHandler.AddConstraints(SchemaGenerationContextBase context, Attribute attribute)
{
if (!context.Type.IsNumber()) return;
if (!context.Type.IsNumber() && !context.Type.IsNullableNumber()) return;

context.Intents.Add(new MaximumIntent(Value));
}
Expand Down
2 changes: 1 addition & 1 deletion JsonSchema.Generation/Attributes/MinimumAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MinimumAttribute(double value)

void IAttributeHandler.AddConstraints(SchemaGenerationContextBase context, Attribute attribute)
{
if (!context.Type.IsNumber()) return;
if (!context.Type.IsNumber() && !context.Type.IsNullableNumber()) return;

context.Intents.Add(new MinimumIntent(Value));
}
Expand Down
2 changes: 1 addition & 1 deletion JsonSchema.Generation/Attributes/MultipleOfAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MultipleOfAttribute(double value)

void IAttributeHandler.AddConstraints(SchemaGenerationContextBase context, Attribute attribute)
{
if (!context.Type.IsNumber()) return;
if (!context.Type.IsNumber() && !context.Type.IsNullableNumber()) return;

context.Intents.Add(new MultipleOfIntent(Value));
}
Expand Down
12 changes: 12 additions & 0 deletions JsonSchema.Generation/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Text;
using System.Text.Json.Serialization;
using Json.More;

namespace Json.Schema.Generation;

Expand Down Expand Up @@ -125,4 +126,15 @@ public static bool IsNullableValueType(this Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}

/// <summary>
/// Determines if the type is a nullable value type, i.e. <see cref="Nullable{T}"/>.
/// </summary>
/// <param name="type">The type</param>
/// <returns>True if the type is <see cref="Nullable{T}"/>; false otherwise.</returns>
public static bool IsNullableNumber(this Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) &&
type.GetGenericArguments()[0].IsNumber();
}
}

0 comments on commit 4cf5eaf

Please sign in to comment.