diff --git a/Rest.Net.Example/Program.cs b/Rest.Net.Example/Program.cs index 7ec8500..421dfd7 100644 --- a/Rest.Net.Example/Program.cs +++ b/Rest.Net.Example/Program.cs @@ -12,7 +12,14 @@ namespace MontoyaTech.Rest.Net.Example { public class Program { - public class User + public class BaseUser + { + public string Id; + + public UserRole Role { get; set; } + } + + public class User : BaseUser { public string Name = null; @@ -28,6 +35,13 @@ namespace MontoyaTech.Rest.Net.Example } } + public enum UserRole : byte + { + Unknown = 0, + Admin = 2, + User = 1 + } + public static RouteFileCache FileCache = new RouteFileCache(100 * 1024 * 1024); public static void Main(string[] args) @@ -41,7 +55,8 @@ namespace MontoyaTech.Rest.Net.Example new Route(HttpRequestMethod.Get, "/file/compress", CompressFile), new Route(HttpRequestMethod.Get, "/auth/{username}", Exists), new Route(HttpRequestMethod.Post, "/auth/signup", Signup), - new Route(HttpRequestMethod.Get, "/auth/", Json) + new Route(HttpRequestMethod.Get, "/auth/", Json), + new Route(HttpRequestMethod.Get, "/auth/role", GetRole) ); string code = listener.GenerateCSharpClient(); @@ -129,6 +144,13 @@ namespace MontoyaTech.Rest.Net.Example return context.Response.WithStatus(HttpStatusCode.OK); } + [RouteGroup("Auth")] + [RouteResponse(typeof(UserRole))] + public static HttpListenerResponse GetRole(HttpListenerContext context) + { + return context.Response.WithStatus(HttpStatusCode.OK).WithJson(UserRole.Admin); + } + [RouteGroup("Auth")] [RouteName("Get")] [RouteResponse(typeof(User))] diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index 52c90b4..17b24a1 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.3.8 + 1.3.9 Logo_Symbol_Black_Outline.png @@ -33,4 +33,8 @@ + + + + \ No newline at end of file diff --git a/Rest.Net/RestCSharpClientGenerator.cs b/Rest.Net/RestCSharpClientGenerator.cs index da70051..f78b174 100644 --- a/Rest.Net/RestCSharpClientGenerator.cs +++ b/Rest.Net/RestCSharpClientGenerator.cs @@ -157,7 +157,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - protected virtual void GenerateCSharpIncludedTypes(List types, CodeWriter writer) + protected internal virtual void GenerateCSharpIncludedTypes(List types, CodeWriter writer) { foreach (var type in types) this.GenerateCSharpIncludedType(type, writer); @@ -168,24 +168,36 @@ namespace MontoyaTech.Rest.Net /// /// /// - protected virtual void GenerateCSharpIncludedType(Type type, CodeWriter writer) + protected internal virtual void GenerateCSharpIncludedType(Type type, CodeWriter writer) { writer.WriteBreak(); - writer.WriteLine($"public class {type.Name}").WriteLine("{").Indent(); + writer.Write($"public {(type.IsEnum ? "enum" : "class")} {type.Name}"); - var fields = type.GetFields(); + if (type.IsEnum) + writer.Write(" : ").Write(this.GetTypeFullyResolvedName(Enum.GetUnderlyingType(type))); + else if (!(IsTypeDotNet(type.BaseType) && type.BaseType.Name == "Object")) + writer.Write(" : ").Write(this.GetTypeFullyResolvedName(type.BaseType)); + + writer.NewLine().WriteLine("{").Indent(); + + FieldInfo[] fields = null; + + if (type.IsEnum) + fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static); + else + fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public); if (fields != null) foreach (var field in fields) - if (field.IsPublic) + if (field.IsPublic && !field.IsSpecialName) this.GenerateCSharpIncludedField(field, writer); - var properties = type.GetProperties(); + var properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public); if (properties != null) foreach (var property in properties) - if (property.GetSetMethod() != null && property.GetGetMethod() != null) + if (!property.IsSpecialName && property.GetSetMethod() != null && property.GetGetMethod() != null) this.GenerateCSharpIncludedProperty(property, writer); writer.Outdent().WriteLine("}"); @@ -196,11 +208,14 @@ namespace MontoyaTech.Rest.Net /// /// /// - protected virtual void GenerateCSharpIncludedField(FieldInfo field, CodeWriter writer) + protected internal virtual void GenerateCSharpIncludedField(FieldInfo field, CodeWriter writer) { writer.WriteBreak(); - writer.WriteLine($"public {this.GetTypeFullyResolvedName(field.FieldType)} {field.Name};"); + if (field.DeclaringType.IsEnum) + writer.WriteLine($"{field.Name} = {field.GetRawConstantValue()},"); + else + writer.WriteLine($"public {this.GetTypeFullyResolvedName(field.FieldType)} {field.Name};"); } /// @@ -208,7 +223,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - protected virtual void GenerateCSharpIncludedProperty(PropertyInfo property, CodeWriter writer) + protected internal virtual void GenerateCSharpIncludedProperty(PropertyInfo property, CodeWriter writer) { writer.WriteBreak(); @@ -220,7 +235,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - protected virtual void GenerateCSharpRouteGroups(Dictionary> groups, CodeWriter writer) + protected internal virtual void GenerateCSharpRouteGroups(Dictionary> groups, CodeWriter writer) { foreach (var group in groups) this.GenerateCSharpRouteGroup(group.Key, group.Value, writer); @@ -232,7 +247,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - protected virtual void GenerateCSharpRouteGroup(string name, List routes, CodeWriter writer) + protected internal virtual void GenerateCSharpRouteGroup(string name, List routes, CodeWriter writer) { writer.WriteBreak(); @@ -270,7 +285,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - protected virtual void GenerateCSharpRouteFunction(Route route, CodeWriter writer) + protected internal virtual void GenerateCSharpRouteFunction(Route route, CodeWriter writer) { writer.WriteBreak(); diff --git a/Rest.Net/RestClientGenerator.cs b/Rest.Net/RestClientGenerator.cs index 509b46f..3399bf3 100644 --- a/Rest.Net/RestClientGenerator.cs +++ b/Rest.Net/RestClientGenerator.cs @@ -49,6 +49,17 @@ namespace MontoyaTech.Rest.Net dependencies.Add(type); + if (type.IsEnum) + return dependencies.ToList(); + + { + var types = this.FindTypeDependencies(type.BaseType); + + for (int i = 0; i < types.Count; i++) + if (!dependencies.Contains(types[i])) + dependencies.Add(types[i]); + } + var arguments = type.GetGenericArguments(); if (arguments != null) @@ -69,7 +80,7 @@ namespace MontoyaTech.Rest.Net { foreach (var field in fields) { - if (field.IsPublic) + if (field.IsPublic && !field.IsSpecialName) { var types = this.FindTypeDependencies(field.FieldType); @@ -86,7 +97,7 @@ namespace MontoyaTech.Rest.Net { foreach (var property in properties) { - if (property.GetSetMethod() != null && property.GetGetMethod() != null) + if (!property.IsSpecialName && property.GetSetMethod() != null && property.GetGetMethod() != null) { var types = this.FindTypeDependencies(property.PropertyType);