44
55using System ;
66using System . ClientModel . Primitives ;
7- using System . Collections . Generic ;
7+ using System . Text ;
88using System . Text . Json ;
99using OpenAI ;
1010
@@ -14,6 +14,14 @@ internal partial class InternalMCPToolRequireApproval1 : IJsonModel<InternalMCPT
1414 {
1515 void IJsonModel < InternalMCPToolRequireApproval1 > . Write ( Utf8JsonWriter writer , ModelReaderWriterOptions options )
1616 {
17+ #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
18+ if ( Patch . Contains ( "$"u8 ) )
19+ {
20+ writer . WriteRawValue ( Patch . GetJson ( "$"u8 ) ) ;
21+ return ;
22+ }
23+ #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
24+
1725 writer . WriteStartObject ( ) ;
1826 JsonModelWriteCore ( writer , options ) ;
1927 writer . WriteEndObject ( ) ;
@@ -26,36 +34,20 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
2634 {
2735 throw new FormatException ( $ "The model { nameof ( InternalMCPToolRequireApproval1 ) } does not support writing '{ format } ' format.") ;
2836 }
29- if ( Optional . IsDefined ( Always ) && _additionalBinaryDataProperties ? . ContainsKey ( "always" ) != true )
37+ #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
38+ if ( Optional . IsDefined ( Always ) && ! Patch . Contains ( "$.always"u8 ) )
3039 {
3140 writer . WritePropertyName ( "always"u8 ) ;
3241 writer . WriteObjectValue ( Always , options ) ;
3342 }
34- if ( Optional . IsDefined ( Never ) && _additionalBinaryDataProperties ? . ContainsKey ( " never") != true )
43+ if ( Optional . IsDefined ( Never ) && ! Patch . Contains ( "$. never"u8 ) )
3544 {
3645 writer . WritePropertyName ( "never"u8 ) ;
3746 writer . WriteObjectValue ( Never , options ) ;
3847 }
39- // Plugin customization: remove options.Format != "W" check
40- if ( _additionalBinaryDataProperties != null )
41- {
42- foreach ( var item in _additionalBinaryDataProperties )
43- {
44- if ( ModelSerializationExtensions . IsSentinelValue ( item . Value ) )
45- {
46- continue ;
47- }
48- writer . WritePropertyName ( item . Key ) ;
49- #if NET6_0_OR_GREATER
50- writer . WriteRawValue ( item . Value ) ;
51- #else
52- using ( JsonDocument document = JsonDocument . Parse ( item . Value ) )
53- {
54- JsonSerializer . Serialize ( writer , document . RootElement ) ;
55- }
56- #endif
57- }
58- }
48+
49+ Patch . WriteTo ( writer ) ;
50+ #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
5951 }
6052
6153 InternalMCPToolRequireApproval1 IJsonModel < InternalMCPToolRequireApproval1 > . Create ( ref Utf8JsonReader reader , ModelReaderWriterOptions options ) => JsonModelCreateCore ( ref reader , options ) ;
@@ -68,18 +60,20 @@ protected virtual InternalMCPToolRequireApproval1 JsonModelCreateCore(ref Utf8Js
6860 throw new FormatException ( $ "The model { nameof ( InternalMCPToolRequireApproval1 ) } does not support reading '{ format } ' format.") ;
6961 }
7062 using JsonDocument document = JsonDocument . ParseValue ( ref reader ) ;
71- return DeserializeInternalMCPToolRequireApproval1 ( document . RootElement , options ) ;
63+ return DeserializeInternalMCPToolRequireApproval1 ( document . RootElement , null , options ) ;
7264 }
7365
74- internal static InternalMCPToolRequireApproval1 DeserializeInternalMCPToolRequireApproval1 ( JsonElement element , ModelReaderWriterOptions options )
66+ internal static InternalMCPToolRequireApproval1 DeserializeInternalMCPToolRequireApproval1 ( JsonElement element , BinaryData data , ModelReaderWriterOptions options )
7567 {
7668 if ( element . ValueKind == JsonValueKind . Null )
7769 {
7870 return null ;
7971 }
8072 McpToolFilter always = default ;
8173 McpToolFilter never = default ;
82- IDictionary < string , BinaryData > additionalBinaryDataProperties = new ChangeTrackingDictionary < string , BinaryData > ( ) ;
74+ #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
75+ JsonPatch patch = new JsonPatch ( data is null ? ReadOnlyMemory < byte > . Empty : data . ToMemory ( ) ) ;
76+ #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
8377 foreach ( var prop in element . EnumerateObject ( ) )
8478 {
8579 if ( prop . NameEquals ( "always"u8 ) )
@@ -100,10 +94,9 @@ internal static InternalMCPToolRequireApproval1 DeserializeInternalMCPToolRequir
10094 never = McpToolFilter . DeserializeMcpToolFilter ( prop . Value , prop . Value . GetUtf8Bytes ( ) , options ) ;
10195 continue ;
10296 }
103- // Plugin customization: remove options.Format != "W" check
104- additionalBinaryDataProperties . Add ( prop . Name , BinaryData . FromString ( prop . Value . GetRawText ( ) ) ) ;
97+ patch . Set ( [ .. "$."u8 , .. Encoding . UTF8 . GetBytes ( prop . Name ) ] , prop . Value . GetUtf8Bytes ( ) ) ;
10598 }
106- return new InternalMCPToolRequireApproval1 ( always , never , additionalBinaryDataProperties ) ;
99+ return new InternalMCPToolRequireApproval1 ( always , never , patch ) ;
107100 }
108101
109102 BinaryData IPersistableModel < InternalMCPToolRequireApproval1 > . Write ( ModelReaderWriterOptions options ) => PersistableModelWriteCore ( options ) ;
@@ -130,13 +123,50 @@ protected virtual InternalMCPToolRequireApproval1 PersistableModelCreateCore(Bin
130123 case "J" :
131124 using ( JsonDocument document = JsonDocument . Parse ( data ) )
132125 {
133- return DeserializeInternalMCPToolRequireApproval1 ( document . RootElement , options ) ;
126+ return DeserializeInternalMCPToolRequireApproval1 ( document . RootElement , data , options ) ;
134127 }
135128 default :
136129 throw new FormatException ( $ "The model { nameof ( InternalMCPToolRequireApproval1 ) } does not support reading '{ options . Format } ' format.") ;
137130 }
138131 }
139132
140133 string IPersistableModel < InternalMCPToolRequireApproval1 > . GetFormatFromOptions ( ModelReaderWriterOptions options ) => "J" ;
134+
135+ #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
136+ private bool PropagateGet ( ReadOnlySpan < byte > jsonPath , out JsonPatch . EncodedValue value )
137+ {
138+ ReadOnlySpan < byte > local = jsonPath . SliceToStartOfPropertyName ( ) ;
139+ value = default ;
140+
141+ if ( local . StartsWith ( "always"u8 ) )
142+ {
143+ return Always . Patch . TryGetEncodedValue ( [ .. "$"u8 , .. local . Slice ( "always"u8 . Length ) ] , out value ) ;
144+ }
145+ if ( local . StartsWith ( "never"u8 ) )
146+ {
147+ return Never . Patch . TryGetEncodedValue ( [ .. "$"u8 , .. local . Slice ( "never"u8 . Length ) ] , out value ) ;
148+ }
149+ return false ;
150+ }
151+ #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
152+
153+ #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
154+ private bool PropagateSet ( ReadOnlySpan < byte > jsonPath , JsonPatch . EncodedValue value )
155+ {
156+ ReadOnlySpan < byte > local = jsonPath . SliceToStartOfPropertyName ( ) ;
157+
158+ if ( local . StartsWith ( "always"u8 ) )
159+ {
160+ Always . Patch . Set ( [ .. "$"u8 , .. local . Slice ( "always"u8 . Length ) ] , value ) ;
161+ return true ;
162+ }
163+ if ( local . StartsWith ( "never"u8 ) )
164+ {
165+ Never . Patch . Set ( [ .. "$"u8 , .. local . Slice ( "never"u8 . Length ) ] , value ) ;
166+ return true ;
167+ }
168+ return false ;
169+ }
170+ #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
141171 }
142172}
0 commit comments