Skip to content

Migrate NSwag core to System.Text.Json (v15) - [WIP]#5355

Open
RicoSuter wants to merge 8 commits intomasterfrom
feature/migrate-core-to-stj
Open

Migrate NSwag core to System.Text.Json (v15) - [WIP]#5355
RicoSuter wants to merge 8 commits intomasterfrom
feature/migrate-core-to-stj

Conversation

@RicoSuter
Copy link
Copy Markdown
Owner

No description provided.

Phase 0: Add UseLocalNJsonSchemaProjects switch to Directory.Build.props
and conditional NJsonSchema project/NuGet references to all 6 projects.

Phase 1: Add 5 OpenAPI document serialization snapshot tests using Verify
to lock down behavior before and after migration.

Phase 2: Migrate all 28 NSwag.Core model files from Newtonsoft attributes
to STJ equivalents ([JsonPropertyName], [JsonIgnore], [JsonPropertyOrder]).
Rewrite OpenApiPathItemConverter for STJ. Add EnumMemberStringEnumConverter
for [EnumMember]-aware enum serialization. Add OpenApiParameterJsonConverter
to handle the required bool/array property collision between OpenApiParameter
and JsonSchema. Add parameterless JsonConstructor to OpenApiComponents for
STJ deserialization support.

58/63 NSwag.Core tests pass (4 remaining failures in reference resolution
and external file loading - see docs/plans/findings.md).
- Remove [JsonRequired] from OpenApiOAuthFlow.Scopes (real-world specs
  have OAuth flows without scopes, Newtonsoft was more lenient)
- Update findings.md to reflect 0 remaining failures
- Remove temp debug test
- NSwagDocumentBase: Replace JObject/JToken with JsonNode/JsonObject,
  JsonConvert with JsonSerializer, JsonSerializerSettings with
  JsonSerializerOptions (CamelCase naming, JsonStringEnumConverter)
- 10 command files: mechanical [JsonProperty] → [JsonPropertyName],
  [JsonIgnore] namespace swap, JsonConvert → JsonSerializer
- Core.Yaml: Replace ExpandoObjectConverter with recursive JsonNode
  to ExpandoObject conversion for YamlDotNet compatibility
- AspNetCore: Replace JsonConvert with JsonSerializer in SwaggerUi
  settings and JsonExceptionFilterAttribute, rewrite exception
  serialization without Newtonsoft JsonExceptionConverter
- Remove NJsonSchema.NewtonsoftJson package/project reference
- Replace unused Newtonsoft using with System.Text.Json.Serialization
- Replace JsonConvert.SerializeObject with JsonSerializer.Serialize
- New NSwag.Generation.NewtonsoftJson project with UseNewtonsoftJson()
  extension method and NewtonsoftJsonSettingsResolver for ASP.NET Core
  MvcNewtonsoftJsonOptions detection
- Remove Newtonsoft using from AspNetCoreOpenApiDocumentGenerator,
  deprecate GetJsonSerializerSettings in favor of new package
- Remove last Newtonsoft usings from CodeGeneration projects

Only NSwag.AspNet.WebApi (legacy) and NSwagStudio (WPF) retain
Newtonsoft dependencies. All other NSwag projects are now STJ-only.
…ttings

- Enable UseLocalNJsonSchemaProjects=true in Directory.Build.props
  for development on this branch
- Replace NewtonsoftJsonSchemaGeneratorSettings with
  SystemTextJsonSchemaGeneratorSettings across 26 test files
- Update snapshot files to reflect STJ-based schema generation output
- Remove orphaned Newtonsoft-only test class and its snapshots
- Add JsonElement safety net in CSharpValueGenerator for large specs
@RicoSuter RicoSuter changed the title Migrate NSwag core to System.Text.Json (v15) Migrate NSwag core to System.Text.Json (v15) - [WIP] Apr 5, 2026
…nsoft removal

- Fix null reference in NSwagDocumentBase (lines 181, 518)
- Cache JsonSerializerOptions as static readonly in NSwagDocumentBase
- Add depth limit and try-catch to JsonExceptionFilterAttribute serialization
- Remove hard Newtonsoft dependency from NSwagServiceCollectionExtensions (reflection-based)
- Fix YAML boolean/number type loss (WithAttemptingUnquotedStringTypeDeserialization)
- Migrate YamlDocumentTests from Newtonsoft.Json.Linq to System.Text.Json.Nodes
- Add parameter IsRequired round-trip test and YAML round-trip test
- Add missing NSwag.Generation.NewtonsoftJson reference to AspNetCore.Tests
- Fix QueryParametersTests JToken assertion for STJ
- Bump Namotion.Reflection to 3.5.0
- Add review.md and migration.md, remove findings.md
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.

1 participant