使用Newtonsoft.Json.Schema是否有办法拦截“定义”名称的生成?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Newtonsoft.Json.Schema是否有办法拦截“定义”名称的生成?相关的知识,希望对你有一定的参考价值。
鉴于我有以下类型:
public class MyClass
{
public MyGenericClass<bool> Gen { get; set; }
}
public class MyGenericClass<T>
{
[Required]
public T Prop { get; set; }
}
我这样设置架构生成器
var generator = new JSchemaGenerator
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
SchemaIdGenerationHandling = SchemaIdGenerationHandling.None
};
generator.GenerationProviders.Add(
new StringEnumGenerationProvider { CamelCaseText = true });
我得到以下架构
{
"definitions": {
"MyGenericClass<Boolean>": {
"type": [
"object",
"null"
],
"properties": {
"prop": {
"type": "boolean"
}
},
"required": [
"prop"
]
}
},
"type": "object",
"properties": {
"gen": {
"$ref": "#/definitions/MyGenericClass<Boolean>"
}
},
"required": [
"gen"
]
}
除了我需要共享模式的服务不喜欢尖括号“<>”之外,这很棒。
有没有办法拦截定义名称的创建,所以我可以删除<>?请注意,其他SchemaIdGenerationHandling都不起作用。
令人讨厌的东西
public class GenericSchemaProvider : JSchemaGenerationProvider
{
public override bool CanGenerateSchema(JSchemaTypeGenerationContext context)
{
return context.ObjectType.IsGenericType && !context.ObjectType.IsAssignableFrom(typeof(IEnumerable));
}
public override JSchema GetSchema(JSchemaTypeGenerationContext context)
{
return CanGenerateSchema(context) ? CreateSchemaWithCustomId(context, context.Required) : null;
}
private static JSchema CreateSchemaWithCustomId(JSchemaTypeGenerationContext context, Required required)
{
var schema = context.Generator.Generate(context.ObjectType, required != Required.Always);
schema.Id = new Uri(TypeName(context.ObjectType), UriKind.Relative);
return schema;
}
private static string TypeName(Type type, string acc = "")
{
var name = type.Name;
var index = name.IndexOf('`');
return index == -1 ? acc+name : TypeName(type.GetGenericArguments()[0],acc+name.Substring(0, index));
}
}
差不多了
{
"definitions": {
"MyGenericClassNullableDouble": {
"$id": "MyGenericClassNullableDouble",
"type": [
"object",
"null"
],
"properties": {
"prop": {
"type": "number"
}
},
"required": [
"prop"
]
},
"MyGenericClassDouble": {
"$id": "MyGenericClassDouble",
"type": [
"object",
"null"
],
"properties": {
"prop": {
"type": "number"
}
},
"required": [
"prop"
]
},
"MyGenericClassBoolean": {
"$id": "MyGenericClassBoolean",
"type": [
"object",
"null"
],
"properties": {
"prop": {
"type": "boolean"
}
},
"required": [
"prop"
]
}
},
"type": "object",
"properties": {
"genBool": {
"$ref": "MyGenericClassBoolean"
},
"genDouble": {
"$ref": "MyGenericClassDouble"
},
"genNulableDouble": {
"$ref": "MyGenericClassNullableDouble"
}
}
}
但引用并不是对定义的正确引用。
答案
我相信你走在正确的轨道上,我认为你只需要提供你自己的JSchemaGenerationProvider
实现,而不是使用StringEnumGenerationProvider
。像creating a custom provider:
public class FormatSchemaProvider : JSchemaGenerationProvider
{
public override JSchema GetSchema(JSchemaTypeGenerationContext context)
{
// customize the generated schema for these types to have a format
if (context.ObjectType == typeof(MyGenericClass<bool>))
{
return CreateSchemaWithFormat(
context.ObjectType, context.Required, "MyGenericClass");
}
// use default schema generation for all other types
return null;
}
private JSchema CreateSchemaWithFormat(Type type, Required required, string format)
{
JSchemaGenerator generator = new JSchemaGenerator();
JSchema schema = generator.Generate(type, required != Required.Always);
schema.Format = format;
return schema;
}
}
然后添加新一代提供者:
JSchemaGenerator generator = new JSchemaGenerator();
generator.GenerationProviders.Add(new FormatSchemaProvider());
JSchema schema = generator.Generate(typeof(MyClass));
以上是关于使用Newtonsoft.Json.Schema是否有办法拦截“定义”名称的生成?的主要内容,如果未能解决你的问题,请参考以下文章
这段代码的作用是啥,使用的每个符号是啥意思?使用的语言是 C,我只是编码的初学者
在不使用 mysql 服务器资源的情况下加密数据的最佳方法是啥?使用 CFB 模式的 mcrypt 是使用 php 的最佳方式吗?