Skip to content

[OpenAPI] Default CreateSchemaReferenceId produces invalid schema keys for array types #66299

@ascott18

Description

@ascott18

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The default CreateSchemaReferenceId on OpenApiOptions generates schema reference IDs containing [] for array types. For example, a return type of ItemResult<string[]> produces the schema key ItemResultOfString[].

The [] characters are invalid in OpenAPI schema keys, which must match ^[a-zA-Z0-9\.\-_]+$. This causes OpenApiDocument.LoadAsync to report a validation error:

The key 'ItemResultOfString[]' in 'schemas' of components MUST match the regular expression '^[a-zA-Z0-9\.\-_]+$'. [#/components]

Expected Behavior

Array types should have valid schema reference IDs, e.g. ItemResultOfStringArray instead of ItemResultOfString[].

Steps To Reproduce

#:sdk Microsoft.NET.Sdk.Web
#:property TargetFramework=net10.0
#:property PublishAot=false
#:package Microsoft.AspNetCore.OpenApi@10.0.5

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

var app = builder.Build();
app.MapOpenApi();
app.MapGet("/api/test", () => new ItemResult<string[]>());

await app.StartAsync();

using var client = new HttpClient();
using var stream = await (await client.GetAsync("http://localhost:5000/openapi/v1.json")).Content.ReadAsStreamAsync();
var result = await Microsoft.OpenApi.OpenApiDocument.LoadAsync(stream, "json");

Console.Error.WriteLine(result.Diagnostic.Errors.Count > 0
    ? $"Errors:\n{string.Join("\n", result.Diagnostic.Errors.Select(e => $"  {e.Message}"))}"
    : "No errors (unexpected).");

await app.StopAsync();

public class ItemResult<T>
{
    public bool WasSuccessful { get; set; }
    public T? Object { get; set; }
}

Run the above file-based app. Observe error in output:

Errors:
  The key 'ItemResultOfString[]' in 'schemas' of components MUST match the regular expression '^[a-zA-Z0-9\.\-_]+$'.

Exceptions (if any)

No response

.NET Version

10.0.201

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    NativeAOTarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcbugThis issue describes a behavior which is not expected - a bug.feature-openapi

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions