feat: add SerializeAsJsonAsync overloads accepting OpenApiJsonWriterS…#2985
feat: add SerializeAsJsonAsync overloads accepting OpenApiJsonWriterS…#2985Mahdigln wants to merge 2 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); | ||
| } |
| /// <param name="specVersion">The Open API specification version.</param> | ||
| /// <param name="settings">Settings controlling JSON output, including <see cref="OpenApiJsonWriterSettings.Terse"/> for compact formatting.</param> | ||
| /// <param name="cancellationToken">The cancellation token.</param> | ||
| public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, OpenApiJsonWriterSettings settings, CancellationToken cancellationToken) |
There was a problem hiding this comment.
the cancellation token should be optional
| public static Task<string> SerializeAsJsonAsync<T>( | ||
| this T element, | ||
| OpenApiSpecVersion specVersion, | ||
| OpenApiJsonWriterSettings settings) |
There was a problem hiding this comment.
missing the cancellation token here
| this T element, | ||
| OpenApiSpecVersion specVersion, | ||
| string format, | ||
| OpenApiWriterSettings? settings) |
There was a problem hiding this comment.
missing the cancellation token here
| var settings = new OpenApiJsonWriterSettings { Terse = true }; | ||
|
|
||
| using var stream = new MemoryStream(); | ||
| await parameter.SerializeAsJsonAsync(stream, OpenApiSpecVersion.OpenApi3_1, settings, CancellationToken.None); |
There was a problem hiding this comment.
we should be passing the test context cancellation token instead
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.