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

4
.gitignore vendored
View File

@ -13,6 +13,7 @@ generated-sources/*
generated-code/*
*.swp
*.swo
*.csproj.user
/target
/generated-files
@ -36,6 +37,9 @@ samples/client/petstore/objc/SwaggerClientTests/Pods
samples/client/petstore/objc/SwaggerClientTests/SwaggerClient.xcworkspace
samples/client/petstore/objc/SwaggerClientTests/Podfile.lock
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
.idea
.lib

View File

@ -134,7 +134,7 @@ namespace {{packageName}}.Client
}
/// <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 ",".
/// Otherwise just return the string.
/// </summary>
@ -143,10 +143,11 @@ namespace {{packageName}}.Client
public string ParameterToString(object obj)
{
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
// 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>)
return String.Join(",", (obj as List<string>).ToArray());
else

View File

@ -79,6 +79,39 @@ namespace {{packageName}}.Client
}
}
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>
/// Returns a string with essential information for debugging.
/// </summary>

View File

@ -147,7 +147,7 @@ namespace {{packageName}}.Client
}
/// <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 ",".
/// Otherwise just return the string.
/// </summary>
@ -156,10 +156,11 @@ namespace {{packageName}}.Client
public string ParameterToString(object obj)
{
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
// For example: 2009-06-15T13:45:30.0000000
return ((DateTime)obj).ToString ("o");
return ((DateTime)obj).ToString (Configuration.DateTimeFormat);
else if (obj is IList)
{
string flattenString = "";

View File

@ -139,6 +139,39 @@ namespace {{packageName}}.Client
}
}
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>
/// Returns a string with essential information for debugging.
/// </summary>

View File

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

View File

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

View File

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

View File

@ -147,7 +147,7 @@ namespace IO.Swagger.Client
}
/// <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 ",".
/// Otherwise just return the string.
/// </summary>
@ -156,10 +156,11 @@ namespace IO.Swagger.Client
public string ParameterToString(object obj)
{
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
// For example: 2009-06-15T13:45:30.0000000
return ((DateTime)obj).ToString ("o");
return ((DateTime)obj).ToString (Configuration.DateTimeFormat);
else if (obj is IList)
{
string flattenString = "";

View File

@ -139,6 +139,39 @@ namespace IO.Swagger.Client
}
}
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>
/// Returns a string with essential information for debugging.
/// </summary>

View File

@ -7,6 +7,8 @@ using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;
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 Newtonsoft.Json;
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 Newtonsoft.Json;
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 Newtonsoft.Json;
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 Newtonsoft.Json;
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">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -69,5 +69,9 @@
<ItemGroup>
<Folder Include="Lib\" />
<Folder Include="Lib\SwaggerClient\" />
<EmbeddedResource Include="swagger-logo.png" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
</Project>

View File

@ -3,10 +3,17 @@ using System;
using System.Collections.Generic;
using IO.Swagger.Client;
namespace SwaggerClient.TestApiClient
namespace SwaggerClientTest.TestApiClient
{
public class TestApiClient
{
[TearDown()]
public void TearDown()
{
// Reset to default, just in case
Configuration.DateTimeFormat = "o";
}
[Test ()]
public void TestParameterToString ()
{
@ -19,6 +26,12 @@ namespace SwaggerClient.TestApiClient
// test array of int
List<int> numList = new List<int>(new int[] {1, 37});
Assert.AreEqual("1,37", api.ParameterToString (numList));
}
[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);
@ -31,7 +44,30 @@ namespace SwaggerClient.TestApiClient
// 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.Model;
namespace SwaggerClient.TestConfiguration
namespace SwaggerClientTest.TestConfiguration
{
public class TestConfiguration
{
[TearDown ()]
public void TearDown ()
{
// Reset to default, just in case
Configuration.DateTimeFormat = "o";
}
[Test ()]
public void TestAuthentication ()
{
@ -32,6 +39,22 @@ namespace SwaggerClient.TestConfiguration
Assert.AreNotSame (p.Configuration, Configuration.Default);
}
[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 ()
{

View File

@ -6,9 +6,9 @@ using System.Collections.Generic;
using IO.Swagger.Api;
using IO.Swagger.Model;
using IO.Swagger.Client;
using System.Reflection;
namespace SwaggerClient.TestPet
namespace SwaggerClientTest.TestPet
{
[TestFixture ()]
public class TestPet
@ -166,15 +166,15 @@ namespace SwaggerClient.TestPet
[Test ()]
public void TestUploadFile ()
{
Assembly _assembly = Assembly.GetExecutingAssembly();
Stream _imageStream = _assembly.GetManifestResourceStream("SwaggerClientTest.swagger-logo.png");
PetApi petApi = new PetApi ();
//NOTE: please provide a valid file (full path)
FileStream fileStream = new FileStream("/var/tmp/small.gif", FileMode.Open);
// 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
// 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