Skip to content

Commit 6669fac

Browse files
author
Hüseyin Uslu
committed
Fixed a bug in Handler.cs - handling of multiple optional parameters. Fixes Astn#28.
Added tests for multiple optional parameters. Added nuget restore package support. Updated newtonsoft json.net nuget package to 6.0.5. Removed unsued ThreadCulture call as it doesn't have any real benefits - we are already culture safe except the nullable-datetime.
1 parent fa755e0 commit 6669fac

11 files changed

Lines changed: 226 additions & 13 deletions

File tree

.nuget/NuGet.Config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<solution>
4+
<add key="disableSourceControlIntegration" value="true" />
5+
</solution>
6+
</configuration>

.nuget/NuGet.targets

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
5+
6+
<!-- Enable the restore command to run before builds -->
7+
<RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>
8+
9+
<!-- Property that enables building a package from a project -->
10+
<BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>
11+
12+
<!-- Determines if package restore consent is required to restore packages -->
13+
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>
14+
15+
<!-- Download NuGet.exe if it does not already exist -->
16+
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
17+
</PropertyGroup>
18+
19+
<ItemGroup Condition=" '$(PackageSources)' == '' ">
20+
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
21+
<!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
22+
<!--
23+
<PackageSource Include="https://www.nuget.org/api/v2/" />
24+
<PackageSource Include="https://my-nuget-source/nuget/" />
25+
-->
26+
</ItemGroup>
27+
28+
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
29+
<!-- Windows specific commands -->
30+
<NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
31+
</PropertyGroup>
32+
33+
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
34+
<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
35+
<NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
36+
</PropertyGroup>
37+
38+
<PropertyGroup>
39+
<PackagesProjectConfig Condition=" '$(OS)' == 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
40+
<PackagesProjectConfig Condition=" '$(OS)' != 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config</PackagesProjectConfig>
41+
</PropertyGroup>
42+
43+
<PropertyGroup>
44+
<PackagesConfig Condition="Exists('$(MSBuildProjectDirectory)\packages.config')">$(MSBuildProjectDirectory)\packages.config</PackagesConfig>
45+
<PackagesConfig Condition="Exists('$(PackagesProjectConfig)')">$(PackagesProjectConfig)</PackagesConfig>
46+
</PropertyGroup>
47+
48+
<PropertyGroup>
49+
<!-- NuGet command -->
50+
<NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
51+
<PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
52+
53+
<NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
54+
<NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 "$(NuGetExePath)"</NuGetCommand>
55+
56+
<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
57+
58+
<RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
59+
<NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>
60+
61+
<PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
62+
<PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>
63+
64+
<!-- Commands -->
65+
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
66+
<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
67+
68+
<!-- We need to ensure packages are restored prior to assembly resolve -->
69+
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
70+
RestorePackages;
71+
$(BuildDependsOn);
72+
</BuildDependsOn>
73+
74+
<!-- Make the build depend on restore packages -->
75+
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
76+
$(BuildDependsOn);
77+
BuildPackage;
78+
</BuildDependsOn>
79+
</PropertyGroup>
80+
81+
<Target Name="CheckPrerequisites">
82+
<!-- Raise an error if we're unable to locate nuget.exe -->
83+
<Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
84+
<!--
85+
Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
86+
This effectively acts as a lock that makes sure that the download operation will only happen once and all
87+
parallel builds will have to wait for it to complete.
88+
-->
89+
<MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
90+
</Target>
91+
92+
<Target Name="_DownloadNuGet">
93+
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
94+
</Target>
95+
96+
<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
97+
<Exec Command="$(RestoreCommand)"
98+
Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
99+
100+
<Exec Command="$(RestoreCommand)"
101+
LogStandardErrorAsError="true"
102+
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
103+
</Target>
104+
105+
<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
106+
<Exec Command="$(BuildCommand)"
107+
Condition=" '$(OS)' != 'Windows_NT' " />
108+
109+
<Exec Command="$(BuildCommand)"
110+
LogStandardErrorAsError="true"
111+
Condition=" '$(OS)' == 'Windows_NT' " />
112+
</Target>
113+
114+
<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
115+
<ParameterGroup>
116+
<OutputFilename ParameterType="System.String" Required="true" />
117+
</ParameterGroup>
118+
<Task>
119+
<Reference Include="System.Core" />
120+
<Using Namespace="System" />
121+
<Using Namespace="System.IO" />
122+
<Using Namespace="System.Net" />
123+
<Using Namespace="Microsoft.Build.Framework" />
124+
<Using Namespace="Microsoft.Build.Utilities" />
125+
<Code Type="Fragment" Language="cs">
126+
<![CDATA[
127+
try {
128+
OutputFilename = Path.GetFullPath(OutputFilename);
129+
130+
Log.LogMessage("Downloading latest version of NuGet.exe...");
131+
WebClient webClient = new WebClient();
132+
webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
133+
134+
return true;
135+
}
136+
catch (Exception ex) {
137+
Log.LogErrorFromException(ex);
138+
return false;
139+
}
140+
]]>
141+
</Code>
142+
</Task>
143+
</UsingTask>
144+
</Project>

AustinHarris.JsonRpc.AspNet/AustinHarris.JsonRpc.AspNet.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<SccLocalPath>SAK</SccLocalPath>
1616
<SccAuxPath>SAK</SccAuxPath>
1717
<SccProvider>SAK</SccProvider>
18+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
19+
<RestorePackages>true</RestorePackages>
1820
</PropertyGroup>
1921
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2022
<DebugSymbols>true</DebugSymbols>
@@ -36,7 +38,7 @@
3638
<ItemGroup>
3739
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
3840
<SpecificVersion>False</SpecificVersion>
39-
<HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
41+
<HintPath>..\packages\Newtonsoft.Json.6.0.5\lib\net40\Newtonsoft.Json.dll</HintPath>
4042
</Reference>
4143
<Reference Include="System" />
4244
<Reference Include="System.Core" />
@@ -57,6 +59,13 @@
5759
<None Include="packages.config" />
5860
</ItemGroup>
5961
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
62+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
63+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
64+
<PropertyGroup>
65+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
66+
</PropertyGroup>
67+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
68+
</Target>
6069
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6170
Other similar extension points exist, see Microsoft.Common.targets.
6271
<Target Name="BeforeBuild">
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net40" />
3+
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net40" />
44
</packages>

AustinHarris.JsonRpc.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.30501.0
4+
VisualStudioVersion = 12.0.30723.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BBFBBA8A-2F75-422C-ACCD-D05A6EF7244C}"
77
ProjectSection(SolutionItems) = preProject
@@ -16,6 +16,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AustinHarris.JsonRpc.AspNet
1616
EndProject
1717
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AustinHarris.JsonRpcTest", "AustinHarris.JsonRpcTest\AustinHarris.JsonRpcTest.csproj", "{0A11B164-EA4A-41E7-BBE8-2CD19DB4C1AD}"
1818
EndProject
19+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{4811B06E-0A04-4CD4-9018-FE8295E8BF08}"
20+
ProjectSection(SolutionItems) = preProject
21+
.nuget\NuGet.Config = .nuget\NuGet.Config
22+
.nuget\NuGet.exe = .nuget\NuGet.exe
23+
.nuget\NuGet.targets = .nuget\NuGet.targets
24+
EndProjectSection
25+
EndProject
1926
Global
2027
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2128
Debug|Any CPU = Debug|Any CPU

AustinHarris.JsonRpcTest/UnitTest1.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,40 @@ public void TestOptionalParamDecimal_2ndMissingArraySyntax()
12851285
Assert.AreEqual(expectedResult, result.Result);
12861286
}
12871287

1288+
[TestMethod]
1289+
public void TestOptionalParametersStrings_BothMissing()
1290+
{
1291+
string request = @"{method:'TestOptionalParameters_Strings',params:[],id:1}";
1292+
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":[null,null],\"id\":1}";
12881293

1294+
var result = JsonRpcProcessor.Process(request);
1295+
result.Wait();
1296+
Assert.IsFalse(result.Result.Contains("error"));
1297+
Assert.AreEqual(expectedResult, result.Result);
1298+
}
1299+
1300+
[TestMethod]
1301+
public void TestOptionalParametersStrings_SecondMissing()
1302+
{
1303+
string request = @"{method:'TestOptionalParameters_Strings',params:['first'],id:1}";
1304+
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":[\"first\",null],\"id\":1}";
1305+
1306+
var result = JsonRpcProcessor.Process(request);
1307+
result.Wait();
1308+
Assert.IsFalse(result.Result.Contains("error"));
1309+
Assert.AreEqual(expectedResult, result.Result);
1310+
}
1311+
1312+
[TestMethod]
1313+
public void TestOptionalParametersStrings_BothExists()
1314+
{
1315+
string request = @"{method:'TestOptionalParameters_Strings',params:['first','second'],id:1}";
1316+
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":[\"first\",\"second\"],\"id\":1}";
1317+
1318+
var result = JsonRpcProcessor.Process(request);
1319+
result.Wait();
1320+
Assert.IsFalse(result.Result.Contains("error"));
1321+
Assert.AreEqual(expectedResult, result.Result);
1322+
}
12891323
}
12901324
}

AustinHarris.JsonRpcTest/service.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,15 @@ public decimal TestOptionalParamdecimal_2x(decimal input1, decimal input2 = 987)
283283
{
284284
return input2;
285285
}
286-
286+
[JsonRpcMethod]
287+
private IList<string> TestOptionalParameters_Strings(string input1 = null, string input2 = null)
288+
{
289+
return new List<string>()
290+
{
291+
input1,
292+
input2
293+
};
294+
}
287295

288296
}
289297
}

Json-Rpc/AustinHarris.JsonRpc.csproj

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<SccLocalPath>SAK</SccLocalPath>
1919
<SccAuxPath>SAK</SccAuxPath>
2020
<SccProvider>SAK</SccProvider>
21+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\CoiniumServ\build\</SolutionDir>
22+
<RestorePackages>true</RestorePackages>
2123
</PropertyGroup>
2224
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
2325
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -69,16 +71,23 @@
6971
<Compile Include="SMDService.cs" />
7072
</ItemGroup>
7173
<ItemGroup>
72-
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
73-
<SpecificVersion>False</SpecificVersion>
74-
<HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
74+
<Reference Include="Newtonsoft.Json">
75+
<HintPath>..\packages\Newtonsoft.Json.6.0.5\lib\net40\Newtonsoft.Json.dll</HintPath>
76+
<Private>True</Private>
7577
</Reference>
7678
<Reference Include="System" />
7779
</ItemGroup>
7880
<ItemGroup>
7981
<None Include="packages.config" />
8082
</ItemGroup>
8183
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
84+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
85+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
86+
<PropertyGroup>
87+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
88+
</PropertyGroup>
89+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
90+
</Target>
8291
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
8392
Other similar extension points exist, see Microsoft.Common.targets.
8493
<Target Name="BeforeBuild">

Json-Rpc/Handler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
275275
{
276276
var paramIndex = parameters.Length; // the index we should start storing default values of optional parameters.
277277
var missingParamsCount = metaDataParamCount - parameters.Length; // the amount of optional parameters without a value set by rpc-call.
278-
Array.Resize(ref parameters, parameters.Length + metadata.defaultValues.Length); // resize the array to include all optional parameters.
278+
Array.Resize(ref parameters, parameters.Length + missingParamsCount); // resize the array to include all optional parameters.
279279

280280
// we need to add in reverse order as parameters can appear after all required parameters.
281281
// as some of the optional parameters could already have assigned their values in rpc-call,

Json-Rpc/JsonRpcProcessor.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,6 @@ public static Task<string> Process(string sessionId, string jsonRpc, object cont
221221
{
222222
var task = Task<string>.Factory.StartNew((_) =>
223223
{
224-
// use invariant culture - we have to set it explicitly for every thread we create to
225-
// prevent any floating-point problems (mostly because of number formats in non en-US cultures).
226-
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
227-
228224
var tup = (Tuple<string,string,object>)_;
229225
string _sessionId;
230226
string _jsonRpc;

0 commit comments

Comments
 (0)