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 @@ -274,6 +274,12 @@ JsonNode TransformSchemaNode(JsonSchemaExporterContext schemaExporterContext, Js
objSchema.InsertAtStart(TypePropertyName, "string");
}

// Include the type keyword in nullable enum types
if (Nullable.GetUnderlyingType(ctx.TypeInfo.Type)?.IsEnum is true && objSchema.ContainsKey(EnumPropertyName) && !objSchema.ContainsKey(TypePropertyName))
{
objSchema.InsertAtStart(TypePropertyName, new JsonArray { (JsonNode)"string", (JsonNode)"null" });
}

// Filter potentially disallowed keywords.
foreach (string keyword in _schemaKeywordsDisallowedByAIVendors)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,20 @@ public static void CreateJsonSchema_AcceptsOptionsWithoutResolver()
Assert.Same(options.TypeInfoResolver, AIJsonUtilities.DefaultOptions.TypeInfoResolver);
}

[Fact]
public static void CreateJsonSchema_NullableEnum_IncludesTypeKeyword()
{
JsonElement expectedSchema = JsonDocument.Parse("""
{
"type": ["string", "null"],
"enum": ["A", "B", null]
}
""").RootElement;

JsonElement schema = AIJsonUtilities.CreateJsonSchema(typeof(MyEnumValue?), serializerOptions: JsonContext.Default.Options);
AssertDeepEquals(expectedSchema, schema);
}

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

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