Skip to content

Commit 59d7c81

Browse files
authored
feat: adds the detected format as part of the diagnostic (#2482)
feat: adds the detected format to the diagnostics ------- Signed-off-by: Vincent Biret <[email protected]>
1 parent f00f73f commit 59d7c81

File tree

11 files changed

+50
-26
lines changed

11 files changed

+50
-26
lines changed

src/Microsoft.OpenApi.YamlReader/OpenApiYamlReader.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ public async Task<ReadResult> ReadAsync(Stream input,
3131
if (input is null) throw new ArgumentNullException(nameof(input));
3232
if (input is MemoryStream memoryStream)
3333
{
34-
return Read(memoryStream, location, settings);
34+
return UpdateFormat(Read(memoryStream, location, settings));
3535
}
3636
else
3737
{
3838
using var preparedStream = new MemoryStream();
3939
await input.CopyToAsync(preparedStream, copyBufferSize, cancellationToken).ConfigureAwait(false);
4040
preparedStream.Position = 0;
41-
return Read(preparedStream, location, settings);
41+
return UpdateFormat(Read(preparedStream, location, settings));
4242
}
4343
}
4444

@@ -67,20 +67,27 @@ public ReadResult Read(MemoryStream input,
6767
{
6868
var diagnostic = new OpenApiDiagnostic();
6969
diagnostic.Errors.Add(new($"#line={ex.LineNumber}", ex.Message));
70+
diagnostic.Format = OpenApiConstants.Yaml;
7071
return new()
7172
{
7273
Document = null,
73-
Diagnostic = diagnostic
74+
Diagnostic = diagnostic,
7475
};
7576
}
7677

77-
return Read(jsonNode, location, settings);
78+
return UpdateFormat(Read(jsonNode, location, settings));
79+
}
80+
private static ReadResult UpdateFormat(ReadResult result)
81+
{
82+
result.Diagnostic ??= new OpenApiDiagnostic();
83+
result.Diagnostic.Format = OpenApiConstants.Yaml;
84+
return result;
7885
}
7986

8087
/// <inheritdoc/>
8188
public static ReadResult Read(JsonNode jsonNode, Uri location, OpenApiReaderSettings settings)
8289
{
83-
return _jsonReader.Read(jsonNode, location, settings);
90+
return UpdateFormat(_jsonReader.Read(jsonNode, location, settings));
8491
}
8592

8693
/// <inheritdoc/>

src/Microsoft.OpenApi/Reader/OpenApiDiagnostic.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public class OpenApiDiagnostic
2525
/// </summary>
2626
public OpenApiSpecVersion SpecificationVersion { get; set; }
2727

28+
/// <summary>
29+
/// The format of the OpenAPI document (e.g., "json", "yaml").
30+
/// </summary>
31+
public string? Format { get; set; }
32+
2833
/// <summary>
2934
/// Append another set of diagnostic Errors and Warnings to this one, this may be appended from another external
3035
/// document's parsing and we want to indicate which file it originated from.

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public ReadResult Read(MemoryStream input,
4242
catch (JsonException ex)
4343
{
4444
diagnostic.Errors.Add(new OpenApiError($"#line={ex.LineNumber}", $"Please provide the correct format, {ex.Message}"));
45+
diagnostic.Format = OpenApiConstants.Json;
4546
return new ReadResult
4647
{
4748
Document = null,
48-
Diagnostic = diagnostic
49+
Diagnostic = diagnostic,
4950
};
5051
}
5152

@@ -102,11 +103,11 @@ public ReadResult Read(JsonNode jsonNode,
102103
}
103104
}
104105
}
105-
106+
diagnostic.Format = OpenApiConstants.Json;
106107
return new()
107108
{
108109
Document = document,
109-
Diagnostic = diagnostic
110+
Diagnostic = diagnostic,
110111
};
111112
}
112113

@@ -138,10 +139,11 @@ public async Task<ReadResult> ReadAsync(Stream input,
138139
catch (JsonException ex)
139140
{
140141
diagnostic.Errors.Add(new OpenApiError($"#line={ex.LineNumber}", $"Please provide the correct format, {ex.Message}"));
142+
diagnostic.Format = OpenApiConstants.Json;
141143
return new ReadResult
142144
{
143145
Document = null,
144-
Diagnostic = diagnostic
146+
Diagnostic = diagnostic,
145147
};
146148
}
147149

test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public async Task StreamShouldCloseIfLeaveStreamOpenSettingEqualsFalse()
2020
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml"));
2121
var settings = new OpenApiReaderSettings { LeaveStreamOpen = false };
2222
settings.AddYamlReader();
23-
_ = await OpenApiDocument.LoadAsync(stream, settings: settings);
23+
(_, var diagnostic) = await OpenApiDocument.LoadAsync(stream, settings: settings);
2424
Assert.False(stream.CanRead);
25+
Assert.Equal(OpenApiConstants.Yaml, diagnostic.Format);
2526
}
2627

2728
[Fact]

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ public void InvalidHostShouldYieldError()
314314
{
315315
new OpenApiError("#/", "Invalid host")
316316
},
317-
SpecificationVersion = OpenApiSpecVersion.OpenApi2_0
317+
SpecificationVersion = OpenApiSpecVersion.OpenApi2_0,
318+
Format = OpenApiConstants.Yaml
318319
}, result.Diagnostic);
319320
}
320321
}

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public async Task ParseDocumentWithWebhooksShouldSucceed()
207207
};
208208

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

@@ -414,7 +414,7 @@ public async Task ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
414414
.Excluding(x => x.Workspace)
415415
.Excluding(y => y.BaseUri));
416416
Assert.Equivalent(
417-
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 }, actual.Diagnostic);
417+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1, Format = OpenApiConstants.Yaml }, actual.Diagnostic);
418418
}
419419

420420
[Fact]

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public async Task ParseCallbackWithReferenceShouldSucceed()
7171
var callback = subscribeOperation.Callbacks["simpleHook"];
7272

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

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

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

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

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public void ParseDocumentFromInlineStringShouldSucceed()
6969
Assert.Equivalent(
7070
new OpenApiDiagnostic()
7171
{
72-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
72+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
73+
Format = OpenApiConstants.Yaml
7374
}, result.Diagnostic);
7475
}
7576

@@ -147,7 +148,8 @@ public async Task ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
147148
{
148149
new OpenApiValidatorError(nameof(OpenApiInfoRules.InfoRequiredFields),"#/info/title", "The field 'title' in 'info' object is REQUIRED.")
149150
},
150-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
151+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
152+
Format = OpenApiConstants.Yaml
151153
}, result.Diagnostic);
152154
}
153155

@@ -170,7 +172,8 @@ public async Task ParseMinimalDocumentShouldSucceed()
170172
Assert.Equivalent(
171173
new OpenApiDiagnostic()
172174
{
173-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
175+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
176+
Format = OpenApiConstants.Yaml
174177
}, result.Diagnostic);
175178
}
176179

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

559562
Assert.Equivalent(
560-
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }, actual.Diagnostic);
563+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, Format = OpenApiConstants.Yaml }, actual.Diagnostic);
561564
}
562565

563566
[Fact]
@@ -1031,7 +1034,7 @@ [new OpenApiSecuritySchemeReference("securitySchemeName2")] =
10311034
.Excluding(y => y.BaseUri));
10321035

10331036
Assert.Equivalent(
1034-
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }, actual.Diagnostic);
1037+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, Format = OpenApiConstants.Yaml }, actual.Diagnostic);
10351038
}
10361039

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

10441047
Assert.Equivalent(
1045-
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 }, actual.Diagnostic);
1048+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0, Format = OpenApiConstants.Yaml }, actual.Diagnostic);
10461049
}
10471050

10481051
[Fact]
@@ -1444,9 +1447,10 @@ public void ParseBasicDocumentWithServerVariableShouldSucceed()
14441447
};
14451448

14461449
Assert.Equivalent(
1447-
new OpenApiDiagnostic
1448-
{
1449-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
1450+
new OpenApiDiagnostic
1451+
{
1452+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
1453+
Format = OpenApiConstants.Yaml
14501454
}, result.Diagnostic);
14511455

14521456
result.Document.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.BaseUri));

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ public async Task ParseBasicSchemaWithReferenceShouldSucceed()
231231
Assert.Equivalent(
232232
new OpenApiDiagnostic()
233233
{
234-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
234+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
235+
Format = OpenApiConstants.Yaml
235236
}, result.Diagnostic);
236237

237238
var expectedComponents = new OpenApiComponents
@@ -394,7 +395,8 @@ public async Task ParseExternalReferenceSchemaShouldSucceed()
394395
Assert.Equivalent(
395396
new OpenApiDiagnostic()
396397
{
397-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
398+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
399+
Format = OpenApiConstants.Yaml
398400
}, result.Diagnostic);
399401

400402
var expectedComponents = new OpenApiComponents

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,7 @@ namespace Microsoft.OpenApi.Reader
19341934
{
19351935
public OpenApiDiagnostic() { }
19361936
public System.Collections.Generic.IList<Microsoft.OpenApi.OpenApiError> Errors { get; set; }
1937+
public string? Format { get; set; }
19371938
public Microsoft.OpenApi.OpenApiSpecVersion SpecificationVersion { get; set; }
19381939
public System.Collections.Generic.IList<Microsoft.OpenApi.OpenApiError> Warnings { get; set; }
19391940
public void AppendDiagnostic(Microsoft.OpenApi.Reader.OpenApiDiagnostic diagnosticToAdd, string? fileNameToAdd = null) { }

0 commit comments

Comments
 (0)