Skip to content

Ignore DisplayAttribute #1922

Description

@TheBigNeo

Hello

For my use case, I am not allowed to export the Name and Description from the DisplayAttribute when generating the JSON schema.

I can work around this by overriding two functions and then setting the Description to null. (See workaround bellow.)

Would a pull request that adds two new properties for UseDisplayName and UseDisplayDescription to the JsonSchemaGeneratorSettings class (like UseXmlDocumentation) be accepted?
Or is this something you would prefer not to have in the settings?

I’d also like to add a setting for UseDefaultValue.
Since my property has not the same type as the type of the value of the DefaultValue attribute (yes, I know…), I’m getting an exception. But I’ve got a workaround for that too.


Workaround:

protected override void GenerateObject(JsonSchema schema, JsonTypeDescription typeDescription, JsonSchemaResolver schemaResolver)
{
    base.GenerateObject(schema, typeDescription, schemaResolver);
    schema.Description = null;
}

public override void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription typeDescription)
{
    base.ApplyDataAnnotations(schema, typeDescription);
    schema.Description = null;
}
public class IgnoreDefaultValuesContractResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        JsonProperty property = base.CreateProperty(member, memberSerialization);

        // Check if property has DefaultValueAttribute
        var defaultValueAttr = member.GetCustomAttributes(typeof(DefaultValueAttribute), true)
                                     .OfType<DefaultValueAttribute>()
                                     .FirstOrDefault();

        if (defaultValueAttr != null)
        {
            property.DefaultValue = null;
        }

        return property;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions