Skip to content

Commit 7d65bef

Browse files
committed
Remove reference for Newtonsoft.Json in Amazon.Lambda.APIGatewayEvents when using netcoreapp3.1
1 parent 31af2ef commit 7d65bef

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerContext.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
using System.Collections.Generic;
55
using System.Runtime.Serialization;
66

7+
#if NETSTANDARD_2_0
8+
using Newtonsoft.Json.Linq;
9+
#else
10+
using System.Text.Json;
11+
#endif
12+
13+
714
/// <summary>
815
/// An object representing the expected format of an API Gateway custom authorizer response.
916
/// </summary>
@@ -115,6 +122,7 @@ public Dictionary<string, string> Claims
115122
object value;
116123
if(this.TryGetValue("claims", out value))
117124
{
125+
#if NETSTANDARD_2_0
118126
JObject jsonClaims = value as JObject;
119127
if (jsonClaims != null)
120128
{
@@ -124,6 +132,18 @@ public Dictionary<string, string> Claims
124132

125133
}
126134
}
135+
#else
136+
if(value is JsonElement jsonClaims)
137+
{
138+
foreach(JsonProperty property in jsonClaims.EnumerateObject())
139+
{
140+
if(property.Value.ValueKind == JsonValueKind.String)
141+
{
142+
_claims[property.Name] = property.Value.GetString();
143+
}
144+
}
145+
}
146+
#endif
127147
}
128148
}
129149

Libraries/src/Amazon.Lambda.APIGatewayEvents/Amazon.Lambda.APIGatewayEvents.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
<PackageTags>AWS;Amazon;Lambda</PackageTags>
1313
</PropertyGroup>
1414

15+
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
16+
<DefineConstants>NETSTANDARD_2_0</DefineConstants>
17+
</PropertyGroup>
18+
1519
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
1620
<DefineConstants>NETCOREAPP_3_1</DefineConstants>
1721
</PropertyGroup>
1822

19-
<ItemGroup>
23+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
2024
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
2125
</ItemGroup>
2226

Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/Amazon.Lambda.AspNetCoreServer.Test.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
1212
</PropertyGroup>
1313

14+
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
15+
<DefineConstants>NETCOREAPP_2_1</DefineConstants>
16+
</PropertyGroup>
17+
18+
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
19+
<DefineConstants>NETCOREAPP_3_1</DefineConstants>
20+
</PropertyGroup>
21+
1422
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
1523
<NoWarn>1701;1702;1705;CS0618</NoWarn>
1624
</PropertyGroup>

Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestCallingWebAPI.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,12 @@ private async Task<APIGatewayProxyResponse> InvokeAPIGatewayRequest(TestLambdaCo
403403
private async Task<APIGatewayProxyResponse> InvokeAPIGatewayRequestWithContent(TestLambdaContext context, string requestContent)
404404
{
405405
var lambdaFunction = new ApiGatewayLambdaFunction();
406-
var request = JsonConvert.DeserializeObject<APIGatewayProxyRequest>(requestContent);
406+
var requestStream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(requestContent));
407+
#if NETCOREAPP_2_1
408+
var request = new Amazon.Lambda.Serialization.Json.JsonSerializer().Deserialize<APIGatewayProxyRequest>(requestStream);
409+
#else
410+
var request = new Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer().Deserialize<APIGatewayProxyRequest>(requestStream);
411+
#endif
407412
return await lambdaFunction.FunctionHandlerAsync(request, context);
408413
}
409414

0 commit comments

Comments
 (0)