GithubHelp home page GithubHelp logo

Comments (5)

jonschoning avatar jonschoning commented on May 27, 2024 1

the following code produces the correct output:

    [AttributeUsage(AttributeTargets.Property)]
    public class FormatAttribute : Attribute, IAttributeHandler<FormatAttribute>
    {
        public string Format { get; }

        public FormatAttribute(string format)
        {
            Format = format;
        }

        public void AddConstraints(SchemaGenerationContextBase context, Attribute attribute)
        {
            context.Intents.Add(new FormatIntent(new Format(Format)));
        }
    }

    public class MyTypeRefiner : ISchemaRefiner
    {

        // create a new context for the property
        public void ReplaceProperty(PropertiesIntent propsIntent, string propName, List<Attribute> attributes)
        {
            var property = propsIntent.Properties[propName];
            property.ReferenceCount--;
            propsIntent.Properties[propName] = SchemaGenerationContextCache.Get(property.Type, attributes);

        }
        public void Run(SchemaGenerationContextBase context)
        {
            var propsIntent = (PropertiesIntent)context.Intents[1];
            ReplaceProperty(propsIntent, nameof(MyType.RichText), [new FormatAttribute("richtext")]);
        }

        public bool ShouldRun(SchemaGenerationContextBase context)
        {
            return context.Type.Name == nameof(MyType);
        }
    }
{
    "type": "object",
    "properties": {
        "Id": {
            "type": "string"
        },
        "Name": {
            "type": "string"
        },
        "RichText": {
            "type": "string",
            "format": "richtext"
        }
    }
}

from json-everything.

gregsdennis avatar gregsdennis commented on May 27, 2024

Hey there. What you want is a refiner.

Inside the refiner, check for a TypeIntent with a value of object alongside a PropertiesIntent that defines your RichText property. Then you can add a Format Intent directly to that property.

from json-everything.

jonschoning avatar jonschoning commented on May 27, 2024

with the following refiner:

 public class MyTypeRefiner : ISchemaRefiner
 {
     public void Run(SchemaGenerationContextBase context)
     {
         var propsIntent = (PropertiesIntent)context.Intents[1];
         var richTextField = propsIntent.Properties["RichText"];
         richTextField.Intents.Add(new FormatIntent(new Format("richtext")));
         return;
     }

     public bool ShouldRun(SchemaGenerationContextBase context)
     {
         // var isMyType = ((TypeIntent)context.Intents[0]).Type == SchemaValueType.Object;
         return context.Type.Name == "MyType";
     }
 }

i get

{
    "type": "object",
    "properties": {
        "Id": {
            "$ref": "#/$defs/string"
        },
        "Name": {
            "$ref": "#/$defs/string"
        },
        "RichText": {
            "$ref": "#/$defs/string"
        }
    },
    "$defs": {
        "string": {
            "type": "string",
            "format": "richtext"
        }
    }
}

how do i modify the Run method to avoid creating $ref/$defs, and also only modify the richtextfield and not the others?

from json-everything.

gregsdennis avatar gregsdennis commented on May 27, 2024

Hm... Yeah, it looks like the change is affecting the other properties. The generator reuses the context for all of the properties because the type and attribute set (which is "no attributes") is the same. So what you really need is to create a new context for that property...

But you can't because the context constructors are internal.

What you could do (which isn't very obvious) is call SchemaGenerationContextCache.Get() with the type and a fabricated format attribute (which you'll need to make because I don't have one 🤦). There are a few house-keeping things you'll need to do as well.

var propsIntent = (PropertiesIntent)context.Intents[1];
var oldRichTextField = propsIntent.Properties["RichText"];
oldRichTextField.References--;

// you'll need to create and implement FormatAttribute
var newRichTextField = SchemaGeneratorContextCache.Get(oldRichTextField.Type, new[]{new FormatAttribute("richtext")});

propsIntent.Properties["RichText"] = newRichTextField;

from json-everything.

gregsdennis avatar gregsdennis commented on May 27, 2024

Glad you worked it out. I think I might add this as an example in the docs!

from json-everything.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.