Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): update swashbuckle-aspnetcore monorepo to 6.9.0 #33

Merged
merged 10 commits into from
Jan 13, 2025
Merged
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 application/json content type.
PrincessMadMath marked this conversation as resolved.
Show resolved Hide resolved
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");
Gcaya marked this conversation as resolved.
Show resolved Hide resolved

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("Tem<>platedOkNoContentType")]
[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
Loading