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

C#11 required member support when generating a schema from a type #1672

Open
wuzzeb opened this issue Jan 30, 2024 · 0 comments
Open

C#11 required member support when generating a schema from a type #1672

wuzzeb opened this issue Jan 30, 2024 · 0 comments

Comments

@wuzzeb
Copy link

wuzzeb commented Jan 30, 2024

C# 11 added a required member and starting in .NET8, System.Text.Json now has full support for deserializing required members, giving an error if the member is missing. The compiler creates a System.Runtime.CompilerServices.RequiredMemberAttribute so JsonSchemaGenerator should check for this attribute in addition to the other required attributes.

I was able to temporarily support this by the following custom schema processor, but it is slightly hacky. I couldn't find a way of determining the final json property name from the Namotion ContextualPropertyInfo, is there one? I instead just re-looked up the JsonName attribute (only a few properties in our codebase have that name attribute, most are just named directly). Also, I had another hack to determine if it was a derived type by looking at AllOf... but it works for our schema.

    public class RequiredModifierSchemaProcessor : ISchemaProcessor
    {
      public void Process(SchemaProcessorContext ctx)
      {
        foreach (var prop in ctx.ContextualType.Properties)
        {
          if (prop.PropertyInfo.DeclaringType != ctx.ContextualType.Type)
          {
            continue;
          }
          if (prop.GetAttribute<System.Runtime.CompilerServices.RequiredMemberAttribute>(false) != null)
          {
            string name = prop.Name;
            var jsonNameAttr = prop.GetAttribute<System.Text.Json.Serialization.JsonPropertyNameAttribute>(
              false
            );
            if (jsonNameAttr != null)
            {
              name = jsonNameAttr.Name;
            }
            if (ctx.Schema.AllOf.Count > 1)
            {
              ctx.Schema.AllOf.ElementAt(1).RequiredProperties.Add(name);
            }
            else
            {
              ctx.Schema.RequiredProperties.Add(name);
            }
          }
        }
      }
    }
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

No branches or pull requests

1 participant