@@ -126,7 +126,7 @@ internal bool ApplyToDocument(JsonNode jsonNode, OverlayDiagnostic overlayDiagno
126126 /// <param name="readerSettings">Settings to use when reading the document.</param>
127127 /// <param name="cancellationToken">Cancellation token to cancel the operation.</param>
128128 /// <returns>The OpenAPI document after applying the action.</returns>
129- public async Task < OverlayApplicationResult > ApplyToExtendedDocumentAsync ( string ? format = default , OverlayReaderSettings ? readerSettings = default , CancellationToken cancellationToken = default )
129+ public async Task < OverlayApplicationResultOfJsonNode > ApplyToExtendedDocumentAsync ( string ? format = default , OverlayReaderSettings ? readerSettings = default , CancellationToken cancellationToken = default )
130130 {
131131 if ( string . IsNullOrEmpty ( Extends ) )
132132 {
@@ -135,6 +135,24 @@ public async Task<OverlayApplicationResult> ApplyToExtendedDocumentAsync(string?
135135 return await ApplyToDocumentAsync ( Extends , format , readerSettings , cancellationToken ) . ConfigureAwait ( false ) ;
136136 }
137137
138+ /// <summary>
139+ /// Applies the action to an OpenAPI document loaded from the extends property.
140+ /// The document is read in the specified format (e.g., JSON or YAML).
141+ /// </summary>
142+ /// <param name="format">The format of the document (e.g., JSON or YAML).</param>
143+ /// <param name="readerSettings">Settings to use when reading the document.</param>
144+ /// <param name="cancellationToken">Cancellation token to cancel the operation.</param>
145+ /// <returns>The OpenAPI document after applying the action.</returns>
146+ public async Task < OverlayApplicationResultOfOpenApiDocument > ApplyToExtendedDocumentAndLoadAsync ( string ? format = default , OverlayReaderSettings ? readerSettings = default , CancellationToken cancellationToken = default )
147+ {
148+ if ( string . IsNullOrEmpty ( Extends ) )
149+ {
150+ throw new InvalidOperationException ( "The 'extends' property must be set to apply the overlay to an extended document." ) ;
151+ }
152+ var jsonResult = await ApplyToExtendedDocumentAsync ( format , readerSettings , cancellationToken ) . ConfigureAwait ( false ) ;
153+ return LoadDocument ( jsonResult , new Uri ( Extends ) , format ?? string . Empty , readerSettings ) ;
154+ }
155+
138156 /// <summary>
139157 /// Applies the action to an OpenAPI document loaded from a specified path or URI.
140158 /// The document is read in the specified format (e.g., JSON or YAML).
@@ -144,7 +162,7 @@ public async Task<OverlayApplicationResult> ApplyToExtendedDocumentAsync(string?
144162 /// <param name="readerSettings">Settings to use when reading the document.</param>
145163 /// <param name="cancellationToken">Cancellation token to cancel the operation.</param>
146164 /// <returns>The OpenAPI document after applying the action.</returns>
147- public async Task < OverlayApplicationResult > ApplyToDocumentAsync ( string documentPathOrUri , string ? format = default , OverlayReaderSettings ? readerSettings = default , CancellationToken cancellationToken = default )
165+ public async Task < OverlayApplicationResultOfJsonNode > ApplyToDocumentAsync ( string documentPathOrUri , string ? format = default , OverlayReaderSettings ? readerSettings = default , CancellationToken cancellationToken = default )
148166 {
149167 ArgumentException . ThrowIfNullOrEmpty ( documentPathOrUri ) ;
150168 readerSettings ??= new OverlayReaderSettings ( ) ;
@@ -164,6 +182,23 @@ public async Task<OverlayApplicationResult> ApplyToDocumentAsync(string document
164182 using var fileStream = new FileStream ( documentPathOrUri , FileMode . Open , FileAccess . Read ) ;
165183 await fileStream . CopyToAsync ( input , cancellationToken ) . ConfigureAwait ( false ) ;
166184 }
185+ var result = await ApplyToDocumentStreamAsync ( input , format , readerSettings , cancellationToken ) . ConfigureAwait ( false ) ;
186+ await input . DisposeAsync ( ) . ConfigureAwait ( false ) ;
187+ return result ;
188+ }
189+
190+ /// <summary>
191+ /// Applies the action to an OpenAPI document loaded from a specified path or URI.
192+ /// The document is read in the specified format (e.g., JSON or YAML).
193+ /// </summary>
194+ /// <param name="documentPathOrUri">Path or URI to the OpenAPI document.</param>
195+ /// <param name="format">The format of the document (e.g., JSON or YAML).</param>
196+ /// <param name="readerSettings">Settings to use when reading the document.</param>
197+ /// <param name="cancellationToken">Cancellation token to cancel the operation.</param>
198+ /// <returns>The OpenAPI document after applying the action.</returns>
199+ public async Task < OverlayApplicationResultOfOpenApiDocument > ApplyToDocumentAndLoadAsync ( string documentPathOrUri , string ? format = default , OverlayReaderSettings ? readerSettings = default , CancellationToken cancellationToken = default )
200+ {
201+ var jsonResult = await ApplyToDocumentAsync ( documentPathOrUri , format , readerSettings , cancellationToken ) . ConfigureAwait ( false ) ;
167202
168203 // Convert file paths to absolute paths before creating URI to handle relative paths correctly
169204 Uri uri ;
@@ -178,22 +213,20 @@ public async Task<OverlayApplicationResult> ApplyToDocumentAsync(string document
178213 var absolutePath = Path . GetFullPath ( documentPathOrUri ) ;
179214 uri = new Uri ( absolutePath , UriKind . Absolute ) ;
180215 }
181- var result = await ApplyToDocumentStreamAsync ( input , uri , format , readerSettings , cancellationToken ) . ConfigureAwait ( false ) ;
182- await input . DisposeAsync ( ) . ConfigureAwait ( false ) ;
183- return result ;
216+
217+ return LoadDocument ( jsonResult , uri , format ?? string . Empty , readerSettings ) ;
184218 }
185219
186220 /// <summary>
187221 /// Applies the action to an OpenAPI document loaded from a specified path or URI.
188222 /// The document is read in the specified format (e.g., JSON or YAML).
189223 /// </summary>
190224 /// <param name="input">A stream containing the OpenAPI document.</param>
191- /// <param name="location">The URI location of the document, used for to load external references.</param>
192225 /// <param name="format">The format of the document (e.g., JSON or YAML).</param>
193226 /// <param name="readerSettings">Settings to use when reading the document.</param>
194227 /// <param name="cancellationToken">Cancellation token to cancel the operation.</param>
195228 /// <returns>The OpenAPI document after applying the action.</returns>
196- public async Task < OverlayApplicationResult > ApplyToDocumentStreamAsync ( Stream input , Uri location , string ? format = default , OverlayReaderSettings ? readerSettings = default , CancellationToken cancellationToken = default )
229+ public async Task < OverlayApplicationResultOfJsonNode > ApplyToDocumentStreamAsync ( Stream input , string ? format = default , OverlayReaderSettings ? readerSettings = default , CancellationToken cancellationToken = default )
197230 {
198231 ArgumentNullException . ThrowIfNull ( input ) ;
199232 readerSettings ??= new OverlayReaderSettings ( ) ;
@@ -213,21 +246,48 @@ public async Task<OverlayApplicationResult> ApplyToDocumentStreamAsync(Stream in
213246 throw new InvalidOperationException ( "Failed to parse the OpenAPI document." ) ;
214247 var overlayDiagnostic = new OverlayDiagnostic ( ) ;
215248 var result = ApplyToDocument ( jsonNode , overlayDiagnostic ) ;
216- var openAPIJsonReader = new OpenApiJsonReader ( ) ;
217- var ( openAPIDocument , openApiDiagnostic ) = openAPIJsonReader . Read ( jsonNode , location , readerSettings . OpenApiSettings ) ;
218- if ( openApiDiagnostic is not null )
249+ return new OverlayApplicationResultOfJsonNode
219250 {
220- openApiDiagnostic . Format = format ;
221- }
222- return new OverlayApplicationResult
223- {
224- Document = openAPIDocument ,
251+ Document = jsonNode ,
225252 Diagnostic = overlayDiagnostic ,
226- OpenApiDiagnostic = openApiDiagnostic ,
227253 IsSuccessful = result ,
254+ OpenApiDiagnostic = new OpenApiDiagnostic ( )
255+ {
256+ Format = format
257+ }
228258 } ;
229259 }
230260 /// <summary>
261+ /// Applies the action to an OpenAPI document loaded from a specified path or URI.
262+ /// The document is read in the specified format (e.g., JSON or YAML).
263+ /// </summary>
264+ /// <param name="input">A stream containing the OpenAPI document.</param>
265+ /// <param name="location">The URI location of the document, used for to load external references.</param>
266+ /// <param name="format">The format of the document (e.g., JSON or YAML).</param>
267+ /// <param name="readerSettings">Settings to use when reading the document.</param>
268+ /// <param name="cancellationToken">Cancellation token to cancel the operation.</param>
269+ /// <returns>The OpenAPI document after applying the action.</returns>
270+ public async Task < OverlayApplicationResultOfOpenApiDocument > ApplyToDocumentStreamAndLoadAsync ( Stream input , Uri location , string ? format = default , OverlayReaderSettings ? readerSettings = default , CancellationToken cancellationToken = default )
271+ {
272+ var jsonResult = await ApplyToDocumentStreamAsync ( input , format , readerSettings , cancellationToken ) . ConfigureAwait ( false ) ;
273+ return LoadDocument ( jsonResult , location , format ?? string . Empty , readerSettings ) ;
274+ }
275+ internal static OverlayApplicationResultOfOpenApiDocument LoadDocument ( OverlayApplicationResultOfJsonNode jsonResult , Uri location , string format , OverlayReaderSettings ? readerSettings )
276+ {
277+ readerSettings ??= new OverlayReaderSettings ( ) ;
278+ var openAPIJsonReader = new OpenApiJsonReader ( ) ;
279+ if ( jsonResult . Document is null )
280+ {
281+ return OverlayApplicationResultOfOpenApiDocument . FromJsonResultWithFailedLoad ( jsonResult ) ;
282+ }
283+ var ( openAPIDocument , openApiDiagnostic ) = openAPIJsonReader . Read ( jsonResult . Document , location , readerSettings . OpenApiSettings ) ;
284+ if ( openApiDiagnostic is not null && ! string . IsNullOrEmpty ( format ) )
285+ {
286+ openApiDiagnostic . Format = format ;
287+ }
288+ return OverlayApplicationResultOfOpenApiDocument . FromJsonResult ( jsonResult , openAPIDocument , openApiDiagnostic ) ;
289+ }
290+ /// <summary>
231291 /// Combines this overlay document with another overlay document.
232292 /// The returned document will be a new document, and its metadata (info, etc.) will be the one from the other document.
233293 /// The actions from both documents will be merged. The current document actions will be first, and the ones from the other document will be next.
0 commit comments