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 typeAttribute = ContextualTypeExtensions.ToCachedType(type).GetTypeAttribute<JsonSchemaAttribute>();
if (!string.IsNullOrEmpty(typeAttribute?.Name))
return typeAttribute.Name;
CachedType val = ContextualTypeExtensions.ToCachedType(type);
if (val.get_Type().IsConstructedGenericType)
return GetName(val).Split(new char[1] {
'`'
}).First() + "Of" + string.Join("And", from a in val.get_GenericArguments()
select Generate(a.get_OriginalType()));
return GetName(val);
}
private static string GetName(CachedType cType)
{
if (!(cType.get_TypeName() == "Int16")) {
if (!(cType.get_TypeName() == "Int32")) {
if (!(cType.get_TypeName() == "Int64"))
return GetNullableDisplayName(cType, cType.get_TypeName());
return GetNullableDisplayName(cType, "Long");
}
return GetNullableDisplayName(cType, "Integer");
}
return GetNullableDisplayName(cType, "Short");
}
private static string GetNullableDisplayName(CachedType type, string actual)
{
return (type.get_IsNullableType() ? "Nullable" : "") + actual;
}
}
}