Bumped package version to 1.8.2. Added the ability to use json names in the Javascript Client generator if a JsonProperty exists on a field or a property.
This commit is contained in:
		| @@ -11,17 +11,22 @@ using System.Collections; | |||||||
| using Newtonsoft.Json.Linq; | using Newtonsoft.Json.Linq; | ||||||
| using System.Web; | using System.Web; | ||||||
| using System.Net.Http; | using System.Net.Http; | ||||||
|  | using Newtonsoft.Json; | ||||||
|  |  | ||||||
| namespace MontoyaTech.Rest.Net.Example | namespace MontoyaTech.Rest.Net.Example | ||||||
| { | { | ||||||
|     public class BaseUser |     public class BaseUser | ||||||
|     { |     { | ||||||
|  |         [JsonProperty("id")] | ||||||
|         public string Id; |         public string Id; | ||||||
|  |  | ||||||
|  |         [JsonProperty("firstInitial")] | ||||||
|         public char FirstInitial; |         public char FirstInitial; | ||||||
|  |  | ||||||
|  |         [JsonProperty("role")] | ||||||
|         public UserRole Role { get; set; } |         public UserRole Role { get; set; } | ||||||
|  |  | ||||||
|  |         [JsonProperty("permissions")] | ||||||
|         public List<Permission> Permissions; |         public List<Permission> Permissions; | ||||||
|  |  | ||||||
|         public class Permission |         public class Permission | ||||||
| @@ -94,11 +99,11 @@ namespace MontoyaTech.Rest.Net.Example | |||||||
|  |  | ||||||
|             File.WriteAllText("Client.cs", listener.GenerateCSharpClient()); |             File.WriteAllText("Client.cs", listener.GenerateCSharpClient()); | ||||||
|  |  | ||||||
|             File.WriteAllText("Client.js", listener.GenerateJavascriptClient()); |             File.WriteAllText("Client.js", listener.GenerateJavascriptClient(useJsonNames: true)); | ||||||
|  |  | ||||||
|             File.WriteAllText("StaticClient.cs", listener.GenerateCSharpClient("StaticClient", staticCode: true)); |             File.WriteAllText("StaticClient.cs", listener.GenerateCSharpClient("StaticClient", staticCode: true)); | ||||||
|  |  | ||||||
|             File.WriteAllText("StaticClient.js", listener.GenerateJavascriptClient("StaticClient", staticCode: true)); |             File.WriteAllText("StaticClient.js", listener.GenerateJavascriptClient("StaticClient", staticCode: true, useJsonNames: true)); | ||||||
|  |  | ||||||
|             Console.WriteLine("Generated Client.cs, Client.js, StaticClient.cs, StaticClient.js"); |             Console.WriteLine("Generated Client.cs, Client.js, StaticClient.cs, StaticClient.js"); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -17,7 +17,7 @@ | |||||||
|     <AssemblyName>MontoyaTech.Rest.Net</AssemblyName> |     <AssemblyName>MontoyaTech.Rest.Net</AssemblyName> | ||||||
|     <RootNamespace>MontoyaTech.Rest.Net</RootNamespace> |     <RootNamespace>MontoyaTech.Rest.Net</RootNamespace> | ||||||
|     <GenerateDocumentationFile>True</GenerateDocumentationFile> |     <GenerateDocumentationFile>True</GenerateDocumentationFile> | ||||||
|     <Version>1.8.1</Version> |     <Version>1.8.2</Version> | ||||||
|     <PackageReleaseNotes></PackageReleaseNotes> |     <PackageReleaseNotes></PackageReleaseNotes> | ||||||
|     <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon> |     <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon> | ||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
|   | |||||||
| @@ -20,6 +20,11 @@ namespace MontoyaTech.Rest.Net | |||||||
|         /// </summary> |         /// </summary> | ||||||
|         public bool StaticCode = false; |         public bool StaticCode = false; | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Whether or not to use Json Property names instead of the Field/Property names. | ||||||
|  |         /// </summary> | ||||||
|  |         public bool UseJsonNames = false; | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Generates a Javascript Client from a given set of routes and returns it. |         /// Generates a Javascript Client from a given set of routes and returns it. | ||||||
|         /// </summary> |         /// </summary> | ||||||
| @@ -284,28 +289,64 @@ namespace MontoyaTech.Rest.Net | |||||||
|                 writer.WriteBreak().WriteLine("/**").Indent(); |                 writer.WriteBreak().WriteLine("/**").Indent(); | ||||||
|                 writer.WriteLine("@function"); |                 writer.WriteLine("@function"); | ||||||
|  |  | ||||||
|  |                 //Docuemnt the fields | ||||||
|                 foreach (var field in fields) |                 foreach (var field in fields) | ||||||
|  |                 { | ||||||
|  |                     if (this.UseJsonNames) | ||||||
|  |                         writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(field.FieldType)}}} {(field.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? field.Name)}"); | ||||||
|  |                     else | ||||||
|                         writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(field.FieldType)}}} {field.Name}"); |                         writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(field.FieldType)}}} {field.Name}"); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 //Document the properties | ||||||
|                 foreach (var property in properties) |                 foreach (var property in properties) | ||||||
|  |                 { | ||||||
|  |                     if (this.UseJsonNames) | ||||||
|  |                         writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(property.PropertyType)}}} {(property.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? property.Name)}"); | ||||||
|  |                     else | ||||||
|                         writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(property.PropertyType)}}} {property.Name}"); |                         writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(property.PropertyType)}}} {property.Name}"); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|                 writer.Outdent().WriteLine("*/"); |                 writer.Outdent().WriteLine("*/"); | ||||||
|                 writer.Write("constructor("); |                 writer.Write("constructor("); | ||||||
|  |  | ||||||
|  |                 //Write the default fields | ||||||
|                 foreach (var field in fields) |                 foreach (var field in fields) | ||||||
|  |                 { | ||||||
|  |                     if (this.UseJsonNames) | ||||||
|  |                         writer.WriteSeparator().Write(field.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? field.Name).Write(" = ").Write(this.GetTypeDefaultValue(field.FieldType)); | ||||||
|  |                     else | ||||||
|                         writer.WriteSeparator().Write(field.Name).Write(" = ").Write(this.GetTypeDefaultValue(field.FieldType)); |                         writer.WriteSeparator().Write(field.Name).Write(" = ").Write(this.GetTypeDefaultValue(field.FieldType)); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 //Write the default properties | ||||||
|                 foreach (var property in properties) |                 foreach (var property in properties) | ||||||
|  |                 { | ||||||
|  |                     if (this.UseJsonNames) | ||||||
|  |                         writer.WriteSeparator().Write(property.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? property.Name).Write(" = ").Write(this.GetTypeDefaultValue(property.PropertyType)); | ||||||
|  |                     else | ||||||
|                         writer.WriteSeparator().Write(property.Name).Write(" = ").Write(this.GetTypeDefaultValue(property.PropertyType)); |                         writer.WriteSeparator().Write(property.Name).Write(" = ").Write(this.GetTypeDefaultValue(property.PropertyType)); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|                 writer.WriteLine(") {").Indent(); |                 writer.WriteLine(") {").Indent(); | ||||||
|  |  | ||||||
|  |                 //Init the default fields | ||||||
|                 foreach (var field in fields) |                 foreach (var field in fields) | ||||||
|  |                 { | ||||||
|  |                     if (this.UseJsonNames) | ||||||
|  |                         writer.Write("this.").Write(field.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? field.Name).Write(" = ").Write(field.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? field.Name).WriteLine(";"); | ||||||
|  |                     else | ||||||
|                         writer.Write("this.").Write(field.Name).Write(" = ").Write(field.Name).WriteLine(";"); |                         writer.Write("this.").Write(field.Name).Write(" = ").Write(field.Name).WriteLine(";"); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 //Init the default properties | ||||||
|                 foreach (var property in properties) |                 foreach (var property in properties) | ||||||
|  |                 { | ||||||
|  |                     if (this.UseJsonNames) | ||||||
|  |                         writer.Write("this.").Write(property.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? property.Name).Write(" = ").Write(property.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? property.Name).WriteLine(";"); | ||||||
|  |                     else | ||||||
|                         writer.Write("this.").Write(property.Name).Write(" = ").Write(property.Name).WriteLine(";"); |                         writer.Write("this.").Write(property.Name).Write(" = ").Write(property.Name).WriteLine(";"); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|                 writer.Outdent().WriteLine("}"); |                 writer.Outdent().WriteLine("}"); | ||||||
|             } |             } | ||||||
| @@ -417,11 +458,19 @@ namespace MontoyaTech.Rest.Net | |||||||
|             if (field.DeclaringType != null && field.DeclaringType.IsEnum) |             if (field.DeclaringType != null && field.DeclaringType.IsEnum) | ||||||
|             { |             { | ||||||
|                 writer.WriteLine($"/** @type {{{GetTypeFullyResolvedName(field.DeclaringType)}}} */"); |                 writer.WriteLine($"/** @type {{{GetTypeFullyResolvedName(field.DeclaringType)}}} */"); | ||||||
|  |  | ||||||
|  |                 if (this.UseJsonNames) | ||||||
|  |                     writer.WriteLine($"static {(field.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? field.Name)} = {field.GetRawConstantValue()};"); | ||||||
|  |                 else | ||||||
|                     writer.WriteLine($"static {field.Name} = {field.GetRawConstantValue()};"); |                     writer.WriteLine($"static {field.Name} = {field.GetRawConstantValue()};"); | ||||||
|             } |             } | ||||||
|             else |             else | ||||||
|             { |             { | ||||||
|                 writer.WriteLine($"/** @type {{{GetTypeFullyResolvedName(field.FieldType)}}} */"); |                 writer.WriteLine($"/** @type {{{GetTypeFullyResolvedName(field.FieldType)}}} */"); | ||||||
|  |  | ||||||
|  |                 if (this.UseJsonNames) | ||||||
|  |                     writer.WriteLine($"{(field.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? field.Name)} = {GetTypeDefaultValue(field.FieldType)};"); | ||||||
|  |                 else | ||||||
|                     writer.WriteLine($"{field.Name} = {GetTypeDefaultValue(field.FieldType)};"); |                     writer.WriteLine($"{field.Name} = {GetTypeDefaultValue(field.FieldType)};"); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| @@ -431,6 +480,10 @@ namespace MontoyaTech.Rest.Net | |||||||
|             writer.WriteBreak(); |             writer.WriteBreak(); | ||||||
|  |  | ||||||
|             writer.WriteLine($"/** @type {{{GetTypeFullyResolvedName(property.PropertyType)}}} */"); |             writer.WriteLine($"/** @type {{{GetTypeFullyResolvedName(property.PropertyType)}}} */"); | ||||||
|  |  | ||||||
|  |             if (this.UseJsonNames) | ||||||
|  |                 writer.WriteLine($"{(property.GetCustomAttribute<Newtonsoft.Json.JsonPropertyAttribute>()?.PropertyName ?? property.Name)} = {GetTypeDefaultValue(property.PropertyType)};"); | ||||||
|  |             else | ||||||
|                 writer.WriteLine($"{property.Name} = {GetTypeDefaultValue(property.PropertyType)};"); |                 writer.WriteLine($"{property.Name} = {GetTypeDefaultValue(property.PropertyType)};"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -256,12 +256,13 @@ namespace MontoyaTech.Rest.Net | |||||||
|         /// <param name="clientName"></param> |         /// <param name="clientName"></param> | ||||||
|         /// <param name="staticCode"></param> |         /// <param name="staticCode"></param> | ||||||
|         /// <returns></returns> |         /// <returns></returns> | ||||||
|         public string GenerateJavascriptClient(string clientName = "Client", bool staticCode = false) |         public string GenerateJavascriptClient(string clientName = "Client", bool staticCode = false, bool useJsonNames = false) | ||||||
|         { |         { | ||||||
|             var generator = new RestJavascriptClientGenerator(); |             var generator = new RestJavascriptClientGenerator(); | ||||||
|  |  | ||||||
|             generator.ClientName = clientName; |             generator.ClientName = clientName; | ||||||
|             generator.StaticCode = staticCode; |             generator.StaticCode = staticCode; | ||||||
|  |             generator.UseJsonNames = useJsonNames; | ||||||
|  |  | ||||||
|             return generator.Generate(this); |             return generator.Generate(this); | ||||||
|         } |         } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user