Comments (5)
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.
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.
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.
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.
Glad you worked it out. I think I might add this as an example in the docs!
from json-everything.
Related Issues (20)
- 'Required' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.RequiredAttribute' and 'Json.Schema.Generation.RequiredAttribute'? HOT 22
- Schema exception {"An item with the same key has already been added. Key: Child"} HOT 9
- Possibly wrong return type HOT 10
- Json.Schema.KeywordExtensions is not thread safe with custom keywords HOT 1
- Update Nuget package meta-data HOT 6
- Error "unknown context value" Raised After Checking for Existence HOT 2
- Online schema validator: Is successful validation of illegal json just a quirk or intentional HOT 5
- System.MissingMethodException for IReadOnlyCollection<JsonNode> after updating from JsonSchema.Net.Generation 4.3.0 to 4.3.0.2 HOT 5
- Can't get DiscriminatorKeyword working HOT 2
- Schema Evaluation fails with "Cannot resolve schema at {uri}" HOT 5
- Could not load file or assembly 'JsonPointer.Net, Version=5.0.0.0, Culture=neutral, PublicKeyToken=17ed63f672b0e278'. The system cannot find the file specified. HOT 10
- IndexOutOfRangeException occurred when evaluating schema with parallel execution. HOT 1
- jsonnet as sister of json-e HOT 3
- Escaping single quote in json path string HOT 8
- Extract data annotation attributes from JsonSchema.Net.Generation into separate project HOT 2
- Is there a way to do JSON validation that does case insensitive matching on property names? HOT 8
- Support language packs in schema playground
- sileo-featured.json
- How to export or save JsonSchema to plaintext? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-everything.