Skip to content

Microsoft.AspNetCore.OpenApi should allow overriding the schema for specific typesΒ #56448

@michael-wolfenden

Description

@michael-wolfenden

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Using Microsoft.AspNetCore.OpenApi -Version 9.0.0-preview.5.24306.11

The following code:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();

var app = builder.Build();
app.MapOpenApi();

app.MapGet("/{orderId:long}", (long orderId) => orderId);

app.Run();

correctly generates an OpenAPI specification with an int64 parameter and an int64 response

{
  "openapi": "3.0.1",
  "info": {
    "title": "...",
    "version": "1.0.0"
  },
  "paths": {
    "/{orderId}": {
      "get": {
        "tags": [],
        "parameters": [
          {
            "name": "orderId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "integer",
                  "format": "int64"
                }
              }
            }
          }
        }
      }
    }
  },
  "tags": []
}

If I want to use a strongly typed Id for OrderId instead of a primitive,

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();

var app = builder.Build();
app.MapOpenApi();

app.MapGet("/{orderId:long}", (OrderId orderId) => orderId);

app.Run();

public readonly record struct OrderId(long Value) : IParsable<OrderId>;

The generated OpenAPI specification has a with an int64 / object hybrid parameter (I assume due the long constraint) and an object response.

{
  "openapi": "3.0.1",
  "info": {
    "title": "...",
    "version": "1.0.0"
  },
  "paths": {
    "/{orderId}": {
      "get": {
        "tags": [],
        "parameters": [
          {
            "name": "orderId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "properties": {
                "value": {
                  "type": "integer",
                  "format": "int64"
                }
              },
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "value": {
                      "type": "integer",
                      "format": "int64"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "tags": []
}

I would prefer to map OrderId as a primitive int64 as per the first example.

Describe the solution you'd like

Squashbuckle has the ability to override the schema for specific types, for example

services.AddSwaggerGen(c =>
{
    ...
    c.MapType<OrderId>(() => new OpenApiSchema { Type = "integer" });
};

I would like to see the same functionality in Microsoft.AspNetCore.OpenApi

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThis issue tracks updating documentationarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions