skip to content

When you upgrade an ASP.NET Core 9 API with custom OpenAPI transformers to .NET 10, what changes in the generated document and what breaks?

level: seniorimportance: should knowfreq 35%

answer

  1. OpenApiVersion default is now OpenApi3_1
  2. nullable becomes a type array with null
  3. Microsoft.OpenApi 2: interfaces, JsonSchemaType flags
  4. OpenApiAny replaced by JsonNode
  5. WithOpenApi obsolete as ASPDEPR002

basics

~20 s

ASP.NET Core 10 emits OpenAPI 3.1 by default and builds on Microsoft.OpenApi 2, so transformers using Nullable, OpenApiAny, string schema types or Reference objects must be rewritten, even if OpenApiVersion is set back to OpenApi3_0.

solid answer

~30 s

Two things change. The **document**: `OpenApiOptions.OpenApiVersion` now defaults to `OpenApiSpecVersion.OpenApi3_1`, so schemas follow JSON Schema 2020-12 and a nullable property is written as a `type` array containing `null` instead of `nullable: true`. Consumers whose tooling reads only 3.0 need `OpenApiVersion = OpenApiSpecVersion.OpenApi3_0`. The **object model**: the generator moved to Microsoft.OpenApi 2, whose types live in `Microsoft.OpenApi`, expose interfaces such as `IOpenApiSchema` with concrete and `*Reference` implementations, drop `OpenApiSchema.Nullable` in favour of the `JsonSchemaType.Null` flag, and replace `OpenApiAny` with `JsonNode`. Transformers break at compile time regardless of the version you serialize. Also, `WithOpenApi()` is obsolete (`ASPDEPR002`), replaced by `WithSummary`, `WithDescription` and `AddOpenApiOperationTransformer`.

code

csharp · 22 lines
csharp
using System.Text.Json.Nodes;
using Microsoft.OpenApi;              // was Microsoft.OpenApi.Models

builder.Services.AddOpenApi(options =>
{
    // Keep 3.0 only for a consumer whose tooling cannot read 3.1
    options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_0;

    options.AddSchemaTransformer((schema, context, ct) =>
    {
        if (context.JsonTypeInfo.Type == typeof(FlightStatus))
        {
            // .NET 9 used OpenApiObject / OpenApiString here
            schema.Example = new JsonObject
            {
                ["flightNumber"] = "KJ1042",
                ["gate"] = "B12"
            };
        }
        return Task.CompletedTask;
    });
});

go deeper

for a junior

Recall that .NET 10 serves OpenAPI 3.1 by default and that WithOpenApi is obsolete there.

for a middle

Explain how 3.1 writes nullable types and which option or build switch returns a document to 3.0.

for a senior

Separate the output version from the object model break: list what fails in transformers under Microsoft.OpenApi 2 and plan the upgrade by diffing documents per consumer.

for a principal

Decide per published document whether consumers can absorb 3.1 now, and how long a 3.0 pin is allowed to live before it is removed.

## Two separate changes Upgrading a .NET 9 API that uses the built-in `Microsoft.AspNetCore.OpenApi` generator to .NET 10 changes two things that are easy to conflate: 1. the **OpenAPI version** of the document the app serves, and 2. the **C# object model** your transformers are written against. You can undo the first with one option. You cannot undo the second. ## The document: 3.1 by default `OpenApiOptions.OpenApiVersion` is initialized to `OpenApiSpecVersion.OpenApi3_1` in ASP.NET Core 10. The XML comment on that property in the 10.0 source still says the default is 3.0; the initializer and the release notes say 3.1, and the initializer is what runs. OpenAPI 3.1 aligns schemas with **JSON Schema draft 2020-12**: | Aspect | OpenAPI 3.0 output | OpenAPI 3.1 output | |---|---|---| | nullable `string?` property | `type: string` with `nullable: true` | `type` array of `string` and `null` | | schema dialect | OpenAPI's subset of JSON Schema | JSON Schema 2020-12 | | how to get it in .NET 10 | set `OpenApiVersion = OpenApiSpecVersion.OpenApi3_0` | default | If a consumer's client generator, gateway import or contract test reads only 3.0, set the option per document. For build-time generation the matching switch is `--openapi-version` in the `OpenApiGenerateDocumentsOptions` MSBuild property. ## The object model: Microsoft.OpenApi 2 To write 3.1 the generator moved to **Microsoft.OpenApi 2**, a breaking major version. Transformers compiled against .NET 9 fail for reasons unrelated to the output version: - **Namespace.** Types moved from `Microsoft.OpenApi.Models` to `Microsoft.OpenApi`. - **Interfaces.** Document entities are typed as interfaces, for example `IOpenApiSchema`, with a concrete `OpenApiSchema` for inline content and `OpenApiSchemaReference` for a `$ref`. You mutate the concrete type. - **No `Nullable`.** `OpenApiSchema.Nullable` is gone; nullability is the `JsonSchemaType.Null` flag inside `Type`, which is now the `JsonSchemaType` flags enum rather than a string. - **No `OpenApiAny`.** Examples and defaults are `JsonNode` values, so `new OpenApiString("KJ1042")` becomes `JsonValue.Create("KJ1042")`. - **References.** `Reference = new OpenApiReference { ... }` is replaced by reference classes such as `OpenApiSecuritySchemeReference("Bearer", document)`. - **HTTP methods** are `HttpMethod` objects rather than an enum, and some former properties now live in `Metadata`. The release notes are explicit that these changes are needed **even when the app keeps generating 3.0**. Swashbuckle.AspNetCore 10 made the same move, so its filters break in the same way. ## WithOpenApi is obsolete `WithOpenApi()` and `WithOpenApi(op => ...)` carry `[Obsolete]` with diagnostic `ASPDEPR002` in .NET 10. Its documentation says it does not integrate with the built-in generator and was aimed at Swashbuckle. Replace it with: - `WithSummary`, `WithDescription`, `WithName` and `Produces` for ordinary metadata; - `AddOpenApiOperationTransformer(...)` for anything that edits the operation object directly. ## Other differences you will see in the diff - The 10.0 release notes point out that with the web default `NumberHandling` of `AllowReadingFromString`, `int` and `long` properties are described without `type: integer` and with a digits-only `pattern`; setting `NumberHandling` to `Strict` restores `type: integer`. - Component schemas are sorted by name, so a document diff can reorder entries without any real change. ## Additions you get for free - Serve **YAML** by giving `MapOpenApi` a pattern ending in `.yaml` or `.yml`; the response content type is `text/plain+yaml`. - **XML documentation comments** flow into the document once `GenerateDocumentationFile` is on. - `IOpenApiDocumentProvider` is resolvable from DI, keyed by document name, so a test or tool can read the document without an HTTP request. ## An upgrade checklist 1. Retarget to `net10.0`, update packages and compile; fix every transformer the compiler flags. 2. Replace `WithOpenApi` calls and silence nothing with a pragma. 3. Diff the old and new documents and decide, per consumer, whether 3.1 is acceptable. 4. Pin `OpenApiVersion` to 3.0 only for documents whose consumers need it, and record why.

  • Does setting OpenApiVersion to OpenApi3_0 let old .NET 9 transformers compile unchanged?
    No. The option only chooses how the document is serialized. Transformers are compiled against Microsoft.OpenApi 2 either way, so `Nullable`, `OpenApiAny`, string `Type` values and `OpenApiReference` must be replaced regardless of the output version.
  • How do you check for nullability in a .NET 10 schema transformer?
    Test the flags enum: `schema.Type?.HasFlag(JsonSchemaType.Null) == true`. To make a schema nullable, OR the flag in with `schema.Type |= JsonSchemaType.Null`. There is no `Nullable` property on `OpenApiSchema` any more.
  • What replaces WithOpenApi(op => { op.Summary = ...; return op; }) in .NET 10?
    Use `WithSummary(...)` for the summary and other metadata calls for names, descriptions and responses. For edits that need the operation object, chain `AddOpenApiOperationTransformer((operation, context, ct) => ...)` on the endpoint.

  • Setting OpenApiVersion back to OpenApi3_0 lets .NET 9 transformers compile unchanged.
  • In .NET 10 you still mark a schema nullable with OpenApiSchema.Nullable = true.
  • The XML comment on OpenApiVersion says 3.0, so 3.0 is still the default.
  • WithOpenApi() is the recommended way to customize one operation in .NET 10.
  • Only Microsoft.AspNetCore.OpenApi moved to Microsoft.OpenApi 2; Swashbuckle filters are unaffected.

Assumes ASP.NET Core 10 against 9: OpenApiVersion defaults to OpenApi3_1, the generator uses Microsoft.OpenApi 2, and WithOpenApi is obsolete as ASPDEPR002.