You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you set a custom OpenApiOptions.CreateSchemaReferenceId delegate it won't use the custom id from it when setting the $ref for a property that has the same type as its declaring type. The reason for this seems to be this code in OpenApiSchemaService where it uses the default JsonTypeInfo.GetSchemaReferenceId extension method instead of the one from settings:
if (jsonPropertyInfo.PropertyType == jsonPropertyInfo.DeclaringType)
{
return new JsonObject { [OpenApiSchemaKeywords.RefKeyword] = context.TypeInfo.GetSchemaReferenceId() };
}
Expected Behavior
I expected the custom CreateSchemaReferenceId to be used even for nested properties of the same type as its parent which doesn't seem to be the case as far as I can tell.
Steps To Reproduce
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi(options =>
{
options.CreateSchemaReferenceId = _ => "Overridden";
});
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseRouting();
app.UseAuthorization();
app.MapOpenApi();
app.MapControllers();
app.Run();
[ApiController]
[Route("Api/[controller]")]
public class TestController : ControllerBase
{
[HttpGet("")]
[ProducesResponseType<Parent>(StatusCodes.Status200OK)]
public Ok<Parent> Index()
{
return TypedResults.Ok(new Parent { RequiredChild = new() });
}
}
public class Parent
{
public Child? NullableChild { get; init; }
public required Child RequiredChild { get; init; }
public Parent? NestedParent { get; set; }
}
public class Child;
Here I expected the nestedParent $ref to be #/components/schemas/Overridden and not #/components/schemas/Parent since that's what it will reference if you don't override OpenApiOptions.CreateSchemaReferenceId.
Exceptions (if any)
No response
.NET Version
9.0.100-rc.1.24452.12
Anything else?
No response
The text was updated successfully, but these errors were encountered:
gfoidl
added
area-mvc
Includes: MVC, Actions and Controllers, Localization, CORS, most templates
feature-openapi
and removed
needs-area-label
Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
labels
Oct 10, 2024
@marthijn That's not what this issue is about though if I understand you correctly. This issue is about that the custom delegate you set with OpenApiOptions.CreateSchemaReferenceId isn't being used to generate schema reference ids in a certain case.
I admit that my repro isn't very clear about this though because I was experimenting with the nullability stuff you are referencing as well when I was testing this.
I'm not sure but your issue sounds more like it's about a shortcoming with the OpenAPI 3.0 spec regarding nullability which #58619 would fix I think?
Is there an existing issue for this?
Describe the bug
If you set a custom
OpenApiOptions.CreateSchemaReferenceId
delegate it won't use the custom id from it when setting the $ref for a property that has the same type as its declaring type. The reason for this seems to be this code inOpenApiSchemaService
where it uses the defaultJsonTypeInfo.GetSchemaReferenceId
extension method instead of the one from settings:Expected Behavior
I expected the custom
CreateSchemaReferenceId
to be used even for nested properties of the same type as its parent which doesn't seem to be the case as far as I can tell.Steps To Reproduce
/openapi/v1.json:
Here I expected the nestedParent
$ref
to be#/components/schemas/Overridden
and not#/components/schemas/Parent
since that's what it will reference if you don't overrideOpenApiOptions.CreateSchemaReferenceId
.Exceptions (if any)
No response
.NET Version
9.0.100-rc.1.24452.12
Anything else?
No response
The text was updated successfully, but these errors were encountered: