Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Kiota.Builder/OpenApiDocumentDownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,17 @@ ex is SecurityException ||
{
// couldn't parse the URL, it's probably a local file
}
var readResult = await OpenApiDocument.LoadAsync(input, settings: settings, cancellationToken: cancellationToken).ConfigureAwait(false);

ReadResult readResult;
try
{
readResult = await OpenApiDocument.LoadAsync(input, settings: settings, cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
LogOpenApiParsingCriticalError(ex, config.OpenAPIFilePath);
throw;
}
stopwatch.Stop();
if (generatingMode && readResult.Diagnostic?.Warnings is { Count: > 0 })
foreach (var warning in readResult.Diagnostic.Warnings)
Expand Down Expand Up @@ -188,6 +198,9 @@ ex is SecurityException ||
[LoggerMessage(Level = LogLevel.Error, Message = "OpenAPI error: {Pointer} - {Message}")]
private partial void LogOpenApiError(string? pointer, string message);

[LoggerMessage(Level = LogLevel.Critical, Message = "Error parsing specification {Path}")]
private partial void LogOpenApiParsingCriticalError(Exception exception, string path);

[LoggerMessage(Level = LogLevel.Trace, Message = "{Timestamp}ms: Parsed OpenAPI successfully. {Count} paths found.")]
private partial void LogParsedOpenApiSuccessfully(long timestamp, int count);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public sealed class OpenApiDocumentDownloadServiceTests : IDisposable
schema:
type: object";


public void Dispose()
{
_httpClient.Dispose();
Expand Down Expand Up @@ -78,6 +77,51 @@ public async Task GetDocumentFromStreamAsyncTest_No_IncludeKiotaValidationRulesI
Assert.Empty(logEntryForNoServerRule);
}

[Fact]
public async Task GetDocumentFromStreamAsync_LogsSpecificationPathWhenParsingThrows()
{
const string brokenDocument = """
{
"openapi": "3.0.1",
"info": {
"title": "Repro API",
"version": "1.0.0"
},
"paths": {},
"components": {
"schemas": {
"ItemStatus": {
"type": "string",
"enum": [
"Active",
"Archived"
],
"x-ms-enum-flags": []
}
}
}
}
""";

var generationConfig = new GenerationConfiguration
{
OpenAPIFilePath = "repro-broken.json"
};
var fakeLogger = new FakeLogger<OpenApiDocumentDownloadService>();

using var inputDocumentStream = CreateMemoryStreamFromString(brokenDocument);
var documentDownloadService = new OpenApiDocumentDownloadService(_httpClient, fakeLogger);

await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() =>
documentDownloadService.GetDocumentFromStreamAsync(inputDocumentStream, generationConfig, cancellationToken: TestContext.Current.CancellationToken));

var parsingLogEntry = fakeLogger.LogEntries
.Where(l => l.message.Contains("Error parsing specification", StringComparison.OrdinalIgnoreCase));

var logEntry = Assert.Single(parsingLogEntry);
Assert.Contains("repro-broken.json", logEntry.message, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public async Task GetDocumentFromStreamAsyncTest_Default_IncludeKiotaValidationRulesInConfig()
{
Expand Down Expand Up @@ -107,4 +151,4 @@ private static Stream CreateMemoryStreamFromString(string s)
stream.Position = 0;
return stream;
}
}
}
Loading