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

Custom Name resolver #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Custom Name resolver #31

wants to merge 1 commit into from

Conversation

RonSijm
Copy link

@RonSijm RonSijm commented Nov 22, 2022

  • Added public Func<Type, string> NameDiscriminatorResolver { get; set; } = type => type.Name; to JsonDiscriminatorSettings to allow custom Name Discriminator Resolver
  • Added JsonKnownTypesSettingsManager To allow type specific config to be injected

…set; } = type => type.Name;` to JsonDiscriminatorSettings to allow custom Name Discriminator Resolver

 - Added JsonKnownTypesSettingsManager<T> To allow type specific config to be injected
@RonSijm
Copy link
Author

RonSijm commented Nov 22, 2022

PR resolves #30

Copy link
Owner

@dmitry-bym dmitry-bym left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for long response
Can you pls create test for the case, also update language version in project to latest
Also have found issue (not yours) with mapper if user specified DiscriminatorAttribute it will use new JsonDiscriminatorSettings to compare but need to use it from JsonKnownTypesSettingsManager.
I think it's all connected so what can be done here
So i think need to create some comparator to resolve that perfect way
but overall looks ok!

var discriminatorSettings = discriminatorAttribute == null
? DefaultDiscriminatorSettings
var discriminatorSettings = discriminatorAttribute == null
? JsonKnownTypesSettingsManager<T>.DefaultDiscriminatorSettings
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never null so it always use settings from generic manager

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be null unless you explicitly set generic settings. For example: JsonKnownTypesSettingsManager<ClassWithDuplicateChild>.DefaultDiscriminatorSettings.NameDiscriminatorResolver = type => type.FullName;

So, first it looks for the attribute, if that is null, it will look for a specific setting for the generic type (<ClassWithDuplicateChild> in the example), if that is also null it will fall back to the default settings.

At least thats how it worked in my unittest

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RonSijm nope its not null cause there is default value there

public static class JsonKnownTypesSettingsManager<T>
    {
        /// <summary>
        /// Type specific default settings for discriminator
        /// </summary>
        // ReSharper disable once StaticMemberInGenericType - Justification: Intentional property should exist on a per-type base
        public static JsonDiscriminatorSettings DefaultDiscriminatorSettings { get; } = new();
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants