Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Polymorphism DiscriminatorValueSelector work with JsonDerivedTypeAttribute #2948

Open
huiyuanai709 opened this issue Jun 14, 2024 · 1 comment
Labels
help-wanted A change up for grabs for contributions from the community needs investigation question

Comments

@huiyuanai709
Copy link

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

var discriminatorValue = _generatorOptions.DiscriminatorValueSelector(knownTypeDataContract.UnderlyingType)

Additional context

No response

@huiyuanai709
Copy link
Author

Now I use the Approach to get TypeDiscriminator

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");

    Type? SearchParentRecursive<T>(Type type) where T : Attribute
    {
        if(type.BaseType is null || type == typeof(object))
        {
            return null;
        }
        
        var attribute = type.BaseType.GetCustomAttribute<T>();
        if (attribute is null)
        {
            return SearchParentRecursive<T>(type.BaseType);
        }
        
        return type.BaseType;
    }
    options.SelectDiscriminatorValueUsing(subClass => SearchParentRecursive<JsonPolymorphicAttribute>(subClass)!.GetCustomAttributes<JsonDerivedTypeAttribute>().FirstOrDefault(x => x.DerivedType == subClass)!.TypeDiscriminator?.ToString());
});

@martincostello martincostello added needs investigation help-wanted A change up for grabs for contributions from the community labels Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help-wanted A change up for grabs for contributions from the community needs investigation question
Projects
None yet
Development

No branches or pull requests

2 participants