Skip to content

Commit 29604ca

Browse files
committed
feat: adds the detected format to the diagnostics
Signed-off-by: Vincent Biret <[email protected]>
1 parent b928906 commit 29604ca

File tree

12 files changed

+38
-39
lines changed

12 files changed

+38
-39
lines changed

src/Microsoft.OpenApi.YamlReader/OpenApiYamlReader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,20 @@ 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,
7374
Diagnostic = diagnostic,
74-
Format = OpenApiConstants.Yaml,
7575
};
7676
}
7777

7878
return UpdateFormat(Read(jsonNode, location, settings));
7979
}
8080
private static ReadResult UpdateFormat(ReadResult result)
8181
{
82-
result.Format = OpenApiConstants.Yaml;
82+
result.Diagnostic ??= new OpenApiDiagnostic();
83+
result.Diagnostic.Format = OpenApiConstants.Yaml;
8384
return result;
8485
}
8586

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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +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,
4849
Diagnostic = diagnostic,
49-
Format = OpenApiConstants.Json,
5050
};
5151
}
5252

@@ -103,12 +103,11 @@ public ReadResult Read(JsonNode jsonNode,
103103
}
104104
}
105105
}
106-
106+
diagnostic.Format = OpenApiConstants.Json;
107107
return new()
108108
{
109109
Document = document,
110110
Diagnostic = diagnostic,
111-
Format = OpenApiConstants.Json
112111
};
113112
}
114113

@@ -140,11 +139,11 @@ public async Task<ReadResult> ReadAsync(Stream input,
140139
catch (JsonException ex)
141140
{
142141
diagnostic.Errors.Add(new OpenApiError($"#line={ex.LineNumber}", $"Please provide the correct format, {ex.Message}"));
142+
diagnostic.Format = OpenApiConstants.Json;
143143
return new ReadResult
144144
{
145145
Document = null,
146146
Diagnostic = diagnostic,
147-
Format = OpenApiConstants.Json,
148147
};
149148
}
150149

src/Microsoft.OpenApi/Reader/ReadResult.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,12 @@ public class ReadResult
1616
/// </summary>
1717
public OpenApiDiagnostic? Diagnostic { get; set; }
1818
/// <summary>
19-
/// The format of the OpenAPI document (e.g., "json", "yaml").
20-
/// </summary>
21-
public string? Format { get; set; }
22-
/// <summary>
2319
/// Deconstructs the result for easier assignment on the client application.
2420
/// </summary>
2521
public void Deconstruct(out OpenApiDocument? document, out OpenApiDiagnostic? diagnostic)
26-
{
27-
Deconstruct(out document, out diagnostic, out _);
28-
}
29-
/// <summary>
30-
/// Deconstructs the result for easier assignment on the client application.
31-
/// </summary>
32-
public void Deconstruct(out OpenApiDocument? document, out OpenApiDiagnostic? diagnostic, out string? format)
3322
{
3423
document = Document;
3524
diagnostic = Diagnostic;
36-
format = Format;
3725
}
3826
}
3927

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +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-
(_, _, var format) = 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, format);
25+
Assert.Equal(OpenApiConstants.Yaml, diagnostic.Format);
2626
}
2727

2828
[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

0 commit comments

Comments
 (0)