Skip to content

feat: add JsonConverter for OpenApiSchema System.Text.Json serialization#2915

Merged
baywet merged 8 commits into
microsoft:mainfrom
Mahdigln:feature/openapi-schema-json-converter
Jul 2, 2026
Merged

feat: add JsonConverter for OpenApiSchema System.Text.Json serialization#2915
baywet merged 8 commits into
microsoft:mainfrom
Mahdigln:feature/openapi-schema-json-converter

Conversation

@Mahdigln

@Mahdigln Mahdigln commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Description

Adds OpenApiSchemaJsonConverter, a System.Text.Json.JsonConverter<OpenApiSchema> that produces clean OpenAPI wire format output instead of the default verbose reflection-based serialization.

Type of Change

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

Related Issue(s)

Closes #2299

Changes Made

  • Added OpenApiSchemaJsonConverter in src/Microsoft.OpenApi/Converters/
  • Supports all OpenAPI versions: 2.0, 3.0, 3.1 (default), 3.2
  • Added Read (deserialization) support in addition to Write (serialization)
  • Updated PublicAPI.Unshipped.txt with new public API surface

Testing

  • Unit tests added/updated
  • All existing tests pass

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 2.X of the library, if so PR link:
  • My change applies to the version 3.X of the library, if so PR link:
  • I have evaluated the applicability of my change against the other versions above.

Additional Notes

Usage example:

var options = new JsonSerializerOptions();
options.Converters.Add(new OpenApiSchemaJsonConverter());

// Serialize
var json = JsonSerializer.Serialize(schema, options);

// Deserialize
var schema = JsonSerializer.Deserialize<OpenApiSchema>(json, options);

// Target a specific OpenAPI version
options.Converters.Add(new OpenApiSchemaJsonConverter(OpenApiSpecVersion.OpenApi3_0));

@Mahdigln Mahdigln requested a review from a team as a code owner June 29, 2026 10:11

@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!

Let's a couple of comments to help get this merged

Comment thread test/Microsoft.OpenApi.Tests/Converters/OpenApiSchemaJsonConverterTests.cs Outdated
Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs Outdated
Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs Outdated
Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs
Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs Outdated
Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs Outdated
Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs Outdated
@Mahdigln Mahdigln requested a review from baywet June 30, 2026 05:24
Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs Outdated

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

This PR introduces first-class System.Text.Json (de)serialization support for OpenApiSchema by adding a dedicated JsonConverter<OpenApiSchema> that emits/consumes OpenAPI wire-format JSON instead of default reflection-based output, addressing the need described in #2299.

Changes:

  • Added OpenApiSchemaJsonConverter with version-targeted serialization (defaulting to OpenAPI 3.2) and deserialization support.
  • Added unit tests validating basic serialize/deserialize and version-specific behavior.
  • Updated public API baselines to include the new converter; also includes an encoding-only change to global.json.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs New converter implementation for OpenApiSchema using OpenAPI wire-format I/O.
test/Microsoft.OpenApi.Tests/Converters/OpenApiSchemaJsonConverterTests.cs New unit tests covering basic serialization/deserialization scenarios.
src/Microsoft.OpenApi/PublicAPI.Unshipped.txt Declares the newly added public converter API surface.
global.json Introduces a UTF-8 BOM at file start (encoding-only change).

Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs
Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs
Comment thread global.json Outdated
Comment thread test/Microsoft.OpenApi.Tests/Converters/OpenApiSchemaJsonConverterTests.cs Outdated
@Mahdigln Mahdigln requested a review from baywet June 30, 2026 12:59

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs
Comment thread src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs
Comment thread global.json Outdated
baywet added 4 commits July 2, 2026 10:51
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
Signed-off-by: Vincent Biret <vibiret@microsoft.com>

@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.

Thank you for making the changes!

@baywet baywet enabled auto-merge (squash) July 2, 2026 15:04
@baywet baywet merged commit 2f8b3d2 into microsoft:main Jul 2, 2026
9 checks passed
@baywet baywet mentioned this pull request Jul 2, 2026
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.

OpenApiSchema System.Text.Json (De)Serialization

4 participants