Newtonsoft.Json.Schema
Newtonsoft.Json.Schema copied to clipboard
Optional generation of schema definitions
For a particular use case I am using json schemas to describe models, not to validate json against the schema.
When describing a model I am not interested in the definition of related models, because this can lead to a very complex structure and I want to focus in a single object. When describing a complex property it is just fine as-is, mentioning the name of the model it represents, so if you want to see the nested model definition you can ask for another description passing the new model name.
My request would be something like:
var schemaGenerator = new JSchemaGenerator();
schemaGenerator.DefinitionGenerationHandling = DefinitionGenerationHandling.No;
I am using the following workaround, but it has obvious performance implications against a proper solution:
var schema = schemaGenerator.Generate(Type);
var jObject = (JsonConvert.DeserializeObject(schema.ToString()) as Newtonsoft.Json.Linq.JObject);
jObject.Remove("definitions");
...
I suggest trying to remove the constraints on the properties sub-schema. Something like:
public void RemoveComplexPropertyConstraints(JSchema schema, int level = 0)
{
schema.ExtensionData.Remove("definitions");
if (schema.Type.HasValue && schema.Type.Value.HasFlag(JSchemaType.Object))
{
if (level > 0)
{
schema.Properties.Clear();
// TODO: remove other constraints if necessary. e.g. dependencies
}
foreach (var property in schema.Properties)
{
RemoveComplexPropertyConstraints(property.Value, level + 1);
}
}
if (schema.Type.HasValue && schema.Type.Value.HasFlag(JSchemaType.Array))
{
foreach (var item in schema.Items)
{
RemoveComplexPropertyConstraints(item, level);
}
}
}
Not sure if it helps but using generator.SchemaReferenceHandling = SchemaReferenceHandling.None; won't generate definitions.