Skip to content

Commit b928906

Browse files
committed
feat: adds the detected format as part of the result
Signed-off-by: Vincent Biret <[email protected]>
1 parent f00f73f commit b928906

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

src/Microsoft.OpenApi.YamlReader/OpenApiYamlReader.cs

Lines changed: 11 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

@@ -70,17 +70,23 @@ public ReadResult Read(MemoryStream input,
7070
return new()
7171
{
7272
Document = null,
73-
Diagnostic = diagnostic
73+
Diagnostic = diagnostic,
74+
Format = OpenApiConstants.Yaml,
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.Format = OpenApiConstants.Yaml;
83+
return result;
7884
}
7985

8086
/// <inheritdoc/>
8187
public static ReadResult Read(JsonNode jsonNode, Uri location, OpenApiReaderSettings settings)
8288
{
83-
return _jsonReader.Read(jsonNode, location, settings);
89+
return UpdateFormat(_jsonReader.Read(jsonNode, location, settings));
8490
}
8591

8692
/// <inheritdoc/>

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public ReadResult Read(MemoryStream input,
4545
return new ReadResult
4646
{
4747
Document = null,
48-
Diagnostic = diagnostic
48+
Diagnostic = diagnostic,
49+
Format = OpenApiConstants.Json,
4950
};
5051
}
5152

@@ -106,7 +107,8 @@ public ReadResult Read(JsonNode jsonNode,
106107
return new()
107108
{
108109
Document = document,
109-
Diagnostic = diagnostic
110+
Diagnostic = diagnostic,
111+
Format = OpenApiConstants.Json
110112
};
111113
}
112114

@@ -141,7 +143,8 @@ public async Task<ReadResult> ReadAsync(Stream input,
141143
return new ReadResult
142144
{
143145
Document = null,
144-
Diagnostic = diagnostic
146+
Diagnostic = diagnostic,
147+
Format = OpenApiConstants.Json,
145148
};
146149
}
147150

src/Microsoft.OpenApi/Reader/ReadResult.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,24 @@ 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>
1923
/// Deconstructs the result for easier assignment on the client application.
2024
/// </summary>
2125
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)
2233
{
2334
document = Document;
2435
diagnostic = Diagnostic;
36+
format = Format;
2537
}
2638
}
2739

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 format) = await OpenApiDocument.LoadAsync(stream, settings: settings);
2424
Assert.False(stream.CanRead);
25+
Assert.Equal(OpenApiConstants.Yaml, format);
2526
}
2627

2728
[Fact]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,8 @@ namespace Microsoft.OpenApi.Reader
20072007
public ReadResult() { }
20082008
public Microsoft.OpenApi.Reader.OpenApiDiagnostic? Diagnostic { get; set; }
20092009
public Microsoft.OpenApi.OpenApiDocument? Document { get; set; }
2010+
public string? Format { get; set; }
20102011
public void Deconstruct(out Microsoft.OpenApi.OpenApiDocument? document, out Microsoft.OpenApi.Reader.OpenApiDiagnostic? diagnostic) { }
2012+
public void Deconstruct(out Microsoft.OpenApi.OpenApiDocument? document, out Microsoft.OpenApi.Reader.OpenApiDiagnostic? diagnostic, out string? format) { }
20112013
}
20122014
}

test/Microsoft.OpenApi.Tests/Reader/OpenApiModelFactoryTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ await File.WriteAllTextAsync(tempFilePathReferrer,
114114
BaseUrl = baseUri,
115115
};
116116
var readResult = await OpenApiDocument.LoadAsync(stream, settings: settings);
117+
Assert.Equal(OpenApiConstants.Json, readResult.Format);
117118
Assert.NotNull(readResult.Document);
118119
Assert.NotNull(readResult.Document.Components);
119120
Assert.Equal(baseUri, readResult.Document.BaseUri);

0 commit comments

Comments
 (0)