Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

[Question]: Polymorphism DiscriminatorValueSelector work with JsonDerivedTypeAttribute

Open huiyuanai709 opened this issue 8 months ago • 1 comments

What are you wanting to achieve?

Try to polymorphism work with source generator JsonDerivedTypeAttribute. The DiscriminatorValueSelector func only have subClass info

public Func<Type, string> DiscriminatorValueSelector { get; set; }

but the JsonDerivedTypeAttribute is on the baseClass

builder.Services.AddSwaggerGen(options =>
{
    options.SchemaGeneratorOptions.UseOneOfForPolymorphism = true;
    options.SchemaGeneratorOptions.SubTypesSelector = baseType =>
        baseType.GetCustomAttributes<JsonDerivedTypeAttribute>().Select(x => x.DerivedType);
    options.SelectDiscriminatorNameUsing(baseType => baseType.GetCustomAttribute<JsonPolymorphicAttribute>()?.TypeDiscriminatorPropertyName ?? "$type");
    options.SelectDiscriminatorValueUsing(); // todo
});

image

What code or approach do you have so far?

var discriminatorValue = _generatorOptions.DiscriminatorValueSelector(knownTypeDataContract.UnderlyingType)

at https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/a97c53fd4dfe1a2ca2e2607959cf6c002b02c2e0/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs#L473

Additional context

No response

huiyuanai709 avatar Jun 14 '24 03:06 huiyuanai709