55using System . Collections . Generic ;
66using System . Linq ;
77using Microsoft . OpenApi . Interfaces ;
8+ using Microsoft . OpenApi . Models . References ;
89using Microsoft . OpenApi . Writers ;
910
1011
@@ -120,11 +121,9 @@ public void SerializeAsV31(IOpenApiWriter writer)
120121 PathItems ,
121122 ( w , key , component ) =>
122123 {
123- if ( component . Reference != null &&
124- component . Reference . Type == ReferenceType . Schema &&
125- component . Reference . Id == key )
124+ if ( component is OpenApiPathItemReference reference )
126125 {
127- component . SerializeAsV31WithoutReference ( w ) ;
126+ reference . SerializeAsV31 ( w ) ;
128127 }
129128 else
130129 {
@@ -133,7 +132,7 @@ public void SerializeAsV31(IOpenApiWriter writer)
133132 } ) ;
134133
135134 SerializeInternal ( writer , OpenApiSpecVersion . OpenApi3_1 , ( writer , element ) => element . SerializeAsV31 ( writer ) ,
136- ( writer , referenceElement ) => referenceElement . SerializeAsV31WithoutReference ( writer ) ) ;
135+ ( writer , referenceElement ) => referenceElement . SerializeAsV31 ( writer ) ) ;
137136 }
138137
139138 /// <summary>
@@ -154,7 +153,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
154153
155154 writer . WriteStartObject ( ) ;
156155 SerializeInternal ( writer , OpenApiSpecVersion . OpenApi3_0 , ( writer , element ) => element . SerializeAsV3 ( writer ) ,
157- ( writer , referenceElement ) => referenceElement . SerializeAsV3WithoutReference ( writer ) ) ;
156+ ( writer , referenceElement ) => referenceElement . SerializeAsV3 ( writer ) ) ;
158157 }
159158
160159 /// <summary>
@@ -172,14 +171,13 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
172171 Schemas ,
173172 ( w , key , component ) =>
174173 {
175- if ( component . Reference is { Type : ReferenceType . Schema } &&
176- component . Reference . Id == key )
174+ if ( component is OpenApiSchemaReference reference )
177175 {
178- component . SerializeAsV3WithoutReference ( w ) ;
176+ action ( w , reference ) ;
179177 }
180178 else
181179 {
182- component . SerializeAsV3 ( w ) ;
180+ callback ( w , component ) ;
183181 }
184182 } ) ;
185183
@@ -189,11 +187,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
189187 Responses ,
190188 ( w , key , component ) =>
191189 {
192- if ( component . Reference != null &&
193- component . Reference . Type == ReferenceType . Response &&
194- string . Equals ( component . Reference . Id , key , StringComparison . OrdinalIgnoreCase ) )
190+ if ( component is OpenApiResponseReference reference )
195191 {
196- action ( w , component ) ;
192+ action ( w , reference ) ;
197193 }
198194 else
199195 {
@@ -207,11 +203,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
207203 Parameters ,
208204 ( w , key , component ) =>
209205 {
210- if ( component . Reference != null &&
211- component . Reference . Type == ReferenceType . Parameter &&
212- string . Equals ( component . Reference . Id , key , StringComparison . OrdinalIgnoreCase ) )
206+ if ( component is OpenApiParameterReference reference )
213207 {
214- action ( w , component ) ;
208+ action ( w , reference ) ;
215209 }
216210 else
217211 {
@@ -225,11 +219,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
225219 Examples ,
226220 ( w , key , component ) =>
227221 {
228- if ( component . Reference != null &&
229- component . Reference . Type == ReferenceType . Example &&
230- string . Equals ( component . Reference . Id , key , StringComparison . OrdinalIgnoreCase ) )
222+ if ( component is OpenApiExampleReference reference )
231223 {
232- action ( writer , component ) ;
224+ action ( w , reference ) ;
233225 }
234226 else
235227 {
@@ -243,12 +235,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
243235 RequestBodies ,
244236 ( w , key , component ) =>
245237 {
246- if ( component . Reference != null &&
247- component . Reference . Type == ReferenceType . RequestBody &&
248- string . Equals ( component . Reference . Id , key , StringComparison . OrdinalIgnoreCase ) )
249-
238+ if ( component is OpenApiRequestBodyReference reference )
250239 {
251- action ( w , component ) ;
240+ action ( w , reference ) ;
252241 }
253242 else
254243 {
@@ -262,11 +251,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
262251 Headers ,
263252 ( w , key , component ) =>
264253 {
265- if ( component . Reference != null &&
266- component . Reference . Type == ReferenceType . Header &&
267- string . Equals ( component . Reference . Id , key , StringComparison . OrdinalIgnoreCase ) )
254+ if ( component is OpenApiHeaderReference reference )
268255 {
269- action ( w , component ) ;
256+ action ( w , reference ) ;
270257 }
271258 else
272259 {
@@ -280,11 +267,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
280267 SecuritySchemes ,
281268 ( w , key , component ) =>
282269 {
283- if ( component . Reference != null &&
284- component . Reference . Type == ReferenceType . SecurityScheme &&
285- string . Equals ( component . Reference . Id , key , StringComparison . OrdinalIgnoreCase ) )
270+ if ( component is OpenApiSecuritySchemeReference reference )
286271 {
287- action ( w , component ) ;
272+ action ( w , reference ) ;
288273 }
289274 else
290275 {
@@ -298,11 +283,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
298283 Links ,
299284 ( w , key , component ) =>
300285 {
301- if ( component . Reference != null &&
302- component . Reference . Type == ReferenceType . Link &&
303- string . Equals ( component . Reference . Id , key , StringComparison . OrdinalIgnoreCase ) )
286+ if ( component is OpenApiLinkReference reference )
304287 {
305- action ( w , component ) ;
288+ action ( w , reference ) ;
306289 }
307290 else
308291 {
@@ -316,11 +299,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
316299 Callbacks ,
317300 ( w , key , component ) =>
318301 {
319- if ( component . Reference != null &&
320- component . Reference . Type == ReferenceType . Callback &&
321- string . Equals ( component . Reference . Id , key , StringComparison . OrdinalIgnoreCase ) )
302+ if ( component is OpenApiCallbackReference reference )
322303 {
323- action ( w , component ) ;
304+ action ( w , reference ) ;
324305 }
325306 else
326307 {
0 commit comments