Skip to content

Commit e4483d8

Browse files
lahmaclaude
andcommitted
Fix MinLength incorrectly added to DateTime and Date types
When a DateTime, DateTimeOffset, DateOnly, or TimeOnly property has [Required], it was getting MinLength=1 because these types are represented as strings with a format in JSON Schema. This is incorrect - MinLength should only apply to actual string properties, not date/time types that use string representation. Added IsDateTimeFormat check to exclude date-time, date, time, duration, and time-span formats from the MinLength=1 logic. Fixes #1583 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e6a278b commit e4483d8

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/NJsonSchema.Tests/Generation/AttributeGenerationTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,37 @@ public async Task When_dictionary_property_has_regex_attribute_then_regex_is_add
299299
Assert.Null(schema.Properties["Versions"].Pattern);
300300
Assert.NotNull(schema.Properties["Versions"].AdditionalPropertiesSchema.ActualSchema.Pattern);
301301
}
302+
303+
public class ClassWithRequiredDateTimeProperties
304+
{
305+
[Required]
306+
public DateTime RequiredDateTime { get; set; }
307+
308+
[Required]
309+
public DateTimeOffset RequiredDateTimeOffset { get; set; }
310+
311+
[Required]
312+
public DateOnly RequiredDateOnly { get; set; }
313+
314+
[Required]
315+
public TimeOnly RequiredTimeOnly { get; set; }
316+
317+
[Required]
318+
public string RequiredString { get; set; }
319+
}
320+
321+
[Fact]
322+
public void When_required_DateTime_then_MinLength_is_not_set()
323+
{
324+
// Act
325+
var schema = NewtonsoftJsonSchemaGenerator.FromType<ClassWithRequiredDateTimeProperties>();
326+
327+
// Assert
328+
Assert.Null(schema.Properties["RequiredDateTime"].MinLength);
329+
Assert.Null(schema.Properties["RequiredDateTimeOffset"].MinLength);
330+
Assert.Null(schema.Properties["RequiredDateOnly"].MinLength);
331+
Assert.Null(schema.Properties["RequiredTimeOnly"].MinLength);
332+
Assert.Equal(1, schema.Properties["RequiredString"].MinLength);
333+
}
302334
}
303335
}

src/NJsonSchema/Generation/JsonSchemaGenerator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,8 @@ public void AddProperty(
12051205
if (hasRequiredAttribute &&
12061206
!propertyTypeDescription.IsEnum &&
12071207
propertyTypeDescription.Type == JsonObjectType.String &&
1208-
!requiredAttribute.TryGetPropertyValue("AllowEmptyStrings", false))
1208+
!requiredAttribute.TryGetPropertyValue("AllowEmptyStrings", false) &&
1209+
!IsDateTimeFormat(propertyTypeDescription.Format))
12091210
{
12101211
propertySchema.MinLength = 1;
12111212
}
@@ -1306,6 +1307,12 @@ public bool IsPropertyIgnoredBySettings(ContextualAccessorInfo accessorInfo)
13061307
return accessorInfo.GetAttributes(true).FirstAssignableToTypeNameOrDefault("DataMemberAttribute", TypeNameStyle.Name);
13071308
}
13081309

1310+
private static bool IsDateTimeFormat(string? format)
1311+
{
1312+
return format is JsonFormatStrings.DateTime or JsonFormatStrings.Date or JsonFormatStrings.Time
1313+
or JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan;
1314+
}
1315+
13091316
private static bool HasDataContractAttribute(Type parentType)
13101317
{
13111318
return parentType.ToCachedType()

0 commit comments

Comments
 (0)