@@ -89,7 +89,8 @@ private void AddSchemaToComponents(IOpenApiSchema? schema, string? referenceId =
8989 {
9090 EnsureComponentsExist ( ) ;
9191 EnsureSchemasExist ( ) ;
92- if ( Components . Schemas is not null && referenceId is not null && schema is not null && ! Components . Schemas . ContainsKey ( referenceId ) )
92+ Components . Schemas ??= new Dictionary < string , IOpenApiSchema > ( ) ;
93+ if ( referenceId is not null && schema is not null && ! Components . Schemas . ContainsKey ( referenceId ) )
9394 {
9495 Components . Schemas . Add ( referenceId , schema ) ;
9596 }
@@ -99,7 +100,8 @@ private void AddParameterToComponents(IOpenApiParameter? parameter, string? refe
99100 {
100101 EnsureComponentsExist ( ) ;
101102 EnsureParametersExist ( ) ;
102- if ( Components . Parameters is not null && parameter is not null && referenceId is not null && ! Components . Parameters . ContainsKey ( referenceId ) )
103+ Components . Parameters ??= new Dictionary < string , IOpenApiParameter > ( ) ;
104+ if ( parameter is not null && referenceId is not null && ! Components . Parameters . ContainsKey ( referenceId ) )
103105 {
104106 Components . Parameters . Add ( referenceId , parameter ) ;
105107 }
@@ -109,7 +111,8 @@ private void AddResponseToComponents(IOpenApiResponse? response, string? referen
109111 {
110112 EnsureComponentsExist ( ) ;
111113 EnsureResponsesExist ( ) ;
112- if ( Components . Responses is not null && referenceId is not null && response is not null && ! Components . Responses . ContainsKey ( referenceId ) )
114+ Components . Responses ??= new Dictionary < string , IOpenApiResponse > ( ) ;
115+ if ( referenceId is not null && response is not null && ! Components . Responses . ContainsKey ( referenceId ) )
113116 {
114117 Components . Responses . Add ( referenceId , response ) ;
115118 }
@@ -118,7 +121,8 @@ private void AddRequestBodyToComponents(IOpenApiRequestBody? requestBody, string
118121 {
119122 EnsureComponentsExist ( ) ;
120123 EnsureRequestBodiesExist ( ) ;
121- if ( Components . RequestBodies is not null && requestBody is not null && referenceId is not null && ! Components . RequestBodies . ContainsKey ( referenceId ) )
124+ Components . RequestBodies ??= new Dictionary < string , IOpenApiRequestBody > ( ) ;
125+ if ( requestBody is not null && referenceId is not null && ! Components . RequestBodies . ContainsKey ( referenceId ) )
122126 {
123127 Components . RequestBodies . Add ( referenceId , requestBody ) ;
124128 }
@@ -127,7 +131,8 @@ private void AddLinkToComponents(IOpenApiLink? link, string? referenceId = null)
127131 {
128132 EnsureComponentsExist ( ) ;
129133 EnsureLinksExist ( ) ;
130- if ( Components . Links is not null && link is not null && referenceId is not null && ! Components . Links . ContainsKey ( referenceId ) )
134+ Components . Links ??= new Dictionary < string , IOpenApiLink > ( ) ;
135+ if ( link is not null && referenceId is not null && ! Components . Links . ContainsKey ( referenceId ) )
131136 {
132137 Components . Links . Add ( referenceId , link ) ;
133138 }
@@ -136,7 +141,8 @@ private void AddCallbackToComponents(IOpenApiCallback? callback, string? referen
136141 {
137142 EnsureComponentsExist ( ) ;
138143 EnsureCallbacksExist ( ) ;
139- if ( Components . Callbacks is not null && callback is not null && referenceId is not null && ! Components . Callbacks . ContainsKey ( referenceId ) )
144+ Components . Callbacks ??= new Dictionary < string , IOpenApiCallback > ( ) ;
145+ if ( callback is not null && referenceId is not null && ! Components . Callbacks . ContainsKey ( referenceId ) )
140146 {
141147 Components . Callbacks . Add ( referenceId , callback ) ;
142148 }
@@ -145,7 +151,8 @@ private void AddHeaderToComponents(IOpenApiHeader? header, string? referenceId =
145151 {
146152 EnsureComponentsExist ( ) ;
147153 EnsureHeadersExist ( ) ;
148- if ( Components . Headers is not null && header is not null && referenceId is not null && ! Components . Headers . ContainsKey ( referenceId ) )
154+ Components . Headers ??= new Dictionary < string , IOpenApiHeader > ( ) ;
155+ if ( header is not null && referenceId is not null && ! Components . Headers . ContainsKey ( referenceId ) )
149156 {
150157 Components . Headers . Add ( referenceId , header ) ;
151158 }
@@ -154,7 +161,8 @@ private void AddExampleToComponents(IOpenApiExample? example, string? referenceI
154161 {
155162 EnsureComponentsExist ( ) ;
156163 EnsureExamplesExist ( ) ;
157- if ( Components . Examples is not null && example is not null && referenceId is not null && ! Components . Examples . ContainsKey ( referenceId ) )
164+ Components . Examples ??= new Dictionary < string , IOpenApiExample > ( ) ;
165+ if ( example is not null && referenceId is not null && ! Components . Examples . ContainsKey ( referenceId ) )
158166 {
159167 Components . Examples . Add ( referenceId , example ) ;
160168 }
@@ -163,7 +171,8 @@ private void AddPathItemToComponents(IOpenApiPathItem? pathItem, string? referen
163171 {
164172 EnsureComponentsExist ( ) ;
165173 EnsurePathItemsExist ( ) ;
166- if ( Components . PathItems is not null && pathItem is not null && referenceId is not null && ! Components . PathItems . ContainsKey ( referenceId ) )
174+ Components . PathItems ??= new Dictionary < string , IOpenApiPathItem > ( ) ;
175+ if ( pathItem is not null && referenceId is not null && ! Components . PathItems . ContainsKey ( referenceId ) )
167176 {
168177 Components . PathItems . Add ( referenceId , pathItem ) ;
169178 }
@@ -172,7 +181,8 @@ private void AddSecuritySchemeToComponents(IOpenApiSecurityScheme? securitySchem
172181 {
173182 EnsureComponentsExist ( ) ;
174183 EnsureSecuritySchemesExist ( ) ;
175- if ( Components . SecuritySchemes is not null && securityScheme is not null && referenceId is not null && ! Components . SecuritySchemes . ContainsKey ( referenceId ) )
184+ Components . SecuritySchemes ??= new Dictionary < string , IOpenApiSecurityScheme > ( ) ;
185+ if ( securityScheme is not null && referenceId is not null && ! Components . SecuritySchemes . ContainsKey ( referenceId ) )
176186 {
177187 Components . SecuritySchemes . Add ( referenceId , securityScheme ) ;
178188 }
0 commit comments