feat: add SerializeAsJsonAsync overloads accepting OpenApiJsonWriterS…#2985
feat: add SerializeAsJsonAsync overloads accepting OpenApiJsonWriterS…#2985Mahdigln wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds new serialization overloads to allow callers to supply OpenApiJsonWriterSettings / OpenApiWriterSettings (notably enabling compact/minified JSON via Terse = true) when serializing OpenAPI elements to streams or strings, avoiding the need to post-process JSON output.
Changes:
- Add
SerializeAsJsonAsyncoverloads that acceptOpenApiJsonWriterSettingsfor both stream and string outputs. - Add a
SerializeAsyncstring-returning overload that acceptsOpenApiWriterSettings?. - Add unit tests validating compact vs pretty JSON output, and register new APIs in
PublicAPI.Unshipped.txt.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/Microsoft.OpenApi.Tests/Extensions/OpenApiSerializableExtensionsTests.cs | Adds tests for compact JSON via new settings-based overloads and formatted JSON via the new string-returning overload. |
| src/Microsoft.OpenApi/PublicAPI.Unshipped.txt | Registers the newly added public overload signatures. |
| src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs | Introduces the new overloads and adds RS0027 suppressions around existing shipped methods. |
| #pragma warning disable RS0027 // The settings overload below has the same parameter count but different types; no ambiguity exists. | ||
| public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, CancellationToken cancellationToken = default) | ||
| #pragma warning restore RS0027 |
| #pragma warning disable RS0027 // The settings-bearing SerializeAsync overloads below have the same parameter count but different types; no ambiguity exists. | ||
| public static Task SerializeAsync<T>(this T element, IOpenApiWriter writer, OpenApiSpecVersion specVersion, CancellationToken cancellationToken = default) | ||
| #pragma warning restore RS0027 | ||
| where T : IOpenApiSerializable |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs:24
- This RS0027 suppression comment says the settings overload below has the same parameter count, but the current settings overload has an extra CancellationToken parameter, so the comment is inaccurate (even if the suppression itself is still needed). Please update the rationale comment to match the actual overload set.
#pragma warning disable RS0027 // The settings overload below has the same parameter count but different types; no ambiguity exists.
| public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, OpenApiJsonWriterSettings settings, CancellationToken cancellationToken) | ||
| where T : IOpenApiSerializable | ||
| { | ||
| return element.SerializeAsync(stream, specVersion, OpenApiConstants.Json, settings, cancellationToken); | ||
| } |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs:43
- The newly added overloads include an (optional)
CancellationTokenparameter, but the PR description says these overloads intentionally omitCancellationTokento avoid RS0026/RS0027. Please align the implementation/API surface with the stated intent (either removeCancellationTokenfrom the new overloads and adjust tests/PublicAPI entries, or update the PR description and consider whether the warning suppressions are still necessary).
/// <param name="cancellationToken">The cancellation token.</param>
#pragma warning disable RS0026 // Intentional overload alongside the CancellationToken-only variant; types are unambiguous.
public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, OpenApiJsonWriterSettings settings, CancellationToken cancellationToken = default)
#pragma warning restore RS0026
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Xunit; |
| static Microsoft.OpenApi.OpenApiSerializableExtensions.SerializeAsJsonAsync<T>(this T element, Microsoft.OpenApi.OpenApiSpecVersion specVersion, Microsoft.OpenApi.OpenApiJsonWriterSettings! settings, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<string!>! | ||
| static Microsoft.OpenApi.OpenApiSerializableExtensions.SerializeAsJsonAsync<T>(this T element, System.IO.Stream! stream, Microsoft.OpenApi.OpenApiSpecVersion specVersion, Microsoft.OpenApi.OpenApiJsonWriterSettings! settings, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! | ||
| static Microsoft.OpenApi.OpenApiSerializableExtensions.SerializeAsync<T>(this T element, Microsoft.OpenApi.OpenApiSpecVersion specVersion, string! format, Microsoft.OpenApi.OpenApiWriterSettings? settings, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<string!>! |
Description
Add
SerializeAsJsonAsyncandSerializeAsyncoverloads that acceptOpenApiJsonWriterSettings, enabling callers to produce compact (minified) JSON output without having to parse and re-serialize the result.Type of Change
Related Issue(s)
Closes #2798
Changes Made
SerializeAsJsonAsync<T>(Stream, OpenApiSpecVersion, OpenApiJsonWriterSettings)overloadSerializeAsJsonAsync<T>(OpenApiSpecVersion, OpenApiJsonWriterSettings)overload returningstringSerializeAsync<T>(OpenApiSpecVersion, string, OpenApiWriterSettings?)overload returningstringPublicAPI.Unshipped.txtTesting
Tests cover:
Terse = truevia stream overload produces compact JSONTerse = truevia string overload produces compact JSONTerse = falsevia stringSerializeAsyncoverload produces pretty-printed JSONChecklist
Versions applicability
Additional Notes
The three new overloads intentionally omit the
CancellationTokenparameter to avoid RS0026/RS0027 analyzer violations that arise from adding optional-parameter overloads alongside existing shipped ones. Callers who need both custom settings and cancellation support can use the existingSerializeAsync(Stream, OpenApiSpecVersion, string, OpenApiWriterSettings?, CancellationToken)overload directly.#pragma warning disable RS0027is applied to four existing shipped methods where the analyzer fires a false positive: the new overloads use distinct types (OpenApiJsonWriterSettingsvsCancellationToken) so no call-site ambiguity exists.