Skip to content

Commit

Permalink
chore(deps): update swashbuckle-aspnetcore monorepo to 6.9.0 (#33)
Browse files Browse the repository at this point in the history
* chore(deps): update swashbuckle-aspnetcore monorepo to 6.9.0

* feat: rename TypedResult controller and add proper content type test

* Should be fixed

* That should not be there

* One day it will work

* Again

* Guess we don't want that...

* Reword comment

---------

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: guillaume.caya <guillaume.caya@gsoft.com>
  • Loading branch information
3 people authored Jan 13, 2025
1 parent 3118f82 commit 093546e
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.9.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net.Mime;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -9,7 +10,7 @@ namespace Workleap.Extensions.OpenAPI.TypedResult;
internal sealed class ExtractSchemaTypeResultFilter : IOperationFilter
{
// Based on this documentation: https://learn.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-8.0
private const string DefaultContentType = "application/json";
private const string DefaultContentType = MediaTypeNames.Application.Json;

public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
Expand All @@ -24,6 +25,13 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
// when the ProducesResponseType attribute is present.
if (operation.Responses.TryGetValue(responseMetadata.HttpCode.ToString(), out var existingResponse))
{
// If no content type is specified, three will be added by default: application/json, text/plain, and text/json.
// In this case we want to enforce the proper content type associated with the method's return type.
if (IsDefaultContentTypes(existingResponse.Content))
{
existingResponse.Content.Clear();
}

var canEnrichContent = !existingResponse.Content.Any() && responseMetadata.SchemaType != null;

if (!canEnrichContent)
Expand Down Expand Up @@ -59,6 +67,11 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
}
}

private static bool IsDefaultContentTypes(IDictionary<string, OpenApiMediaType> contentTypes) =>
contentTypes.ContainsKey(MediaTypeNames.Application.Json) &&
contentTypes.ContainsKey(MediaTypeNames.Text.Plain) &&
contentTypes.ContainsKey("text/json");

internal static IEnumerable<ResponseMetadata> GetResponsesMetadata(Type returnType)
{
// Unwrap Task<> to get the return type
Expand Down Expand Up @@ -146,11 +159,9 @@ internal static HashSet<int> ExtractResponseCodesFromAttributes(IEnumerable<Cust
{
return new(statusCode, null);
}

// For types like Ok<T>, BadRequest<T>, NotFound<T>
else
{
return new(statusCode, resultType.GenericTypeArguments.First());
}
return new(statusCode, resultType.GenericTypeArguments.First());
}

private static int? ExtractStatusCodeFromType(Type resultType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;

namespace WebApi.OpenAPI.SystemTest.ExtractTypeResult;

public class TypedResultProperContentTypeController
{
[HttpGet]
[EndpointName("OkNoContentType")]
[Route("/useApplicationJsonContentTypeWithOk")]
[ProducesResponseType<string>(StatusCodes.Status200OK)]
public Ok GivenOkTypedResultAndNoContenTypeThenContentTypeApplicationJson()
{
return TypedResults.Ok();
}

[HttpGet]
[EndpointName("TemplatedOkNoContentType")]
[Route("/useApplicationJsonContentTypeWithOk<T>")]
[ProducesResponseType<string>(StatusCodes.Status200OK)]
public Ok<string> GivenTemplatedOkTypedResultAndNoContenTypeThenContentTypeApplicationJson()
{
return TypedResults.Ok("example");
}

[HttpGet]
[EndpointName("ResultsNoContentType")]
[Route("/useApplicationJsonContentTypeWithResultsType")]
[ProducesResponseType<string>(StatusCodes.Status200OK)]
[ProducesResponseType<string>(StatusCodes.Status400BadRequest, "text/plain")]
[ProducesResponseType<string>(StatusCodes.Status404NotFound, "text/plain")]
public Results<Ok<string>, BadRequest<string>, NotFound<string>> GivenResultsTypeAndNoContentTypeThenContentTypeApplicationJson()
{
return TypedResults.Ok("example");
}

[HttpGet]
[EndpointName("OkContentTypeTextPlain")]
[Route("/overwriteContenTypeWithProduceAttributeTextPlainForOk")]
[ProducesResponseType<string>(StatusCodes.Status200OK, "text/plain")]
public Ok GivenOkTypedResultAndContentTypeThenKeepContentType()
{
return TypedResults.Ok();
}

[HttpGet]
[EndpointName("TemplatedOkContentTypeTextPlain")]
[Route("/overwriteContenTypeWithProduceAttributeTextPlainForOk<T>")]
[ProducesResponseType<string>(StatusCodes.Status200OK, "text/plain")]
public Ok<string> GivenTemplatedOkTypedResultAndContentTypeThenKeepContentType()
{
return TypedResults.Ok("example");
}

[HttpGet]
[EndpointName("ResultsContentTypeTextPlain")]
[Route("/overwriteContenTypeWithProduceAttributeTextPlainForResultsType")]
[ProducesResponseType<string>(StatusCodes.Status200OK, "text/plain")]
[ProducesResponseType<string>(StatusCodes.Status400BadRequest, "text/plain")]
[ProducesResponseType<string>(StatusCodes.Status404NotFound, "text/plain")]
public Results<Ok<string>, BadRequest<string>, NotFound<string>> GivenResultsTypeAndContentTypeThenKeepContentType()
{
return TypedResults.Ok("example");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.9.0" />
<PackageReference Include="Workleap.OpenApi.MSBuild" Version="0.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Loading

0 comments on commit 093546e

Please sign in to comment.