Added the ability to customize the DateTimeFormat

Updated Configuration to have a DateTimeFormat
Added Unit Tests for Configuration.DateTimeFormat
Cleaned up namespaces in SwaggerClientTest
Added an embedded resource for testing uploads
This commit is contained in:
Jeff Kwan 2015-12-11 18:28:37 -05:00
parent ca26608b28
commit e0582ae912
20 changed files with 231 additions and 39 deletions

6
.gitignore vendored
View File

@ -13,6 +13,7 @@ generated-sources/*
generated-code/* generated-code/*
*.swp *.swp
*.swo *.swo
*.csproj.user
/target /target
/generated-files /generated-files
@ -36,6 +37,9 @@ samples/client/petstore/objc/SwaggerClientTests/Pods
samples/client/petstore/objc/SwaggerClientTests/SwaggerClient.xcworkspace samples/client/petstore/objc/SwaggerClientTests/SwaggerClient.xcworkspace
samples/client/petstore/objc/SwaggerClientTests/Podfile.lock samples/client/petstore/objc/SwaggerClientTests/Podfile.lock
samples/server/petstore/nodejs/node_modules samples/server/petstore/nodejs/node_modules
samples/client/petstore/csharp/SwaggerClientTest/.vs
samples/client/petstore/csharp/SwaggerClientTest/obj
samples/client/petstore/csharp/SwaggerClientTest/bin
target target
.idea .idea
.lib .lib
@ -60,4 +64,4 @@ samples/client/petstore/python/.venv/
*.java~ *.java~
*.pm~ *.pm~
*.xml~ *.xml~
*.t~ *.t~

View File

@ -134,7 +134,7 @@ namespace {{packageName}}.Client
} }
/// <summary> /// <summary>
/// If parameter is DateTime, output in ISO8601 format. /// If parameter is DateTime, output in a formatted string (default ISO 8601), customizable with Configuration.DateTime.
/// If parameter is a list of string, join the list with ",". /// If parameter is a list of string, join the list with ",".
/// Otherwise just return the string. /// Otherwise just return the string.
/// </summary> /// </summary>
@ -143,10 +143,11 @@ namespace {{packageName}}.Client
public string ParameterToString(object obj) public string ParameterToString(object obj)
{ {
if (obj is DateTime) if (obj is DateTime)
// Return an ISO 8601 formatted string, also known as a Round-trip date/time pattern // Return a formatted date string - Can be customized with Configuration.DateTimeFormat
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15T13:45:30.0000000 // For example: 2009-06-15T13:45:30.0000000
return ((DateTime)obj).ToString ("o"); return ((DateTime)obj).ToString (Configuration.DateTimeFormat);
else if (obj is List<string>) else if (obj is List<string>)
return String.Join(",", (obj as List<string>).ToArray()); return String.Join(",", (obj as List<string>).ToArray());
else else

View File

@ -78,7 +78,40 @@ namespace {{packageName}}.Client
_tempFolderPath = value + Path.DirectorySeparatorChar; _tempFolderPath = value + Path.DirectorySeparatorChar;
} }
} }
private const string ISO8601_DATETIME_FORMAT = "o";
private static string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
/// <summary>
/// Gets or sets the the date time format used when serializing in the ApiClient
/// By default, it's set to ISO 8601 - "o", for others see:
/// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
/// and https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
/// No validation is done to ensure that the string you're providing is valid
/// </summary>
/// <value>The DateTimeFormat string</value>
public static String DateTimeFormat
{
get
{
return _dateTimeFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
// Never allow a blank or null string, go back to the default
_dateTimeFormat = ISO8601_DATETIME_FORMAT;
return;
}
// Caution, no validation when you choose date time format other than ISO 8601
// Take a look at the above links
_dateTimeFormat = value;
}
}
/// <summary> /// <summary>
/// Returns a string with essential information for debugging. /// Returns a string with essential information for debugging.
/// </summary> /// </summary>

View File

@ -147,7 +147,7 @@ namespace {{packageName}}.Client
} }
/// <summary> /// <summary>
/// If parameter is DateTime, output in ISO8601 format. /// If parameter is DateTime, output in a formatted string (default ISO 8601), customizable with Configuration.DateTime.
/// If parameter is a list, join the list with ",". /// If parameter is a list, join the list with ",".
/// Otherwise just return the string. /// Otherwise just return the string.
/// </summary> /// </summary>
@ -156,10 +156,11 @@ namespace {{packageName}}.Client
public string ParameterToString(object obj) public string ParameterToString(object obj)
{ {
if (obj is DateTime) if (obj is DateTime)
// Return an ISO 8601 formatted string, also known as a Round-trip date/time pattern // Return a formatted date string - Can be customized with Configuration.DateTimeFormat
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15T13:45:30.0000000 // For example: 2009-06-15T13:45:30.0000000
return ((DateTime)obj).ToString ("o"); return ((DateTime)obj).ToString (Configuration.DateTimeFormat);
else if (obj is IList) else if (obj is IList)
{ {
string flattenString = ""; string flattenString = "";

View File

@ -138,7 +138,40 @@ namespace {{packageName}}.Client
_tempFolderPath = value + Path.DirectorySeparatorChar; _tempFolderPath = value + Path.DirectorySeparatorChar;
} }
} }
private const string ISO8601_DATETIME_FORMAT = "o";
private static string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
/// <summary>
/// Gets or sets the the date time format used when serializing in the ApiClient
/// By default, it's set to ISO 8601 - "o", for others see:
/// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
/// and https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
/// No validation is done to ensure that the string you're providing is valid
/// </summary>
/// <value>The DateTimeFormat string</value>
public static String DateTimeFormat
{
get
{
return _dateTimeFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
// Never allow a blank or null string, go back to the default
_dateTimeFormat = ISO8601_DATETIME_FORMAT;
return;
}
// Caution, no validation when you choose date time format other than ISO 8601
// Take a look at the above links
_dateTimeFormat = value;
}
}
/// <summary> /// <summary>
/// Returns a string with essential information for debugging. /// Returns a string with essential information for debugging.
/// </summary> /// </summary>

View File

@ -6,6 +6,7 @@ using RestSharp;
using IO.Swagger.Client; using IO.Swagger.Client;
using IO.Swagger.Model; using IO.Swagger.Model;
namespace IO.Swagger.Api namespace IO.Swagger.Api
{ {

View File

@ -6,6 +6,7 @@ using RestSharp;
using IO.Swagger.Client; using IO.Swagger.Client;
using IO.Swagger.Model; using IO.Swagger.Model;
namespace IO.Swagger.Api namespace IO.Swagger.Api
{ {

View File

@ -6,6 +6,7 @@ using RestSharp;
using IO.Swagger.Client; using IO.Swagger.Client;
using IO.Swagger.Model; using IO.Swagger.Model;
namespace IO.Swagger.Api namespace IO.Swagger.Api
{ {

View File

@ -147,7 +147,7 @@ namespace IO.Swagger.Client
} }
/// <summary> /// <summary>
/// If parameter is DateTime, output in ISO8601 format. /// If parameter is DateTime, output in a formatted string (default ISO 8601), customizable with Configuration.DateTime.
/// If parameter is a list, join the list with ",". /// If parameter is a list, join the list with ",".
/// Otherwise just return the string. /// Otherwise just return the string.
/// </summary> /// </summary>
@ -156,10 +156,11 @@ namespace IO.Swagger.Client
public string ParameterToString(object obj) public string ParameterToString(object obj)
{ {
if (obj is DateTime) if (obj is DateTime)
// Return an ISO 8601 formatted string, also known as a Round-trip date/time pattern // Return a formatted date string - Can be customized with Configuration.DateTimeFormat
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15T13:45:30.0000000 // For example: 2009-06-15T13:45:30.0000000
return ((DateTime)obj).ToString ("o"); return ((DateTime)obj).ToString (Configuration.DateTimeFormat);
else if (obj is IList) else if (obj is IList)
{ {
string flattenString = ""; string flattenString = "";

View File

@ -138,7 +138,40 @@ namespace IO.Swagger.Client
_tempFolderPath = value + Path.DirectorySeparatorChar; _tempFolderPath = value + Path.DirectorySeparatorChar;
} }
} }
private const string ISO8601_DATETIME_FORMAT = "o";
private static string _dateTimeFormat = ISO8601_DATETIME_FORMAT;
/// <summary>
/// Gets or sets the the date time format used when serializing in the ApiClient
/// By default, it's set to ISO 8601 - "o", for others see:
/// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
/// and https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
/// No validation is done to ensure that the string you're providing is valid
/// </summary>
/// <value>The DateTimeFormat string</value>
public static String DateTimeFormat
{
get
{
return _dateTimeFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
// Never allow a blank or null string, go back to the default
_dateTimeFormat = ISO8601_DATETIME_FORMAT;
return;
}
// Caution, no validation when you choose date time format other than ISO 8601
// Take a look at the above links
_dateTimeFormat = value;
}
}
/// <summary> /// <summary>
/// Returns a string with essential information for debugging. /// Returns a string with essential information for debugging.
/// </summary> /// </summary>

View File

@ -7,6 +7,8 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -122,4 +124,6 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -7,6 +7,8 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -187,4 +189,6 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -7,6 +7,8 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -187,4 +189,6 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -7,6 +7,8 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -122,4 +124,6 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -7,6 +7,8 @@ using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IO.Swagger.Model namespace IO.Swagger.Model
{ {
@ -219,4 +221,6 @@ namespace IO.Swagger.Model
} }
} }
} }

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -69,5 +69,9 @@
<ItemGroup> <ItemGroup>
<Folder Include="Lib\" /> <Folder Include="Lib\" />
<Folder Include="Lib\SwaggerClient\" /> <Folder Include="Lib\SwaggerClient\" />
<EmbeddedResource Include="swagger-logo.png" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -3,11 +3,18 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using IO.Swagger.Client; using IO.Swagger.Client;
namespace SwaggerClient.TestApiClient namespace SwaggerClientTest.TestApiClient
{ {
public class TestApiClient public class TestApiClient
{ {
[Test ()] [TearDown()]
public void TearDown()
{
// Reset to default, just in case
Configuration.DateTimeFormat = "o";
}
[Test ()]
public void TestParameterToString () public void TestParameterToString ()
{ {
ApiClient api = new ApiClient (); ApiClient api = new ApiClient ();
@ -19,20 +26,49 @@ namespace SwaggerClient.TestApiClient
// test array of int // test array of int
List<int> numList = new List<int>(new int[] {1, 37}); List<int> numList = new List<int>(new int[] {1, 37});
Assert.AreEqual("1,37", api.ParameterToString (numList)); Assert.AreEqual("1,37", api.ParameterToString (numList));
// test datetime
DateTime dateUtc = DateTime.Parse("2008-04-10T13:30:00.0000000z", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2008-04-10T13:30:00.0000000Z", api.ParameterToString (dateUtc));
// test datetime with no timezone
DateTime dateWithNoTz = DateTime.Parse("2008-04-10T13:30:00.000", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2008-04-10T13:30:00.0000000", api.ParameterToString (dateWithNoTz));
// test datetime with a time zone
DateTime dateWithTz = DateTime.Parse("2008-04-10T13:30:00.0000000-04:00", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2008-04-10T13:30:00.0000000-04:00", api.ParameterToString (dateWithTz));
} }
}
[Test ()]
public void TestParameterToString_DateTime ()
{
ApiClient api = new ApiClient();
// test datetime
DateTime dateUtc = DateTime.Parse("2008-04-10T13:30:00.0000000z", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2008-04-10T13:30:00.0000000Z", api.ParameterToString(dateUtc));
// test datetime with no timezone
DateTime dateWithNoTz = DateTime.Parse("2008-04-10T13:30:00.000", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2008-04-10T13:30:00.0000000", api.ParameterToString(dateWithNoTz));
// test datetime with a time zone
DateTime dateWithTz = DateTime.Parse("2008-04-10T13:30:00.0000000-04:00", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2008-04-10T13:30:00.0000000-04:00", api.ParameterToString(dateWithTz));
}
[Test ()]
public void TestParameterToString_DateTime_WithUFormat ()
{
// Setup the DateTimeFormat across all of the calls
Configuration.DateTimeFormat = "u";
ApiClient api = new ApiClient();
// test datetime
DateTime dateUtc = DateTime.Parse("2009-06-15 20:45:30Z", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("2009-06-15 20:45:30Z", api.ParameterToString(dateUtc));
}
[Test ()]
public void TestParameterToString_DateTime_WithCustomFormat ()
{
// Setup the DateTimeFormat across all of the calls
Configuration.DateTimeFormat = "dd/MM/yy HH:mm:ss";
ApiClient api = new ApiClient();
// test datetime
DateTime dateUtc = DateTime.Parse("2009-06-15 20:45:30Z", null, System.Globalization.DateTimeStyles.RoundtripKind);
Assert.AreEqual("15/06/09 20:45:30", api.ParameterToString(dateUtc));
}
}
} }

View File

@ -5,10 +5,17 @@ using IO.Swagger.Client;
using IO.Swagger.Api; using IO.Swagger.Api;
using IO.Swagger.Model; using IO.Swagger.Model;
namespace SwaggerClient.TestConfiguration namespace SwaggerClientTest.TestConfiguration
{ {
public class TestConfiguration public class TestConfiguration
{ {
[TearDown ()]
public void TearDown ()
{
// Reset to default, just in case
Configuration.DateTimeFormat = "o";
}
[Test ()] [Test ()]
public void TestAuthentication () public void TestAuthentication ()
{ {
@ -32,7 +39,23 @@ namespace SwaggerClient.TestConfiguration
Assert.AreNotSame (p.Configuration, Configuration.Default); Assert.AreNotSame (p.Configuration, Configuration.Default);
} }
[Test ()] [Test ()]
public void TestDateTimeFormat_Default ()
{
// Should default to the Round-trip Format Specifier - "o"
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
Assert.AreEqual("o", Configuration.DateTimeFormat);
}
[Test ()]
public void TestDateTimeFormat_UType()
{
Configuration.DateTimeFormat = "u";
Assert.AreEqual("u", Configuration.DateTimeFormat);
}
[Test ()]
public void TestDefautlConfiguration () public void TestDefautlConfiguration ()
{ {
PetApi p1 = new PetApi (); PetApi p1 = new PetApi ();

View File

@ -6,9 +6,9 @@ using System.Collections.Generic;
using IO.Swagger.Api; using IO.Swagger.Api;
using IO.Swagger.Model; using IO.Swagger.Model;
using IO.Swagger.Client; using IO.Swagger.Client;
using System.Reflection;
namespace SwaggerClientTest.TestPet
namespace SwaggerClient.TestPet
{ {
[TestFixture ()] [TestFixture ()]
public class TestPet public class TestPet
@ -166,15 +166,15 @@ namespace SwaggerClient.TestPet
[Test ()] [Test ()]
public void TestUploadFile () public void TestUploadFile ()
{ {
PetApi petApi = new PetApi (); Assembly _assembly = Assembly.GetExecutingAssembly();
//NOTE: please provide a valid file (full path) Stream _imageStream = _assembly.GetManifestResourceStream("SwaggerClientTest.swagger-logo.png");
FileStream fileStream = new FileStream("/var/tmp/small.gif", FileMode.Open); PetApi petApi = new PetApi ();
// test file upload with form parameters // test file upload with form parameters
petApi.UploadFile(petId, "new form name", fileStream); petApi.UploadFile(petId, "new form name", _imageStream);
// test file upload without any form parameters // test file upload without any form parameters
// using optional parameter syntax introduced at .net 4.0 // using optional parameter syntax introduced at .net 4.0
petApi.UploadFile(petId: petId, file: fileStream); petApi.UploadFile(petId: petId, file: _imageStream);
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB