DefaultSchemaNameGenerator
The default schema name generator implementation.
using Namotion.Reflection;
using NJsonSchema.Annotations;
using System;
using System.Linq;
namespace NJsonSchema.Generation
{
public class DefaultSchemaNameGenerator : ISchemaNameGenerator
{
public virtual string Generate(Type type)
{
JsonSchemaAttribute inheritedAttribute = type.ToCachedType().GetInheritedAttribute<JsonSchemaAttribute>();
if (!string.IsNullOrEmpty(inheritedAttribute?.Name))
return inheritedAttribute.Name;
CachedType cachedType = type.ToCachedType();
if (cachedType.Type.IsConstructedGenericType)
return GetName(cachedType).Split(new char[1] {
'`'
}).First() + "Of" + string.Join("And", from a in cachedType.GenericArguments
select Generate(a.OriginalType));
return GetName(cachedType);
}
private static string GetName(CachedType cType)
{
if (!(cType.TypeName == "Int16")) {
if (!(cType.TypeName == "Int32")) {
if (!(cType.TypeName == "Int64"))
return GetNullableDisplayName(cType, cType.TypeName);
return GetNullableDisplayName(cType, "Long");
}
return GetNullableDisplayName(cType, "Integer");
}
return GetNullableDisplayName(cType, "Short");
}
private static string GetNullableDisplayName(CachedType type, string actual)
{
return (type.IsNullableType ? "Nullable" : "") + actual;
}
}
}