Skip to content

Commit 00ec76d

Browse files
committed
feat: add SerializeAsJsonAsync overloads accepting OpenApiJsonWriterSettings
1 parent 8f4a60b commit 00ec76d

3 files changed

Lines changed: 123 additions & 1 deletion

File tree

src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System.Globalization;
@@ -21,12 +21,28 @@ public static class OpenApiSerializableExtensions
2121
/// <param name="stream">The output stream.</param>
2222
/// <param name="specVersion">The Open API specification version.</param>
2323
/// <param name="cancellationToken">The cancellation token.</param>
24+
#pragma warning disable RS0027 // The settings overload below has the same parameter count but different types; no ambiguity exists.
2425
public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, CancellationToken cancellationToken = default)
26+
#pragma warning restore RS0027
2527
where T : IOpenApiSerializable
2628
{
2729
return element.SerializeAsync(stream, specVersion, OpenApiConstants.Json, cancellationToken);
2830
}
2931

32+
/// <summary>
33+
/// Serialize the <see cref="IOpenApiSerializable"/> to the Open API document (JSON) using the given stream, specification version and settings.
34+
/// </summary>
35+
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
36+
/// <param name="element">The Open API element.</param>
37+
/// <param name="stream">The output stream.</param>
38+
/// <param name="specVersion">The Open API specification version.</param>
39+
/// <param name="settings">Settings controlling JSON output, including <see cref="OpenApiJsonWriterSettings.Terse"/> for compact formatting.</param>
40+
public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, OpenApiJsonWriterSettings settings)
41+
where T : IOpenApiSerializable
42+
{
43+
return element.SerializeAsync(stream, specVersion, OpenApiConstants.Json, settings, CancellationToken.None);
44+
}
45+
3046
/// <summary>
3147
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document (YAML) using the given stream and specification version.
3248
/// </summary>
@@ -104,7 +120,9 @@ public static Task SerializeAsync<T>(
104120
/// <param name="writer">The output writer.</param>
105121
/// <param name="specVersion">Version of the specification the output should conform to</param>
106122
/// <param name="cancellationToken">The cancellation token.</param>
123+
#pragma warning disable RS0027 // The settings-bearing SerializeAsync overloads below have the same parameter count but different types; no ambiguity exists.
107124
public static Task SerializeAsync<T>(this T element, IOpenApiWriter writer, OpenApiSpecVersion specVersion, CancellationToken cancellationToken = default)
125+
#pragma warning restore RS0027
108126
where T : IOpenApiSerializable
109127
{
110128
Utils.CheckArgumentNull(element);
@@ -142,15 +160,33 @@ public static Task SerializeAsync<T>(this T element, IOpenApiWriter writer, Open
142160
/// <param name="element">The Open API element.</param>
143161
/// <param name="specVersion">The Open API specification version.</param>
144162
/// <param name="cancellationToken">The cancellation token.</param>
163+
#pragma warning disable RS0027 // The settings overload below has the same parameter count but different types; no ambiguity exists.
145164
public static Task<string> SerializeAsJsonAsync<T>(
146165
this T element,
147166
OpenApiSpecVersion specVersion,
148167
CancellationToken cancellationToken = default)
168+
#pragma warning restore RS0027
149169
where T : IOpenApiSerializable
150170
{
151171
return element.SerializeAsync(specVersion, OpenApiConstants.Json, cancellationToken);
152172
}
153173

174+
/// <summary>
175+
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document as a string in JSON format using the given settings.
176+
/// </summary>
177+
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
178+
/// <param name="element">The Open API element.</param>
179+
/// <param name="specVersion">The Open API specification version.</param>
180+
/// <param name="settings">Settings controlling JSON output, including <see cref="OpenApiJsonWriterSettings.Terse"/> for compact formatting.</param>
181+
public static Task<string> SerializeAsJsonAsync<T>(
182+
this T element,
183+
OpenApiSpecVersion specVersion,
184+
OpenApiJsonWriterSettings settings)
185+
where T : IOpenApiSerializable
186+
{
187+
return element.SerializeAsync(specVersion, OpenApiConstants.Json, settings);
188+
}
189+
154190
/// <summary>
155191
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document as a string in YAML format.
156192
/// </summary>
@@ -175,11 +211,13 @@ public static Task<string> SerializeAsYamlAsync<T>(
175211
/// <param name="specVersion">The Open API specification version.</param>
176212
/// <param name="format">Open API document format.</param>
177213
/// <param name="cancellationToken">The cancellation token.</param>
214+
#pragma warning disable RS0027 // The settings overload below has the same parameter count but different types; no ambiguity exists.
178215
public static async Task<string> SerializeAsync<T>(
179216
this T element,
180217
OpenApiSpecVersion specVersion,
181218
string format,
182219
CancellationToken cancellationToken = default)
220+
#pragma warning restore RS0027
183221
where T : IOpenApiSerializable
184222
{
185223
Utils.CheckArgumentNull(element);
@@ -195,5 +233,30 @@ public static async Task<string> SerializeAsync<T>(
195233
return await streamReader.ReadToEndAsync().ConfigureAwait(false);
196234
#endif
197235
}
236+
237+
/// <summary>
238+
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document as a string in the given format using the given settings.
239+
/// </summary>
240+
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
241+
/// <param name="element">The Open API element.</param>
242+
/// <param name="specVersion">The Open API specification version.</param>
243+
/// <param name="format">Open API document format.</param>
244+
/// <param name="settings">Provide configuration settings for controlling writing output.</param>
245+
public static async Task<string> SerializeAsync<T>(
246+
this T element,
247+
OpenApiSpecVersion specVersion,
248+
string format,
249+
OpenApiWriterSettings? settings)
250+
where T : IOpenApiSerializable
251+
{
252+
Utils.CheckArgumentNull(element);
253+
254+
using var stream = new MemoryStream();
255+
await element.SerializeAsync(stream, specVersion, format, settings, CancellationToken.None).ConfigureAwait(false);
256+
stream.Position = 0;
257+
258+
using var streamReader = new StreamReader(stream);
259+
return await streamReader.ReadToEndAsync().ConfigureAwait(false);
260+
}
198261
}
199262
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#nullable enable
22
const Microsoft.OpenApi.OpenApiConstants.JsonSchemaExamplesExtension = "x-jsonschema-examples" -> string!
33
const Microsoft.OpenApi.OpenApiConstants.OaiLicenseIdentifier = "x-oai-license-identifier" -> string!
4+
static Microsoft.OpenApi.OpenApiSerializableExtensions.SerializeAsJsonAsync<T>(this T element, Microsoft.OpenApi.OpenApiSpecVersion specVersion, Microsoft.OpenApi.OpenApiJsonWriterSettings! settings) -> System.Threading.Tasks.Task<string!>!
5+
static Microsoft.OpenApi.OpenApiSerializableExtensions.SerializeAsJsonAsync<T>(this T element, System.IO.Stream! stream, Microsoft.OpenApi.OpenApiSpecVersion specVersion, Microsoft.OpenApi.OpenApiJsonWriterSettings! settings) -> System.Threading.Tasks.Task!
6+
static Microsoft.OpenApi.OpenApiSerializableExtensions.SerializeAsync<T>(this T element, Microsoft.OpenApi.OpenApiSpecVersion specVersion, string! format, Microsoft.OpenApi.OpenApiWriterSettings? settings) -> System.Threading.Tasks.Task<string!>!

test/Microsoft.OpenApi.Tests/Extensions/OpenApiSerializableExtensionsTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,60 @@ public async Task UsesTheTerseOutputInformationFromSettingsNoSettings()
9090

9191
Assert.Equal("{\n \"name\": \"param1\",\n \"in\": \"query\",\n \"description\": \"A sample parameter\",\n \"schema\": {\n \"type\": \"string\"\n }\n}", output);
9292
}
93+
94+
[Fact]
95+
public async Task SerializeAsJsonAsync_WithTerseSettings_WritesToStream()
96+
{
97+
var parameter = new OpenApiParameter
98+
{
99+
Name = "param1",
100+
In = ParameterLocation.Query,
101+
Schema = new OpenApiSchema { Type = JsonSchemaType.String }
102+
};
103+
104+
var settings = new OpenApiJsonWriterSettings { Terse = true };
105+
106+
using var stream = new MemoryStream();
107+
await parameter.SerializeAsJsonAsync(stream, OpenApiSpecVersion.OpenApi3_1, settings);
108+
109+
stream.Position = 0;
110+
using var reader = new StreamReader(stream);
111+
var output = await reader.ReadToEndAsync();
112+
113+
Assert.Equal("{\"name\":\"param1\",\"in\":\"query\",\"schema\":{\"type\":\"string\"}}", output);
114+
}
115+
116+
[Fact]
117+
public async Task SerializeAsJsonAsync_WithTerseSettings_ReturnsCompactString()
118+
{
119+
var parameter = new OpenApiParameter
120+
{
121+
Name = "param1",
122+
In = ParameterLocation.Query,
123+
Schema = new OpenApiSchema { Type = JsonSchemaType.String }
124+
};
125+
126+
var settings = new OpenApiJsonWriterSettings { Terse = true };
127+
128+
var output = await parameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1, settings);
129+
130+
Assert.Equal("{\"name\":\"param1\",\"in\":\"query\",\"schema\":{\"type\":\"string\"}}", output);
131+
}
132+
133+
[Fact]
134+
public async Task SerializeAsync_WithSettings_ReturnsFormattedString()
135+
{
136+
var parameter = new OpenApiParameter
137+
{
138+
Name = "param1",
139+
In = ParameterLocation.Query,
140+
Schema = new OpenApiSchema { Type = JsonSchemaType.String }
141+
};
142+
143+
var settings = new OpenApiJsonWriterSettings { Terse = false };
144+
145+
var output = await parameter.SerializeAsync(OpenApiSpecVersion.OpenApi3_1, OpenApiConstants.Json, settings);
146+
147+
Assert.Equal("{\n \"name\": \"param1\",\n \"in\": \"query\",\n \"schema\": {\n \"type\": \"string\"\n }\n}", output);
148+
}
93149
}

0 commit comments

Comments
 (0)