1
1
// Copyright (c) Microsoft. All rights reserved.
2
- using System . ComponentModel ;
3
- using System . Reflection ;
4
2
using System . Text . Json ;
5
- using System . Text . Json . Nodes ;
6
3
using System . Text . Json . Serialization ;
7
- using JsonSchemaMapper ;
4
+ using Microsoft . Extensions . AI ;
8
5
using Microsoft . SemanticKernel ;
9
6
10
7
namespace Step04 ;
@@ -14,55 +11,18 @@ internal static class JsonSchemaGenerator
14
11
/// <summary>
15
12
/// Wrapper for generating a JSON schema as string from a .NET type.
16
13
/// </summary>
17
- public static string FromType < SchemaType > ( )
14
+ public static string FromType < TSchemaType > ( )
18
15
{
19
16
JsonSerializerOptions options = new ( JsonSerializerOptions . Default )
20
17
{
21
18
UnmappedMemberHandling = JsonUnmappedMemberHandling . Disallow ,
22
19
} ;
23
- JsonSchemaMapperConfiguration config = new ( )
20
+ AIJsonSchemaCreateOptions config = new ( )
24
21
{
25
- TreatNullObliviousAsNonNullable = true ,
26
- TransformSchemaNode = ( context , schema ) =>
27
- {
28
- // NOTE: This can be replaced with `IncludeAdditionalProperties = false` when upgraded to System.Json.Text 9.0.0
29
- if ( context . TypeInfo . Type == typeof ( SchemaType ) )
30
- {
31
- schema [ "additionalProperties" ] = false ;
32
- }
33
-
34
- // Determine if a type or property and extract the relevant attribute provider
35
- ICustomAttributeProvider ? attributeProvider = context . PropertyInfo is not null
36
- ? context . PropertyInfo . AttributeProvider
37
- : context . TypeInfo . Type ;
38
-
39
- // Look up any description attributes
40
- DescriptionAttribute ? descriptionAttr = attributeProvider ?
41
- . GetCustomAttributes ( inherit : true )
42
- . Select ( attr => attr as DescriptionAttribute )
43
- . FirstOrDefault ( attr => attr is not null ) ;
44
-
45
- // Apply description attribute to the generated schema
46
- if ( descriptionAttr != null )
47
- {
48
- if ( schema is not JsonObject jObj )
49
- {
50
- // Handle the case where the schema is a boolean
51
- JsonValueKind valueKind = schema . GetValueKind ( ) ;
52
- schema = jObj = new JsonObject ( ) ;
53
- if ( valueKind is JsonValueKind . False )
54
- {
55
- jObj . Add ( "not" , true ) ;
56
- }
57
- }
58
-
59
- jObj [ "description" ] = descriptionAttr . Description ;
60
- }
61
-
62
- return schema ;
63
- }
22
+ IncludeSchemaKeyword = false ,
23
+ DisallowAdditionalProperties = true ,
64
24
} ;
65
25
66
- return KernelJsonSchemaBuilder . Build ( typeof ( SchemaType ) , "Intent Result" , config ) . AsJson ( ) ;
26
+ return KernelJsonSchemaBuilder . Build ( typeof ( TSchemaType ) , "Intent Result" , config ) . AsJson ( ) ;
67
27
}
68
28
}
0 commit comments