Skip to content

Commit 3bf6bca

Browse files
committed
feat: add array anyof support
1 parent b295bfd commit 3bf6bca

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

OpenAi.JsonSchema.Tests/FluentSchemaBuilderTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,44 @@ public void Test_Actions()
173173
Assert.NotNull(json);
174174
Helper.Assert(json);
175175
}
176+
177+
178+
[Fact]
179+
public void Test_array_anyof()
180+
{
181+
var generator = new DefaultSchemaGenerator();
182+
var options = new JsonSchemaOptions(SchemaDefaults.OpenAi, Helper.JsonOptionsSnakeCase);
183+
184+
var schema = generator.Build(options, _ => _
185+
.Object(_ => _
186+
.Property("answer", _ => _
187+
.Array(_ => _
188+
.AnyOf(
189+
_ => _.Object("Write markdown text.", _ => _
190+
.Property("$type", _ => _.Const("text"))
191+
.Property("content", _ => _.Value<string>())
192+
),
193+
_ => _.Object("Show a visually appearing widget.", _ => _
194+
.Property("$type", _ => _.Const("widget"))
195+
.Property("id", "Value form widgetId in context.", _ => _.Value<string>())
196+
.Property("comment", "A user-facing `comment`", _ => _.Value<string>())
197+
),
198+
_ => _.Object("Add a reference to your statement.", _ => _
199+
.Property("$type", _ => _.Const("citation"))
200+
.Property("title", _ => _.Value<string>())
201+
.Property("url", "URL from context.", _ => _.Value<string>())
202+
)
203+
)
204+
)
205+
)
206+
)
207+
);
208+
209+
var json = schema.ToJson();
210+
output.WriteLine(json);
211+
Assert.NotNull(json);
212+
Helper.Assert(json);
213+
}
176214
}
177215

178216
public record FluentDocument(
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"answer": {
5+
"type": "array",
6+
"items": {
7+
"anyOf": [
8+
{
9+
"type": "object",
10+
"description": "Write markdown text.",
11+
"properties": {
12+
"$type": {
13+
"type": "string",
14+
"const": "text"
15+
},
16+
"content": {
17+
"type": "string"
18+
}
19+
},
20+
"required": [
21+
"$type",
22+
"content"
23+
],
24+
"additionalProperties": false
25+
},
26+
{
27+
"type": "object",
28+
"description": "Show a visually appearing widget.",
29+
"properties": {
30+
"$type": {
31+
"type": "string",
32+
"const": "widget"
33+
},
34+
"id": {
35+
"type": "string",
36+
"description": "Value form widgetId in context."
37+
},
38+
"comment": {
39+
"type": "string",
40+
"description": "A user-facing \u0060comment\u0060"
41+
}
42+
},
43+
"required": [
44+
"$type",
45+
"id",
46+
"comment"
47+
],
48+
"additionalProperties": false
49+
},
50+
{
51+
"type": "object",
52+
"description": "Add a reference to your statement.",
53+
"properties": {
54+
"$type": {
55+
"type": "string",
56+
"const": "citation"
57+
},
58+
"title": {
59+
"type": "string"
60+
},
61+
"url": {
62+
"type": "string",
63+
"description": "URL from context."
64+
}
65+
},
66+
"required": [
67+
"$type",
68+
"title",
69+
"url"
70+
],
71+
"additionalProperties": false
72+
}
73+
]
74+
}
75+
}
76+
},
77+
"required": [
78+
"answer"
79+
],
80+
"additionalProperties": false
81+
}

OpenAi.JsonSchema/Fluent/FluentSchemaBuilder.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ public SchemaNode Object<T>(Action<IFluentObjectSchemaBuilder<T>> properties) wh
8686
return node;
8787
}
8888

89+
public SchemaNode Array(string description, Func<IFluentSchemaBuilder, SchemaNode> items)
90+
{
91+
var itemsType = items(this);
92+
return new SchemaArrayNode(itemsType) {
93+
Description = description
94+
};
95+
}
96+
97+
public SchemaNode Array(Func<IFluentSchemaBuilder, SchemaNode> items)
98+
{
99+
var itemsType = items(this);
100+
return new SchemaArrayNode(itemsType);
101+
}
102+
89103
public SchemaNode Array<T>()
90104
{
91105
return Schema(Resolve(typeof(T)));

OpenAi.JsonSchema/Fluent/IFluentSchemaBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public interface IFluentSchemaBuilder {
1616
SchemaNode Object<T>(string description, Action<IFluentObjectSchemaBuilder<T>> properties) where T : class;
1717
SchemaNode Object<T>(Action<IFluentObjectSchemaBuilder<T>> properties) where T : class;
1818

19+
SchemaNode Array(string description, Func<IFluentSchemaBuilder, SchemaNode> properties);
20+
SchemaNode Array(Func<IFluentSchemaBuilder, SchemaNode> properties);
21+
1922
SchemaNode Array<T>();
2023
SchemaNode Array<T>(string description);
2124
SchemaNode Array<T>(string description, Action<IFluentObjectSchemaBuilder<T>> properties) where T : class;

0 commit comments

Comments
 (0)