33
44using System ;
55using System . Collections . Generic ;
6+ using System . Linq ;
67using System . Net . Http ;
78
89namespace Microsoft . OpenApi
@@ -97,15 +98,18 @@ public virtual void SerializeAsV2(IOpenApiWriter writer)
9798 // operations except "trace"
9899 if ( Operations != null )
99100 {
100- foreach ( var operation in Operations )
101+
102+ foreach ( var operation in Operations . Where ( o => _standardHttp2MethodsNames . Contains ( o . Key . Method , StringComparer . OrdinalIgnoreCase ) ) )
101103 {
102- if ( operation . Key != HttpMethod . Trace )
103- {
104- writer . WriteOptionalObject (
105- operation . Key . Method . ToLowerInvariant ( ) ,
106- operation . Value ,
107- ( w , o ) => o . SerializeAsV2 ( w ) ) ;
108- }
104+ writer . WriteOptionalObject (
105+ operation . Key . Method . ToLowerInvariant ( ) ,
106+ operation . Value ,
107+ ( w , o ) => o . SerializeAsV2 ( w ) ) ;
108+ }
109+ var nonStandardOperations = Operations . Where ( o => ! _standardHttp2MethodsNames . Contains ( o . Key . Method , StringComparer . OrdinalIgnoreCase ) ) . ToDictionary ( static o => o . Key . Method , static o => o . Value ) ;
110+ if ( nonStandardOperations . Count > 0 )
111+ {
112+ writer . WriteRequiredMap ( $ "x-oai-{ OpenApiConstants . AdditionalOperations } ", nonStandardOperations , ( w , o ) => o . SerializeAsV2 ( w ) ) ;
109113 }
110114 }
111115
@@ -126,6 +130,31 @@ public virtual void SerializeAsV2(IOpenApiWriter writer)
126130 writer . WriteEndObject ( ) ;
127131 }
128132
133+ internal static readonly HashSet < string > _standardHttp2MethodsNames = new ( StringComparer . OrdinalIgnoreCase )
134+ {
135+ "get" ,
136+ "put" ,
137+ "post" ,
138+ "delete" ,
139+ "options" ,
140+ "head" ,
141+ "patch" ,
142+ } ;
143+
144+ internal static readonly HashSet < string > _standardHttp30MethodsNames = new ( _standardHttp2MethodsNames , StringComparer . OrdinalIgnoreCase )
145+ {
146+ "trace" ,
147+ } ;
148+
149+ internal static readonly HashSet < string > _standardHttp31MethodsNames = new ( _standardHttp30MethodsNames , StringComparer . OrdinalIgnoreCase )
150+ {
151+ } ;
152+
153+ internal static readonly HashSet < string > _standardHttp32MethodsNames = new ( _standardHttp31MethodsNames , StringComparer . OrdinalIgnoreCase )
154+ {
155+ "query" ,
156+ } ;
157+
129158 internal virtual void SerializeInternal ( IOpenApiWriter writer , OpenApiSpecVersion version ,
130159 Action < IOpenApiWriter , IOpenApiSerializable > callback )
131160 {
@@ -139,16 +168,35 @@ internal virtual void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersio
139168 // description
140169 writer . WriteProperty ( OpenApiConstants . Description , Description ) ;
141170
171+ var standardMethodsNames = version switch
172+ {
173+ OpenApiSpecVersion . OpenApi2_0 => _standardHttp2MethodsNames ,
174+ OpenApiSpecVersion . OpenApi3_0 => _standardHttp30MethodsNames ,
175+ OpenApiSpecVersion . OpenApi3_1 => _standardHttp31MethodsNames ,
176+ OpenApiSpecVersion . OpenApi3_2 or _ => _standardHttp32MethodsNames ,
177+ } ;
178+
142179 // operations
143180 if ( Operations != null )
144181 {
145- foreach ( var operation in Operations )
182+ foreach ( var operation in Operations . Where ( o => standardMethodsNames . Contains ( o . Key . Method , StringComparer . OrdinalIgnoreCase ) ) )
146183 {
147184 writer . WriteOptionalObject (
148185 operation . Key . Method . ToLowerInvariant ( ) ,
149186 operation . Value ,
150187 callback ) ;
151188 }
189+ var nonStandardOperations = Operations . Where ( o => ! standardMethodsNames . Contains ( o . Key . Method , StringComparer . OrdinalIgnoreCase ) ) . ToDictionary ( static o => o . Key . Method , static o => o . Value ) ;
190+ if ( nonStandardOperations . Count > 0 )
191+ {
192+ var additionalOperationsPropertyName = version switch
193+ {
194+ OpenApiSpecVersion . OpenApi2_0 or OpenApiSpecVersion . OpenApi3_0 or OpenApiSpecVersion . OpenApi3_1 =>
195+ $ "x-oai-{ OpenApiConstants . AdditionalOperations } ",
196+ _ => OpenApiConstants . AdditionalOperations ,
197+ } ;
198+ writer . WriteRequiredMap ( additionalOperationsPropertyName , nonStandardOperations , ( w , o ) => o . SerializeAsV32 ( w ) ) ;
199+ }
152200 }
153201
154202 // servers
0 commit comments