Skip to content

Commit 0096e1e

Browse files
lahmaclaude
andauthored
Fix SampleJsonDataGenerator overflow for long min/max values (#1901)
HandleIntegerType used Convert.ToInt32 which throws OverflowException when schema minimum/exclusiveMinimum values exceed int range (e.g., long.MinValue or values > ~2.1 billion). Changed to Convert.ToInt64 to support the full range of integer values in JSON Schema. Fixes #1589 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e6a278b commit 0096e1e

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,5 +535,77 @@ public async Task PropertyExclusiveMinimumDefiniton()
535535
Assert.Empty(validationResult);
536536
Assert.Equal(1.1, testJson.SelectToken("body.numberContent.value").Value<double>());
537537
}
538+
539+
[Fact]
540+
public async Task When_integer_has_long_minimum_then_sample_does_not_overflow()
541+
{
542+
// Arrange
543+
var data = @"{
544+
""type"": ""object"",
545+
""properties"": {
546+
""bigInt"": {
547+
""type"": ""integer"",
548+
""minimum"": 3000000000
549+
}
550+
},
551+
""required"": [""bigInt""]
552+
}";
553+
var schema = await JsonSchema.FromJsonAsync(data);
554+
var generator = new SampleJsonDataGenerator();
555+
556+
// Act
557+
var testJson = generator.Generate(schema);
558+
559+
// Assert
560+
Assert.Equal(3000000000L, testJson.SelectToken("bigInt")!.Value<long>());
561+
}
562+
563+
[Fact]
564+
public async Task When_integer_has_long_exclusive_minimum_then_sample_does_not_overflow()
565+
{
566+
// Arrange
567+
var data = @"{
568+
""type"": ""object"",
569+
""properties"": {
570+
""bigInt"": {
571+
""type"": ""integer"",
572+
""exclusiveMinimum"": 5000000000
573+
}
574+
},
575+
""required"": [""bigInt""]
576+
}";
577+
var schema = await JsonSchema.FromJsonAsync(data);
578+
var generator = new SampleJsonDataGenerator();
579+
580+
// Act
581+
var testJson = generator.Generate(schema);
582+
583+
// Assert
584+
Assert.Equal(5000000000L, testJson.SelectToken("bigInt")!.Value<long>());
585+
}
586+
587+
[Fact]
588+
public async Task When_integer_has_negative_long_minimum_then_sample_does_not_overflow()
589+
{
590+
// Arrange
591+
var data = @"{
592+
""type"": ""object"",
593+
""properties"": {
594+
""bigNegative"": {
595+
""type"": ""integer"",
596+
""minimum"": -9223372036854775808
597+
}
598+
},
599+
""required"": [""bigNegative""]
600+
}";
601+
var schema = await JsonSchema.FromJsonAsync(data);
602+
var generator = new SampleJsonDataGenerator();
603+
604+
// Act
605+
var testJson = generator.Generate(schema);
606+
607+
// Assert
608+
Assert.Equal(long.MinValue, testJson.SelectToken("bigNegative")!.Value<long>());
609+
}
538610
}
539611
}

src/NJsonSchema/Generation/SampleJsonDataGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ private static JToken HandleIntegerType(JsonSchema schema)
151151
{
152152
if (schema.ExclusiveMinimumRaw != null)
153153
{
154-
return JToken.FromObject(Convert.ToInt32(schema.ExclusiveMinimumRaw, CultureInfo.InvariantCulture));
154+
return JToken.FromObject(Convert.ToInt64(schema.ExclusiveMinimumRaw, CultureInfo.InvariantCulture));
155155
}
156156
else if (schema.ExclusiveMinimum != null)
157157
{
158-
return JToken.FromObject(Convert.ToInt32(schema.ExclusiveMinimum, CultureInfo.InvariantCulture));
158+
return JToken.FromObject(Convert.ToInt64(schema.ExclusiveMinimum, CultureInfo.InvariantCulture));
159159
}
160160
else if (schema.Minimum.HasValue)
161161
{
162-
return Convert.ToInt32(schema.Minimum, CultureInfo.InvariantCulture);
162+
return Convert.ToInt64(schema.Minimum, CultureInfo.InvariantCulture);
163163
}
164164
return JToken.FromObject(0);
165165
}

0 commit comments

Comments
 (0)