Skip to content

feat: add SerializeAsJsonAsync overloads accepting OpenApiJsonWriterS…#2985

Open
Mahdigln wants to merge 1 commit into
microsoft:mainfrom
Mahdigln:feature/serialize-as-json-compact-settings
Open

feat: add SerializeAsJsonAsync overloads accepting OpenApiJsonWriterS…#2985
Mahdigln wants to merge 1 commit into
microsoft:mainfrom
Mahdigln:feature/serialize-as-json-compact-settings

Conversation

@Mahdigln

@Mahdigln Mahdigln commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Description

Add SerializeAsJsonAsync and SerializeAsync overloads that accept OpenApiJsonWriterSettings, enabling callers to produce compact (minified) JSON output without having to parse and re-serialize the result.

Type of Change

  • New feature (non-breaking change which adds functionality)

Related Issue(s)

Closes #2798

Changes Made

  • Add SerializeAsJsonAsync<T>(Stream, OpenApiSpecVersion, OpenApiJsonWriterSettings) overload
  • Add SerializeAsJsonAsync<T>(OpenApiSpecVersion, OpenApiJsonWriterSettings) overload returning string
  • Add SerializeAsync<T>(OpenApiSpecVersion, string, OpenApiWriterSettings?) overload returning string
  • Register the three new signatures in PublicAPI.Unshipped.txt

Testing

  • Unit tests added/updated
  • All existing tests pass

Tests cover:

  • Terse = true via stream overload produces compact JSON
  • Terse = true via string overload produces compact JSON
  • Terse = false via string SerializeAsync overload produces pretty-printed JSON

Checklist

  • My code follows the code style of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Versions applicability

  • My change applies to the version 3.X of the library, if so PR link: (this PR)
  • I have evaluated the applicability of my change against the other versions above.

Additional Notes

The three new overloads intentionally omit the CancellationToken parameter 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 existing SerializeAsync(Stream, OpenApiSpecVersion, string, OpenApiWriterSettings?, CancellationToken) overload directly.

#pragma warning disable RS0027 is applied to four existing shipped methods where the analyzer fires a false positive: the new overloads use distinct types (OpenApiJsonWriterSettings vs CancellationToken) so no call-site ambiguity exists.

Copilot AI review requested due to automatic review settings July 22, 2026 12:38
@Mahdigln
Mahdigln requested a review from a team as a code owner July 22, 2026 12:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 SerializeAsJsonAsync overloads that accept OpenApiJsonWriterSettings for both stream and string outputs.
  • Add a SerializeAsync string-returning overload that accepts OpenApiWriterSettings?.
  • 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.

Comment on lines +24 to +26
#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
Comment on lines +123 to 126
#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

@baywet baywet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

/// <param name="stream">The output stream.</param>
/// <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>
public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, OpenApiJsonWriterSettings settings)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should also accept a cancellation token

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support ability to return compact JSON format when calling SerializeAsJsonAsync

3 participants