Skip to content
Merged
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
17 changes: 12 additions & 5 deletions src/Microsoft.OpenApi.YamlReader/OpenApiYamlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public async Task<ReadResult> ReadAsync(Stream input,
if (input is null) throw new ArgumentNullException(nameof(input));
if (input is MemoryStream memoryStream)
{
return Read(memoryStream, location, settings);
return UpdateFormat(Read(memoryStream, location, settings));
}
else
{
using var preparedStream = new MemoryStream();
await input.CopyToAsync(preparedStream, copyBufferSize, cancellationToken).ConfigureAwait(false);
preparedStream.Position = 0;
return Read(preparedStream, location, settings);
return UpdateFormat(Read(preparedStream, location, settings));
}
}

Expand Down Expand Up @@ -67,20 +67,27 @@ public ReadResult Read(MemoryStream input,
{
var diagnostic = new OpenApiDiagnostic();
diagnostic.Errors.Add(new($"#line={ex.LineNumber}", ex.Message));
diagnostic.Format = OpenApiConstants.Yaml;
return new()
{
Document = null,
Diagnostic = diagnostic
Diagnostic = diagnostic,
};
}

return Read(jsonNode, location, settings);
return UpdateFormat(Read(jsonNode, location, settings));
}
private static ReadResult UpdateFormat(ReadResult result)
{
result.Diagnostic ??= new OpenApiDiagnostic();
result.Diagnostic.Format = OpenApiConstants.Yaml;
return result;
}

/// <inheritdoc/>
public static ReadResult Read(JsonNode jsonNode, Uri location, OpenApiReaderSettings settings)
{
return _jsonReader.Read(jsonNode, location, settings);
return UpdateFormat(_jsonReader.Read(jsonNode, location, settings));
}

/// <inheritdoc/>
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.OpenApi/Reader/OpenApiDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class OpenApiDiagnostic
/// </summary>
public OpenApiSpecVersion SpecificationVersion { get; set; }

/// <summary>
/// The format of the OpenAPI document (e.g., "json", "yaml").
/// </summary>
public string? Format { get; set; }

/// <summary>
/// Append another set of diagnostic Errors and Warnings to this one, this may be appended from another external
/// document's parsing and we want to indicate which file it originated from.
Expand Down
10 changes: 6 additions & 4 deletions src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

JsonNode? jsonNode;
var diagnostic = new OpenApiDiagnostic();
settings ??= new OpenApiReaderSettings();

Check warning on line 35 in src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unnecessary check for null. Some code paths are unreachable. (https://rules.sonarsource.com/csharp/RSPEC-2583)

Check warning on line 35 in src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unnecessary check for null. Some code paths are unreachable. (https://rules.sonarsource.com/csharp/RSPEC-2583)

Check warning on line 35 in src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unnecessary check for null. Some code paths are unreachable. (https://rules.sonarsource.com/csharp/RSPEC-2583)

Check warning on line 35 in src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unnecessary check for null. Some code paths are unreachable. (https://rules.sonarsource.com/csharp/RSPEC-2583)

// Parse the JSON text in the stream into JsonNodes
try
Expand All @@ -42,10 +42,11 @@
catch (JsonException ex)
{
diagnostic.Errors.Add(new OpenApiError($"#line={ex.LineNumber}", $"Please provide the correct format, {ex.Message}"));
diagnostic.Format = OpenApiConstants.Json;
return new ReadResult
{
Document = null,
Diagnostic = diagnostic
Diagnostic = diagnostic,
};
}

Expand Down Expand Up @@ -102,11 +103,11 @@
}
}
}

diagnostic.Format = OpenApiConstants.Json;
return new()
{
Document = document,
Diagnostic = diagnostic
Diagnostic = diagnostic,
};
}

Expand Down Expand Up @@ -138,10 +139,11 @@
catch (JsonException ex)
{
diagnostic.Errors.Add(new OpenApiError($"#line={ex.LineNumber}", $"Please provide the correct format, {ex.Message}"));
diagnostic.Format = OpenApiConstants.Json;
return new ReadResult
{
Document = null,
Diagnostic = diagnostic
Diagnostic = diagnostic,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public async Task StreamShouldCloseIfLeaveStreamOpenSettingEqualsFalse()
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml"));
var settings = new OpenApiReaderSettings { LeaveStreamOpen = false };
settings.AddYamlReader();
_ = await OpenApiDocument.LoadAsync(stream, settings: settings);
(_, var diagnostic) = await OpenApiDocument.LoadAsync(stream, settings: settings);
Assert.False(stream.CanRead);
Assert.Equal(OpenApiConstants.Yaml, diagnostic.Format);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ public void InvalidHostShouldYieldError()
{
new OpenApiError("#/", "Invalid host")
},
SpecificationVersion = OpenApiSpecVersion.OpenApi2_0
SpecificationVersion = OpenApiSpecVersion.OpenApi2_0,
Format = OpenApiConstants.Yaml
}, result.Diagnostic);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public async Task ParseDocumentWithWebhooksShouldSucceed()
};

// Assert
Assert.Equivalent(new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 }, actual.Diagnostic);
Assert.Equivalent(new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1, Format = OpenApiConstants.Yaml }, actual.Diagnostic);
actual.Document.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri));
}

Expand Down Expand Up @@ -414,7 +414,7 @@ public async Task ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
.Excluding(x => x.Workspace)
.Excluding(y => y.BaseUri));
Assert.Equivalent(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 }, actual.Diagnostic);
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1, Format = OpenApiConstants.Yaml }, actual.Diagnostic);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task ParseCallbackWithReferenceShouldSucceed()
var callback = subscribeOperation.Callbacks["simpleHook"];

Assert.Equivalent(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }, result.Diagnostic);
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, Format = OpenApiConstants.Yaml }, result.Diagnostic);

Assert.Equivalent(
new OpenApiCallbackReference("simpleHook", result.Document)
Expand Down Expand Up @@ -120,7 +120,7 @@ public async Task ParseMultipleCallbacksWithReferenceShouldSucceed()
var subscribeOperation = path.Operations[HttpMethod.Post];

Assert.Equivalent(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }, result.Diagnostic);
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, Format = OpenApiConstants.Yaml }, result.Diagnostic);

var callback1 = subscribeOperation.Callbacks["simpleHook"];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void ParseDocumentFromInlineStringShouldSucceed()
Assert.Equivalent(
new OpenApiDiagnostic()
{
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
Format = OpenApiConstants.Yaml
}, result.Diagnostic);
}

Expand Down Expand Up @@ -147,7 +148,8 @@ public async Task ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
{
new OpenApiValidatorError(nameof(OpenApiInfoRules.InfoRequiredFields),"#/info/title", "The field 'title' in 'info' object is REQUIRED.")
},
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
Format = OpenApiConstants.Yaml
}, result.Diagnostic);
}

Expand All @@ -170,7 +172,8 @@ public async Task ParseMinimalDocumentShouldSucceed()
Assert.Equivalent(
new OpenApiDiagnostic()
{
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
Format = OpenApiConstants.Yaml
}, result.Diagnostic);
}

Expand Down Expand Up @@ -557,7 +560,7 @@ public async Task ParseStandardPetStoreDocumentShouldSucceed()
actual.Document.Should().BeEquivalentTo(expectedDoc, options => options.Excluding(x => x.Workspace).Excluding(y => y.BaseUri));

Assert.Equivalent(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }, actual.Diagnostic);
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, Format = OpenApiConstants.Yaml }, actual.Diagnostic);
}

[Fact]
Expand Down Expand Up @@ -1031,7 +1034,7 @@ [new OpenApiSecuritySchemeReference("securitySchemeName2")] =
.Excluding(y => y.BaseUri));

Assert.Equivalent(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }, actual.Diagnostic);
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, Format = OpenApiConstants.Yaml }, actual.Diagnostic);
}

[Fact]
Expand All @@ -1042,7 +1045,7 @@ public async Task ParsePetStoreExpandedShouldSucceed()
// TODO: Create the object in memory and compare with the one read from YAML file.

Assert.Equivalent(
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }, actual.Diagnostic);
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, Format = OpenApiConstants.Yaml }, actual.Diagnostic);
}

[Fact]
Expand Down Expand Up @@ -1444,9 +1447,10 @@ public void ParseBasicDocumentWithServerVariableShouldSucceed()
};

Assert.Equivalent(
new OpenApiDiagnostic
{
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
new OpenApiDiagnostic
{
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
Format = OpenApiConstants.Yaml
}, result.Diagnostic);

result.Document.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.BaseUri));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public async Task ParseBasicSchemaWithReferenceShouldSucceed()
Assert.Equivalent(
new OpenApiDiagnostic()
{
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
Format = OpenApiConstants.Yaml
}, result.Diagnostic);

var expectedComponents = new OpenApiComponents
Expand Down Expand Up @@ -394,7 +395,8 @@ public async Task ParseExternalReferenceSchemaShouldSucceed()
Assert.Equivalent(
new OpenApiDiagnostic()
{
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
Format = OpenApiConstants.Yaml
}, result.Diagnostic);

var expectedComponents = new OpenApiComponents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1934,6 +1934,7 @@ namespace Microsoft.OpenApi.Reader
{
public OpenApiDiagnostic() { }
public System.Collections.Generic.IList<Microsoft.OpenApi.OpenApiError> Errors { get; set; }
public string? Format { get; set; }
public Microsoft.OpenApi.OpenApiSpecVersion SpecificationVersion { get; set; }
public System.Collections.Generic.IList<Microsoft.OpenApi.OpenApiError> Warnings { get; set; }
public void AppendDiagnostic(Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnosticToAdd, string? fileNameToAdd = null) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ await File.WriteAllTextAsync(tempFilePathReferrer,
BaseUrl = baseUri,
};
var readResult = await OpenApiDocument.LoadAsync(stream, settings: settings);
Assert.Equal(OpenApiConstants.Json, readResult.Diagnostic.Format);
Assert.NotNull(readResult.Document);
Assert.NotNull(readResult.Document.Components);
Assert.Equal(baseUri, readResult.Document.BaseUri);
Expand Down
Loading