44using  System . Text ; 
55using  System . Text . RegularExpressions ; 
66using  Humanizer ; 
7- using  Json . Schema ; 
8- using  Json . Schema . OpenApi ; 
7+ using  Humanizer . Inflections ; 
98using  Microsoft . OpenApi . Hidi . Extensions ; 
109using  Microsoft . OpenApi . Models ; 
1110using  Microsoft . OpenApi . Services ; 
12- using  Microsoft . OpenApi . Extensions ; 
1311
1412namespace  Microsoft . OpenApi . Hidi . Formatters 
1513{ 
1614    internal  class  PowerShellFormatter  :  OpenApiVisitorBase 
1715    { 
1816        private  const  string  DefaultPutPrefix  =  ".Update" ; 
1917        private  const  string  PowerShellPutPrefix  =  ".Set" ; 
20-         private  readonly  Stack < JsonSchema >  _schemaLoop  =  new ( ) ; 
18+         private  readonly  Stack < OpenApiSchema >  _schemaLoop  =  new ( ) ; 
2119        private  static readonly  Regex  s_oDataCastRegex  =  new ( "(.*(?<=[a-z]))\\ .(As(?=[A-Z]).*)" ,  RegexOptions . Compiled ,  TimeSpan . FromSeconds ( 5 ) ) ; 
2220        private  static readonly  Regex  s_hashSuffixRegex  =  new ( @"^[^-]+" ,  RegexOptions . Compiled ,  TimeSpan . FromSeconds ( 5 ) ) ; 
2321        private  static readonly  Regex  s_oDataRefRegex  =  new ( "(?<=[a-z])Ref(?=[A-Z])" ,  RegexOptions . Compiled ,  TimeSpan . FromSeconds ( 5 ) ) ; 
@@ -26,11 +24,11 @@ static PowerShellFormatter()
2624        { 
2725            // Add singularization exclusions. 
2826            // Enhancement: Read exclusions from a user provided file. 
29-             Humanizer . Inflections . Vocabularies . Default . AddSingular ( "(drive)s$" ,  "$1" ) ;  // drives does not properly singularize to drive. 
30-             Humanizer . Inflections . Vocabularies . Default . AddSingular ( "(data)$" ,  "$1" ) ;  // exclude the following from singularization. 
31-             Humanizer . Inflections . Vocabularies . Default . AddSingular ( "(delta)$" ,  "$1" ) ; 
32-             Humanizer . Inflections . Vocabularies . Default . AddSingular ( "(quota)$" ,  "$1" ) ; 
33-             Humanizer . Inflections . Vocabularies . Default . AddSingular ( "(statistics)$" ,  "$1" ) ; 
27+             Vocabularies . Default . AddSingular ( "(drive)s$" ,  "$1" ) ;  // drives does not properly singularize to drive. 
28+             Vocabularies . Default . AddSingular ( "(data)$" ,  "$1" ) ;  // exclude the following from singularization. 
29+             Vocabularies . Default . AddSingular ( "(delta)$" ,  "$1" ) ; 
30+             Vocabularies . Default . AddSingular ( "(quota)$" ,  "$1" ) ; 
31+             Vocabularies . Default . AddSingular ( "(statistics)$" ,  "$1" ) ; 
3432        } 
3533
3634        //FHL task for PS 
@@ -43,13 +41,13 @@ static PowerShellFormatter()
4341        // 5. Fix anyOf and oneOf schema. 
4442        // 6. Add AdditionalProperties to object schemas. 
4543
46-         public  override  void  Visit ( ref   JsonSchema  schema ) 
47-           { 
48-             AddAdditionalPropertiesToSchema ( ref   schema ) ; 
49-             schema   =   ResolveAnyOfSchema ( ref   schema ) ; 
50-             schema   =   ResolveOneOfSchema ( ref   schema ) ; 
44+         public  override  void  Visit ( OpenApiSchema  schema ) 
45+         { 
46+             AddAdditionalPropertiesToSchema ( schema ) ; 
47+             ResolveAnyOfSchema ( schema ) ; 
48+             ResolveOneOfSchema ( schema ) ; 
5149
52-             base . Visit ( ref   schema ) ; 
50+             base . Visit ( schema ) ; 
5351        } 
5452
5553        public  override  void  Visit ( OpenApiPathItem  pathItem ) 
@@ -166,237 +164,97 @@ private static IList<OpenApiParameter> ResolveFunctionParameters(IList<OpenApiPa
166164                // Replace content with a schema object of type array 
167165                // for structured or collection-valued function parameters 
168166                parameter . Content  =  null ; 
169-                 parameter . Schema  =  new  JsonSchemaBuilder ( ) 
170-                     . Type ( SchemaValueType . Array ) 
171-                     . Items ( new  JsonSchemaBuilder ( ) 
172-                         . Type ( SchemaValueType . String ) ) 
173-                 ; 
167+                 parameter . Schema  =  new ( ) 
168+                 { 
169+                     Type  =  "array" , 
170+                     Items  =  new ( ) 
171+                     { 
172+                         Type  =  "string" 
173+                     } 
174+                 } ; 
174175            } 
175176            return  parameters ; 
176177        } 
177178
178-         private  void  AddAdditionalPropertiesToSchema ( ref   JsonSchema  schema ) 
179+         private  void  AddAdditionalPropertiesToSchema ( OpenApiSchema  schema ) 
179180        { 
180-             if  ( schema  !=  null  &&  ! _schemaLoop . Contains ( schema )  &&  schema . GetJsonType ( ) . Equals ( SchemaValueType . Object ) ) 
181+             if  ( schema  !=  null  &&  ! _schemaLoop . Contains ( schema )  &&  "object" . Equals ( ( string ) schema . Type ,   StringComparison . OrdinalIgnoreCase ) ) 
181182            { 
182-                 var  schemaBuilder  =  new  JsonSchemaBuilder ( ) ; 
183-                 if  ( schema . Keywords  !=  null ) 
184-                 { 
185-                     foreach  ( var  keyword  in  schema . Keywords ) 
186-                     { 
187-                         schemaBuilder . Add ( keyword ) ; 
188-                     } 
189-                 } 
190- 
191-                 schema  =  schemaBuilder . AdditionalProperties ( new  JsonSchemaBuilder ( ) . Type ( SchemaValueType . Object ) ) ; 
183+                 schema . AdditionalProperties  =  new ( )  {  Type  =  "object"  } ; 
192184
193185                /* Because 'additionalProperties' are now being walked, 
194186                 * we need a way to keep track of visited schemas to avoid 
195187                 * endlessly creating and walking them in an infinite recursion. 
196188                 */ 
197-                 var  additionalProps  =  schema . GetAdditionalProperties ( ) ; 
198- 
199-                 if  ( additionalProps  !=  null ) 
200-                 { 
201-                     _schemaLoop . Push ( additionalProps ) ; 
202-                 } 
189+                 _schemaLoop . Push ( schema . AdditionalProperties ) ; 
203190            } 
204-             
205191        } 
206192
207-         private  static JsonSchema  ResolveOneOfSchema ( ref   JsonSchema  schema ) 
193+         private  static void  ResolveOneOfSchema ( OpenApiSchema  schema ) 
208194        { 
209-             if  ( schema . GetOneOf ( ) ? . FirstOrDefault ( )  is  { }  newSchema ) 
195+             if  ( schema . OneOf ? . FirstOrDefault ( )  is  {   }  newSchema ) 
210196            { 
211-                 var  schemaBuilder  =  BuildSchema ( schema ) ; 
212-                 schemaBuilder  =  schemaBuilder . Remove ( "oneOf" ) ; 
213-                 schema  =  schemaBuilder . Build ( ) ; 
214- 
215-                 schema  =  FlattenSchema ( schema ,  newSchema ) ; 
197+                 schema . OneOf  =  null ; 
198+                 FlattenSchema ( schema ,  newSchema ) ; 
216199            } 
217- 
218-             return  schema ; 
219200        } 
220201
221-         private  static JsonSchema  ResolveAnyOfSchema ( ref   JsonSchema  schema ) 
202+         private  static void  ResolveAnyOfSchema ( OpenApiSchema  schema ) 
222203        { 
223-             if  ( schema . GetAnyOf ( ) ? . FirstOrDefault ( )  is  { }  newSchema ) 
204+             if  ( schema . AnyOf ? . FirstOrDefault ( )  is  {   }  newSchema ) 
224205            { 
225-                 var  schemaBuilder  =  BuildSchema ( schema ) ; 
226-                 schemaBuilder  =  schemaBuilder . Remove ( "anyOf" ) ; 
227-                 schema  =  schemaBuilder . Build ( ) ; 
228- 
229-                 schema  =  FlattenSchema ( schema ,  newSchema ) ; 
206+                 schema . AnyOf  =  null ; 
207+                 FlattenSchema ( schema ,  newSchema ) ; 
230208            } 
231- 
232-             return  schema ; 
233209        } 
234210
235-         private  static JsonSchema  FlattenSchema ( JsonSchema  schema ,  JsonSchema  newSchema ) 
211+         private  static void  FlattenSchema ( OpenApiSchema  schema ,  OpenApiSchema  newSchema ) 
236212        { 
237213            if  ( newSchema  !=  null ) 
238214            { 
239-                 var  newSchemaRef  =  newSchema . GetRef ( ) ; 
240- 
241-                 if  ( newSchemaRef  !=  null ) 
215+                 if  ( newSchema . Reference  !=  null ) 
242216                { 
243-                     var   schemaBuilder   =   BuildSchema ( schema ) ; 
244-                     schema  =  schemaBuilder . Ref ( newSchemaRef ) ; 
217+                     schema . Reference   =   newSchema . Reference ; 
218+                     schema . UnresolvedReference  =  true ; 
245219                } 
246220                else 
247221                { 
248222                    // Copies schema properties based on https://github.com/microsoft/OpenAPI.NET.OData/pull/264. 
249-                     schema  =  CopySchema ( schema ,  newSchema ) ; 
250-                 } 
251-             } 
252- 
253-             return  schema ; 
254-         } 
255- 
256-         private  static JsonSchema  CopySchema ( JsonSchema  schema ,  JsonSchema  newSchema ) 
257-         { 
258-             var  schemaBuilder  =  new  JsonSchemaBuilder ( ) ; 
259-             var  keywords  =  schema . Keywords ; 
260-             if  ( keywords  !=  null ) 
261-             { 
262-                 foreach  ( var  keyword  in  keywords ) 
263-                 { 
264-                     schemaBuilder . Add ( keyword ) ; 
223+                     CopySchema ( schema ,  newSchema ) ; 
265224                } 
266225            } 
267- 
268-             if  ( schema . GetTitle ( )  ==  null  &&  newSchema . GetTitle ( )  is  {  }  title ) 
269-             { 
270-                 schemaBuilder . Title ( title ) ; 
271-             } 
272-             if  ( schema . GetJsonType ( )  ==  null  &&  newSchema . GetJsonType ( )  is  {  }  type ) 
273-             { 
274-                 schemaBuilder . Type ( type ) ; 
275-             } 
276-             if  ( schema . GetFormat ( )  ==  null  &&  newSchema . GetFormat ( )  is  {  }  format ) 
277-             { 
278-                 schemaBuilder . Format ( format ) ; 
279-             } 
280-             if  ( schema . GetDescription ( )  ==  null  &&  newSchema . GetDescription ( )  is  {  }  description ) 
281-             { 
282-                 schemaBuilder . Description ( description ) ; 
283-             } 
284-             if  ( schema . GetMaximum ( )  ==  null  &&  newSchema . GetMaximum ( )  is  {  }  max ) 
285-             { 
286-                 schemaBuilder . Maximum ( max ) ; 
287-             } 
288-             if  ( schema . GetExclusiveMaximum ( )  ==  null  &&  newSchema . GetExclusiveMaximum ( )  is  {  }  exclusiveMaximum ) 
289-             { 
290-                 schemaBuilder . ExclusiveMaximum ( exclusiveMaximum ) ; 
291-             } 
292-             if  ( schema . GetMinimum ( )  ==  null  &&  newSchema . GetMinimum ( )  is  {  }  min ) 
293-             { 
294-                 schemaBuilder . Minimum ( min ) ; 
295-             } 
296-             if  ( schema . GetExclusiveMinimum ( )  ==  null  &&  newSchema . GetExclusiveMinimum ( )  is  {  }  exclusiveMinimum ) 
297-             { 
298-                 schemaBuilder . ExclusiveMinimum ( exclusiveMinimum ) ; 
299-             } 
300-             if  ( schema . GetMaxLength ( )  ==  null  &&  newSchema . GetMaxLength ( )  is  {  }  maxLength ) 
301-             { 
302-                 schemaBuilder . MaxLength ( maxLength ) ; 
303-             } 
304-             if  ( schema . GetMinLength ( )  ==  null  &&  newSchema . GetMinLength ( )  is  {  }  minLength ) 
305-             { 
306-                 schemaBuilder . MinLength ( minLength ) ; 
307-             } 
308-             if  ( schema . GetPattern ( )  ==  null  &&  newSchema . GetPattern ( )  is  {  }  pattern ) 
309-             { 
310-                 schemaBuilder . Pattern ( pattern ) ; 
311-             } 
312-             if  ( schema . GetMultipleOf ( )  ==  null  &&  newSchema . GetMultipleOf ( )  is  {  }  multipleOf ) 
313-             { 
314-                 schemaBuilder . MultipleOf ( multipleOf ) ; 
315-             } 
316-             if  ( schema . GetNot ( )  ==  null  &&  newSchema . GetNot ( )  is  {  }  not ) 
317-             { 
318-                 schemaBuilder . Not ( not ) ; 
319-             } 
320-             if  ( schema . GetRequired ( )  ==  null  &&  newSchema . GetRequired ( )  is  {  }  required ) 
321-             { 
322-                 schemaBuilder . Required ( required ) ; 
323-             } 
324-             if  ( schema . GetItems ( )  ==  null  &&  newSchema . GetItems ( )  is  {  }  items ) 
325-             { 
326-                 schemaBuilder . Items ( items ) ; 
327-             } 
328-             if  ( schema . GetMaxItems ( )  ==  null  &&  newSchema . GetMaxItems ( )  is  {  }  maxItems ) 
329-             { 
330-                 schemaBuilder . MaxItems ( maxItems ) ; 
331-             } 
332-             if  ( schema . GetMinItems ( )  ==  null  &&  newSchema . GetMinItems ( )  is  {  }  minItems ) 
333-             { 
334-                 schemaBuilder . MinItems ( minItems ) ; 
335-             } 
336-             if  ( schema . GetUniqueItems ( )  ==  null  &&  newSchema . GetUniqueItems ( )  is  {  }  uniqueItems ) 
337-             { 
338-                 schemaBuilder . UniqueItems ( uniqueItems ) ; 
339-             } 
340-             if  ( schema . GetProperties ( )  ==  null  &&  newSchema . GetProperties ( )  is  {  }  properties ) 
341-             { 
342-                 schemaBuilder . Properties ( properties ) ; 
343-             } 
344-             if  ( schema . GetMaxProperties ( )  ==  null  &&  newSchema . GetMaxProperties ( )  is  {  }  maxProperties ) 
345-             { 
346-                 schemaBuilder . MaxProperties ( maxProperties ) ; 
347-             } 
348-             if  ( schema . GetMinProperties ( )  ==  null  &&  newSchema . GetMinProperties ( )  is  {  }  minProperties ) 
349-             { 
350-                 schemaBuilder . MinProperties ( minProperties ) ; 
351-             } 
352-             if  ( schema . GetDiscriminator ( )  ==  null  &&  newSchema . GetDiscriminator ( )  is  {  }  discriminator ) 
353-             { 
354-                 schemaBuilder . Discriminator ( discriminator . PropertyName ,  discriminator . Mapping ,  discriminator . Extensions ) ; 
355-             } 
356-             if  ( schema . GetOpenApiExternalDocs ( )  ==  null  &&  newSchema . GetOpenApiExternalDocs ( )  is  {  }  externalDocs ) 
357-             { 
358-                 schemaBuilder . OpenApiExternalDocs ( externalDocs ) ; 
359-             } 
360-             if  ( schema . GetEnum ( )  ==  null  &&  newSchema . GetEnum ( )  is  {  }  enumCollection ) 
361-             { 
362-                 schemaBuilder . Enum ( enumCollection ) ; 
363-             } 
364- 
365-             if  ( ! schema . GetReadOnly ( )  is  {  }  &&  newSchema . GetReadOnly ( )  is  {  }  newValue ) 
366-             { 
367-                 schemaBuilder . ReadOnly ( newValue ) ; 
368-             } 
369- 
370-             if  ( ! schema . GetWriteOnly ( )  is  {  }  &&  newSchema . GetWriteOnly ( )  is  {  }  newWriteOnlyValue ) 
371-             { 
372-                 schemaBuilder . WriteOnly ( newWriteOnlyValue ) ; 
373-             } 
374- 
375-             if  ( ! schema . GetNullable ( )  is  {  }  &&  newSchema . GetNullable ( )  is  {  }  newNullableValue ) 
376-             { 
377-                 schemaBuilder . Nullable ( newNullableValue ) ; 
378-             } 
379- 
380-             if  ( ! schema . GetDeprecated ( )  is  {  }  &&  newSchema . GetDeprecated ( )  is  {  }  newDepracatedValue ) 
381-             { 
382-                 schemaBuilder . Deprecated ( newDepracatedValue ) ; 
383-             } 
384- 
385-             return  schemaBuilder ;             
386226        } 
387227
388-         private  static JsonSchemaBuilder   BuildSchema ( JsonSchema  schema ) 
228+         private  static void   CopySchema ( OpenApiSchema  schema ,   OpenApiSchema   newSchema ) 
389229        { 
390-             var  schemaBuilder  =  new  JsonSchemaBuilder ( ) ; 
391-             if  ( schema . Keywords  !=  null ) 
392-             { 
393-                 foreach  ( var  keyword  in  schema . Keywords ) 
394-                 { 
395-                     schemaBuilder . Add ( keyword ) ; 
396-                 } 
397-             } 
398- 
399-             return  schemaBuilder ; 
230+             schema . Title  ??=  newSchema . Title ; 
231+             schema . Type  ??=  newSchema . Type ; 
232+             schema . Format  ??=  newSchema . Format ; 
233+             schema . Description  ??=  newSchema . Description ; 
234+             schema . Maximum  ??=  newSchema . Maximum ; 
235+             schema . ExclusiveMaximum  ??=  newSchema . ExclusiveMaximum ; 
236+             schema . Minimum  ??=  newSchema . Minimum ; 
237+             schema . ExclusiveMinimum  ??=  newSchema . ExclusiveMinimum ; 
238+             schema . MaxLength  ??=  newSchema . MaxLength ; 
239+             schema . MinLength  ??=  newSchema . MinLength ; 
240+             schema . Pattern  ??=  newSchema . Pattern ; 
241+             schema . MultipleOf  ??=  newSchema . MultipleOf ; 
242+             schema . Not  ??=  newSchema . Not ; 
243+             schema . Required  ??=  newSchema . Required ; 
244+             schema . Items  ??=  newSchema . Items ; 
245+             schema . MaxItems  ??=  newSchema . MaxItems ; 
246+             schema . MinItems  ??=  newSchema . MinItems ; 
247+             schema . UniqueItems  ??=  newSchema . UniqueItems ; 
248+             schema . Properties  ??=  newSchema . Properties ; 
249+             schema . MaxProperties  ??=  newSchema . MaxProperties ; 
250+             schema . MinProperties  ??=  newSchema . MinProperties ; 
251+             schema . Discriminator  ??=  newSchema . Discriminator ; 
252+             schema . ExternalDocs  ??=  newSchema . ExternalDocs ; 
253+             schema . Enum  ??=  newSchema . Enum ; 
254+             schema . ReadOnly  =  ! schema . ReadOnly  ?  newSchema . ReadOnly  :  schema . ReadOnly ; 
255+             schema . WriteOnly  =  ! schema . WriteOnly  ?  newSchema . WriteOnly  :  schema . WriteOnly ; 
256+             schema . Nullable  =  ! schema . Nullable  ?  newSchema . Nullable  :  schema . Nullable ; 
257+             schema . Deprecated  =  ! schema . Deprecated  ?  newSchema . Deprecated  :  schema . Deprecated ; 
400258        } 
401259    } 
402260} 
0 commit comments