Skip to content

Commit

Permalink
Remove HttpStatusCode=OK from WriteAsJsonAsync (#2720)
Browse files Browse the repository at this point in the history
* WriteAsJsonAsync HttpStatusCode Fix
  • Loading branch information
RohitRanjanMS authored and brettsam committed Oct 21, 2024
1 parent 889f2f8 commit b6840ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 94 deletions.
1 change: 1 addition & 0 deletions sdk/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Updated `Microsoft.Azure.Functions.Worker.Sdk.Generators` reference to 1.3.4.
- Setting _ToolingSuffix for TargetFrameworkVersion v9.0
- Adding support for SDK container builds with Functions base images
- Removed the default value for HttpStatusCode in WriteAsJsonAsync (#2720)

### Microsoft.Azure.Functions.Worker.Sdk.Generators 1.3.4

Expand Down
97 changes: 9 additions & 88 deletions src/DotNetWorker.Core/Http/HttpResponseDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static Task WriteStringAsync(this HttpResponseData response, string value

/// <summary>
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to 200.
/// The response content-type will be set to <code>application/json; charset=utf-8</code>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="response">The response to write JSON to.</param>
Expand All @@ -90,28 +90,12 @@ public static Task WriteStringAsync(this HttpResponseData response, string value
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous operation.</returns>
public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T instance, CancellationToken cancellationToken = default)
{
return WriteAsJsonAsync(response, instance, "application/json; charset=utf-8", HttpStatusCode.OK, cancellationToken);
return WriteAsJsonAsync(response, instance, "application/json; charset=utf-8", cancellationToken);
}

/// <summary>
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to the provided <paramref name="statusCode"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="response">The response to write JSON to.</param>
/// <param name="instance">The instance to serialize and write as JSON.</param>
/// <param name="statusCode">The status code to set on the response.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous operation.</returns>
public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T instance, HttpStatusCode statusCode,
CancellationToken cancellationToken = default)
{
return WriteAsJsonAsync(response, instance, "application/json; charset=utf-8", statusCode, cancellationToken);
}

/// <summary>
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to 200.
/// The response content-type will be set to the provided <paramref name="contentType"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="response">The response to write JSON to.</param>
Expand All @@ -128,37 +112,12 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in

ObjectSerializer serializer = GetObjectSerializer(response);

return WriteAsJsonAsync(response, instance, serializer, contentType, HttpStatusCode.OK, cancellationToken);
}

/// <summary>
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to the provided <paramref name="statusCode"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="response">The response to write JSON to.</param>
/// <param name="instance">The instance to serialize and write as JSON.</param>
/// <param name="contentType">The content-type to set on the response.</param>
/// <param name="statusCode">The status code to set on the response.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous operation.</returns>
public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T instance, string contentType, HttpStatusCode statusCode,
CancellationToken cancellationToken = default)
{
if (response is null)
{
throw new ArgumentNullException(nameof(response));
}

ObjectSerializer serializer = GetObjectSerializer(response);

return WriteAsJsonAsync(response, instance, serializer, contentType, statusCode, cancellationToken);
return WriteAsJsonAsync(response, instance, serializer, contentType, cancellationToken);
}


/// <summary>
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to 200.
/// The response content-type will be set to <code>application/json; charset=utf-8</code>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="response">The response to write JSON to.</param>
Expand All @@ -168,29 +127,12 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous operation.</returns>
public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T instance, ObjectSerializer serializer, CancellationToken cancellationToken = default)
{
return WriteAsJsonAsync(response, instance, serializer, "application/json; charset=utf-8", HttpStatusCode.OK, cancellationToken);
return WriteAsJsonAsync(response, instance, serializer, "application/json; charset=utf-8", cancellationToken);
}

/// <summary>
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to the provided <paramref name="statusCode"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="response">The response to write JSON to.</param>
/// <param name="instance">The instance to serialize and write as JSON.</param>
/// <param name="serializer">The serializer used to serialize the instance.</param>
/// <param name="statusCode">The status code to set on the response.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous operation.</returns>
public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T instance, ObjectSerializer serializer, HttpStatusCode statusCode,
CancellationToken cancellationToken = default)
{
return WriteAsJsonAsync(response, instance, serializer, "application/json; charset=utf-8", statusCode, cancellationToken);
}


/// <summary>
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to 200.
/// The response content-type will be set to the provided <paramref name="contentType"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="response">The response to write JSON to.</param>
Expand All @@ -199,26 +141,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
/// <param name="contentType">The content-type to set on the response.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous operation.</returns>
public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T instance,
ObjectSerializer serializer, string contentType,
CancellationToken cancellationToken = default)
{
return WriteAsJsonAsync(response, instance, serializer, contentType, HttpStatusCode.OK, cancellationToken);
}

/// <summary>
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to the provided <paramref name="statusCode"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="response">The response to write JSON to.</param>
/// <param name="instance">The instance to serialize and write as JSON.</param>
/// <param name="serializer">The serializer used to serialize the instance.</param>
/// <param name="contentType">The content-type to set on the response.</param>
/// <param name="statusCode">The status code to set on the response.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous operation.</returns>
public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T instance, ObjectSerializer serializer, string contentType, HttpStatusCode statusCode, CancellationToken cancellationToken = default)
public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T instance, ObjectSerializer serializer, string contentType, CancellationToken cancellationToken = default)
{
if (response is null)
{
Expand All @@ -236,8 +159,6 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
}

response.Headers.Add("Content-Type", contentType);
response.StatusCode = statusCode;

return serializer.SerializeAsync(response.Body, instance, typeof(T), cancellationToken);
}

Expand Down
12 changes: 6 additions & 6 deletions test/DotNetWorkerTests/Http/HttpResponseDataExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task WriteAsJsonAsync_UsesRegisteredSerializer()
public async Task WriteAsJsonAsync_ContentTypeOverload_AppliesParameters()
{
FunctionContext context = CreateContext(new NewtonsoftJsonObjectSerializer());
var response = CreateResponse(context);
var response = CreateResponse(context, HttpStatusCode.Accepted);

var poco = new ResponsePoco
{
Expand All @@ -81,23 +81,23 @@ public async Task WriteAsJsonAsync_ContentTypeOverload_AppliesParameters()
string result = ReadResponseBody(response);

Assert.Equal("application/json", response.Headers.GetValues("content-type").FirstOrDefault());
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
Assert.Equal("{\"jsonnetname\":\"Test\",\"jsonnetint\":42}", result);
}

[Fact]
public async Task WriteAsJsonAsync_StatusCodeOverload_AppliesParameters()
{
FunctionContext context = CreateContext(new NewtonsoftJsonObjectSerializer());
var response = CreateResponse(context);
var response = CreateResponse(context, HttpStatusCode.BadRequest);

var poco = new ResponsePoco
{
Name = "Test",
SomeInt = 42
};

await HttpResponseDataExtensions.WriteAsJsonAsync(response, poco, HttpStatusCode.BadRequest);
await HttpResponseDataExtensions.WriteAsJsonAsync(response, poco);

string result = ReadResponseBody(response);

Expand Down Expand Up @@ -127,9 +127,9 @@ public async Task WriteAsJsonAsync_SerializerAndContentTypeOverload_AppliesParam
Assert.Equal("{\"jsonnetname\":\"Test\",\"jsonnetint\":42}", result);
}

private static TestHttpResponseData CreateResponse(FunctionContext context)
private static TestHttpResponseData CreateResponse(FunctionContext context, HttpStatusCode statusCode = HttpStatusCode.OK)
{
var response = new TestHttpResponseData(context, HttpStatusCode.Accepted);
var response = new TestHttpResponseData(context, statusCode);
response.Body = new MemoryStream();
response.Headers = new HttpHeadersCollection();
return response;
Expand Down

0 comments on commit b6840ab

Please sign in to comment.