Skip to content

Commit 255081d

Browse files
authored
fix: Use JSON serializer extracted from the DI container to serialize error responses (#1713)
1 parent bb92801 commit 255081d

9 files changed

+209
-112
lines changed

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.cs

Lines changed: 154 additions & 105 deletions
Large diffs are not rendered by default.

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.tt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@
299299
{
300300
#>
301301
var errorStream = new System.IO.MemoryStream();
302-
System.Text.Json.JsonSerializer.Serialize(errorStream, errorResult);
302+
serializer.Serialize(errorResult, errorStream);
303+
errorStream.Position = 0;
303304
return errorStream;
304305
<#
305306
}

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/CustomizeResponseExamples_NotFoundResponseWithHeaderV1Async_Generated.g.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public CustomizeResponseExamples_NotFoundResponseWithHeaderV1Async_Generated()
5252
StatusCode = 400
5353
};
5454
var errorStream = new System.IO.MemoryStream();
55-
System.Text.Json.JsonSerializer.Serialize(errorStream, errorResult);
55+
serializer.Serialize(errorResult, errorStream);
56+
errorStream.Position = 0;
5657
return errorStream;
5758
}
5859

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/CustomizeResponseExamples_NotFoundResponseWithHeaderV1_Generated.g.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public System.IO.Stream NotFoundResponseWithHeaderV1(Amazon.Lambda.APIGatewayEve
5252
StatusCode = 400
5353
};
5454
var errorStream = new System.IO.MemoryStream();
55-
System.Text.Json.JsonSerializer.Serialize(errorStream, errorResult);
55+
serializer.Serialize(errorResult, errorStream);
56+
errorStream.Position = 0;
5657
return errorStream;
5758
}
5859

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/CustomizeResponseExamples_NotFoundResponseWithHeaderV2Async_Generated.g.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public CustomizeResponseExamples_NotFoundResponseWithHeaderV2Async_Generated()
5252
StatusCode = 400
5353
};
5454
var errorStream = new System.IO.MemoryStream();
55-
System.Text.Json.JsonSerializer.Serialize(errorStream, errorResult);
55+
serializer.Serialize(errorResult, errorStream);
56+
errorStream.Position = 0;
5657
return errorStream;
5758
}
5859

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/CustomizeResponseExamples_NotFoundResponseWithHeaderV2_Generated.g.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public System.IO.Stream NotFoundResponseWithHeaderV2(Amazon.Lambda.APIGatewayEve
5252
StatusCode = 400
5353
};
5454
var errorStream = new System.IO.MemoryStream();
55-
System.Text.Json.JsonSerializer.Serialize(errorStream, errorResult);
55+
serializer.Serialize(errorResult, errorStream);
56+
errorStream.Position = 0;
5657
return errorStream;
5758
}
5859

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/CustomizeResponseExamples_OkResponseWithHeaderAsync_Generated.g.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public CustomizeResponseExamples_OkResponseWithHeaderAsync_Generated()
5252
StatusCode = 400
5353
};
5454
var errorStream = new System.IO.MemoryStream();
55-
System.Text.Json.JsonSerializer.Serialize(errorStream, errorResult);
55+
serializer.Serialize(errorResult, errorStream);
56+
errorStream.Position = 0;
5657
return errorStream;
5758
}
5859

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/CustomizeResponseExamples_OkResponseWithHeader_Generated.g.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public System.IO.Stream OkResponseWithHeader(Amazon.Lambda.APIGatewayEvents.APIG
5252
StatusCode = 400
5353
};
5454
var errorStream = new System.IO.MemoryStream();
55-
System.Text.Json.JsonSerializer.Serialize(errorStream, errorResult);
55+
serializer.Serialize(errorResult, errorStream);
56+
errorStream.Position = 0;
5657
return errorStream;
5758
}
5859

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
9+
namespace TestServerlessApp.IntegrationTests
10+
{
11+
[Collection("Integration Tests")]
12+
public class CustomResponse
13+
{
14+
private readonly IntegrationTestContextFixture _fixture;
15+
16+
public CustomResponse(IntegrationTestContextFixture fixture)
17+
{
18+
_fixture = fixture;
19+
}
20+
21+
[Fact]
22+
public async Task OkResponseWithHeader_Returns200Status()
23+
{
24+
var response = await _fixture.HttpClient.GetAsync($"{_fixture.RestApiUrlPrefix}/okresponsewithheader/1");
25+
response.EnsureSuccessStatusCode();
26+
Assert.Equal("All Good", await response.Content.ReadAsStringAsync());
27+
}
28+
29+
[Fact]
30+
public async Task OkResponseWithHeader_ReturnsValidationErrors()
31+
{
32+
var response = await _fixture.HttpClient.GetAsync($"{_fixture.RestApiUrlPrefix}/okresponsewithheader/hello");
33+
Assert.Equal(400, (int)response.StatusCode);
34+
var content = await response.Content.ReadAsStringAsync();
35+
var errorJson = JObject.Parse(content);
36+
37+
var expectedErrorMessage = "1 validation error(s) detected: Value hello at 'x' failed to satisfy constraint: Input string was not in a correct format.";
38+
Assert.Equal(expectedErrorMessage, errorJson["message"]);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)