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

Add Description Attribute to JsonSchema.Net.Generation #141

Closed
DanielHabenicht opened this issue Jul 8, 2021 · 4 comments · Fixed by #144
Closed

Add Description Attribute to JsonSchema.Net.Generation #141

DanielHabenicht opened this issue Jul 8, 2021 · 4 comments · Fixed by #144
Labels
enhancement New feature in an existing library

Comments

@DanielHabenicht
Copy link

Environment

  • Nuget Package: JsonSchema.Net.Generation
  • Nuget Version: 1.7.1
  • OS: Windows
  • .Net Target: Core 5

Can this library make doing something simpler?
A common use case is to add Descriptions to an Property:

        [Description("The title of the event.")]
        public string Title { get; init; }

It would be great if json-everything would support this out of the box.

Describe alternatives you've considered
Implement it yourself, e.g.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Json.Schema;
using Json.Schema.Generation;

namespace EnoCore.Models.Schema
{
    [AttributeUsage(AttributeTargets.Property)]
    public class Description : Attribute, IAttributeHandler
    {
        public string Value { get; }

        public Description(string value)
        {
            Value = value;
        }

        void IAttributeHandler.AddConstraints(SchemaGeneratorContext context)
        {
            var attribute = context.Attributes.OfType<Description>().FirstOrDefault();
            if (attribute == null) return;

            context.Intents.Add(new DescriptionIntent(attribute.Value));
        }
    }

    public class DescriptionIntent : ISchemaKeywordIntent
    {
        public string Description { get; private set; }

        public DescriptionIntent(string description)
        {
            Description = description;
        }

        public void Apply(JsonSchemaBuilder builder) => builder.Description(this.Description);
    }
}

Additional context
I am happy to make a PR

@DanielHabenicht DanielHabenicht added the enhancement New feature in an existing library label Jul 8, 2021
@gregsdennis
Copy link
Owner

gregsdennis commented Jul 8, 2021

This is a good addition. We should probably add

  • title
  • default

as well. These and examples make up the metadata keywords. Not sure how examples would do as an attribute, though.

(deprecated is actually handled by [Obsolete] currently)


The only reason I didn't put these in at first was that they don't do anything for validation, which I figured was the primary purpose for generating a schema.

@DanielHabenicht
Copy link
Author

ok, I will come up with a PR next weekend.

By the way is there a way to use the maybe already specified Attributes from System.ComponentModel.DataAnnotations (e.g. Required or [Range(minimum: 0, maximum: 2)])?

@gregsdennis
Copy link
Owner

gregsdennis commented Jul 11, 2021

Required is already supported, and I already have Minimum and Maximum defined. I think these are more explicit.

Additionally, I found a StackOverflow question where they didn't want to use JsonIgnore because it interfered with using the same model for serialization. As such, I think it's good to have explicitly defined attributes.

I think what I'm going to do is add an option to enable DataAnnotations support. I can then support things like Range when that's turned on.

Don't worry about a PR, I've got it. Thanks.

@gregsdennis
Copy link
Owner

gregsdennis commented Jul 11, 2021

I've opened #143 for the DataAnnotations support. It's a bit more complex than just adding them.

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

Successfully merging a pull request may close this issue.

2 participants