鉴于我有以下类型:
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"
]
}
除了我需要与之共享架构的服务不喜欢尖括号&#39;&lt;&gt;&#39;之外,哪个很棒。
有没有办法拦截定义名称的创建,所以我可以删除&lt;&gt;?请注意,其他 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"
}
}
}
但引用并不是对定义的正确引用。
答案 0 :(得分:0)
我相信您走在正确的轨道上,我认为您只需要提供自己的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));