Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ void ApplyDataAnnotations(ref JsonNode schema, AIJsonSchemaCreateContext ctx)
if (ResolveAttribute<MinLengthAttribute>() is { } minLengthAttribute)
{
JsonObject obj = ConvertSchemaToObject(ref schema);
if (obj[TypePropertyName] is JsonNode typeNode && typeNode.GetValueKind() is JsonValueKind.String && typeNode.GetValue<string>() is "string")
if (TryGetSchemaType(obj, out string? schemaType, out _) && schemaType is "string")
{
obj[MinLengthStringPropertyName] ??= minLengthAttribute.Length;
}
Expand All @@ -440,7 +440,7 @@ void ApplyDataAnnotations(ref JsonNode schema, AIJsonSchemaCreateContext ctx)
if (ResolveAttribute<MaxLengthAttribute>() is { } maxLengthAttribute)
{
JsonObject obj = ConvertSchemaToObject(ref schema);
if (obj[TypePropertyName] is JsonNode typeNode && typeNode.GetValueKind() is JsonValueKind.String && typeNode.GetValue<string>() is "string")
if (TryGetSchemaType(obj, out string? schemaType, out _) && schemaType is "string")
{
obj[MaxLengthStringPropertyName] ??= maxLengthAttribute.Length;
}
Expand Down Expand Up @@ -530,7 +530,7 @@ void ApplyDataAnnotations(ref JsonNode schema, AIJsonSchemaCreateContext ctx)
{
JsonObject obj = ConvertSchemaToObject(ref schema);

if (obj[TypePropertyName] is JsonNode typeNode && typeNode.GetValueKind() is JsonValueKind.String && typeNode.GetValue<string>() is "string")
if (TryGetSchemaType(obj, out string? schemaType, out _) && schemaType is "string")
{
if (lengthAttribute.MinimumLength > 0)
{
Expand Down Expand Up @@ -629,6 +629,58 @@ static JsonArray CreateJsonArray(object?[] values, JsonSerializerOptions seriali
}
}
#endif
#if NET || NETFRAMEWORK
static bool TryGetSchemaType(JsonObject schema, [NotNullWhen(true)] out string? schemaType, out bool isNullable)
{
schemaType = null;
isNullable = false;

if (!schema.TryGetPropertyValue(TypePropertyName, out JsonNode? typeNode))
{
return false;
}

switch (typeNode?.GetValueKind())
{
case JsonValueKind.String:
schemaType = typeNode.GetValue<string>();
return true;

case JsonValueKind.Array:
string? foundSchemaType = null;
foreach (JsonNode? entry in (JsonArray)typeNode)
{
if (entry?.GetValueKind() is not JsonValueKind.String)
{
return false;
}

string entryValue = entry.GetValue<string>();
if (entryValue is "null")
{
isNullable = true;
continue;
}

if (foundSchemaType is null)
{
foundSchemaType = entryValue;
}
else if (foundSchemaType != entryValue)
{
return false;
}
}

schemaType = foundSchemaType;
return schemaType is not null;

default:
return false;
}
}
#endif

TAttribute? ResolveAttribute<TAttribute>()
where TAttribute : Attribute
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,22 +670,22 @@ public static void CreateJsonSchema_IncorporatesTypesAndAnnotations_Net()
"string",
"null"
],
"minItems": 5
"minLength": 5
},
"MaxLengthProp": {
"type": [
"string",
"null"
],
"maxItems": 50
"maxLength": 50
},
"LengthProp": {
"type": [
"string",
"null"
],
"minItems": 3,
"maxItems": 10
"minLength": 3,
"maxLength": 10
},
"MinLengthArrayProp": {
"type": [
Expand Down Expand Up @@ -848,14 +848,14 @@ public static void CreateJsonSchema_IncorporatesTypesAndAnnotations_NetFx()
"string",
"null"
],
"minItems": 5
"minLength": 5
},
"MaxLengthProp": {
"type": [
"string",
"null"
],
"maxItems": 50
"maxLength": 50
},
"MinLengthArrayProp": {
"type": [
Expand Down Expand Up @@ -972,6 +972,33 @@ private sealed class CreateJsonSchema_IncorporatesTypesAndAnnotations_Type
#endif
}

[Fact]
public static void ClassWithNullableMaxLengthProperty_ReturnsExpectedSchema()
{
JsonElement expectedSchema = JsonDocument.Parse("""
{
"type": "object",
"properties": {
"Value": {
"type": ["string", "null"],
"maxLength": 24,
"minLength": 10
}
}
}
""").RootElement;

JsonElement actualSchema = AIJsonUtilities.CreateJsonSchema(typeof(ClassWithNullableMaxLengthProperty), serializerOptions: JsonContext.Default.Options);
AssertDeepEquals(expectedSchema, actualSchema);
}

public class ClassWithNullableMaxLengthProperty
{
[MinLength(10)]
[MaxLength(24)]
public string? Value { get; set; }
}

[Fact]
public static void AddAIContentType_DerivedAIContent()
{
Expand Down Expand Up @@ -1362,6 +1389,7 @@ private class DerivedAIContent : AIContent
[JsonSerializable(typeof(MyPoco))]
[JsonSerializable(typeof(MyEnumValue?))]
[JsonSerializable(typeof(object[]))]
[JsonSerializable(typeof(ClassWithNullableMaxLengthProperty))]
private partial class JsonContext : JsonSerializerContext;

private static bool DeepEquals(JsonElement element1, JsonElement element2)
Expand Down
Loading