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

ContextualFieldInfo throws with generic types #143

Open
ms0815user opened this issue Feb 24, 2024 · 0 comments · May be fixed by #151
Open

ContextualFieldInfo throws with generic types #143

ms0815user opened this issue Feb 24, 2024 · 0 comments · May be fixed by #151

Comments

@ms0815user
Copy link

I am using NJsonSchema (11.0.0) with Namotion.Reflection (3.1.1) and encountered a problem during schema generation. Below is a shorted example of my usecase.

public class Hashid
{
    protected long _id;

    ...
}

public sealed class Hashid<T> : Hashid
    where T : EntityBase
{
    ...
}

public Request
{
    public Hashid RegionId { get; set; } // works
   
    public Hashid<Domain.Entities.User> UserId { get; set; } // fails
}

During the schema generation and exploration of the Request the following exception is thrown:

System.InvalidOperationException: This operation is only valid on generic types.
   at System.RuntimeType.GetGenericTypeDefinition()
   at Namotion.Reflection.ContextualType.<get_Fields>b__36_0(FieldInfo field)
   at System.Linq.Enumerable.SelectArrayIterator`2.Fill(ReadOnlySpan`1 source, Span`1 destination, Func`2 func)
   at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
   at Namotion.Reflection.ContextualType.get_Fields()
   at NJsonSchema.NewtonsoftJson.Generation.NewtonsoftJsonReflectionService.GenerateProperties(JsonSchema schema, ContextualType contextualType, NewtonsoftJsonSchemaGeneratorSettings settings, JsonSchemaGenerator schemaGenerator, JsonSchemaResolver schemaResolver)
...

This happens because in the Fields property of the ContextualType class the TypeInfo is generic but the DeclaringType of the field is not.

grafik

This could be related to #139.

I currently resolved this issue for me by not having the Hashid<T> inherit from Hashid and copy part of the logic.

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 a pull request may close this issue.

1 participant