Skip to content

Commit b1c3126

Browse files
committed
simplify content negotiation scaffolding with an interface
1 parent c679840 commit b1c3126

7 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/OpenAPI.WebApiGenerator/CodeGeneration/RequestGenerator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ internal partial class Request
7979
/// <param name="mediaTypes">Media types to match against</param>
8080
/// <param name="matchedMediaType">Matched media type if method returns true</param>
8181
/// <returns>True if a matched media type was found</returns>
82-
internal bool TryMatchAcceptMediaType(
83-
MediaTypeHeaderValue[] mediaTypes,
84-
[NotNullWhen(true)] out MediaTypeHeaderValue? matchedMediaType) =>
85-
HttpContext.Request.TryMatchAcceptMediaType(mediaTypes, out matchedMediaType);
82+
internal bool TryMatchAcceptMediaType<T>(
83+
[NotNullWhen(true)] out MediaTypeHeaderValue? matchedMediaType) where T : class, IResponse =>
84+
HttpContext.Request.TryMatchAcceptMediaType(T.ContentMediaTypes, out matchedMediaType);
8685
8786
/// <summary>
8887
/// Validate the request

src/OpenAPI.WebApiGenerator/CodeGeneration/ResponseContentGenerator.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public string GenerateResponseContentClass()
6262
return
6363
$$$"""
6464
{{{_response.Description.AsComment("summary", "para")}}}
65-
internal {{{(_contentGenerators.Any() ? "abstract" : "sealed")}}} class {{{_responseClassName}}} : Response
65+
internal {{{(_contentGenerators.Any() ? "abstract" : "sealed")}}} class {{{_responseClassName}}} : Response{{{(_contentGenerators.Any() ? ", IResponse" : "")}}}
6666
{
6767
private string? {{{contentTypeFieldName}}} = null;{{{
6868
_contentGenerators.AggregateToString(generator =>
@@ -74,10 +74,8 @@ public string GenerateResponseContentClass()
7474
protected abstract IJsonValue Content { get; }
7575
protected abstract string ContentSchemaLocation { get; }
7676
77-
/// <summary>
78-
/// Content media types
79-
/// </summary>
80-
internal static readonly MediaTypeHeaderValue[] ContentMediaTypes =
77+
/// <inheritdoc/>
78+
public static MediaTypeHeaderValue[] ContentMediaTypes { get; } =
8179
[{{_contentGenerators.AggregateToString(generator =>
8280
$$"""
8381
{{generator.ClassName}}.ContentMediaType,

src/OpenAPI.WebApiGenerator/CodeGeneration/ResponseGenerator.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ protected void EnsureExpectedContentType(MediaTypeHeaderValue contentType, Media
6767
generator.GenerateResponseContentClass()).Indent(4)
6868
}}
6969
}
70+
71+
internal interface IResponse
72+
{
73+
/// <summary>
74+
/// Content media types
75+
/// </summary>
76+
internal static abstract MediaTypeHeaderValue[] ContentMediaTypes { get; }
77+
}
7078
#nullable restore
7179
""");
7280
}

tests/Example.OpenApi20/Paths/FooFooId/Put/Operation.Handler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal partial Task<Response> HandleAsync(Request request, CancellationToken c
3535
_ = request.Path.FooId;
3636
_ = request.Header.Bar;
3737

38-
switch (request.TryMatchAcceptMediaType(Response.OK200.ContentMediaTypes, out var matchedMediaType))
38+
switch (request.TryMatchAcceptMediaType<Response.OK200>(out var matchedMediaType))
3939
{
4040
case false:
4141
case true when ReferenceEquals(matchedMediaType, Response.OK200.ApplicationJson.ContentMediaType):

tests/Example.OpenApi30/Paths/FooFooId/Put/Operation.Handler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal partial Task<Response> HandleAsync(Request request, CancellationToken c
3434
_ = request.Path.FooId;
3535
_ = request.Header.Bar;
3636

37-
switch (request.TryMatchAcceptMediaType(Response.OK200.ContentMediaTypes, out var matchedMediaType))
37+
switch (request.TryMatchAcceptMediaType<Response.OK200>(out var matchedMediaType))
3838
{
3939
case false:
4040
case true when ReferenceEquals(matchedMediaType, Response.OK200.ApplicationJson.ContentMediaType):

tests/Example.OpenApi31/Paths/FooFooId/Put/Operation.Handler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal partial Task<Response> HandleAsync(Request request, CancellationToken c
3333
_ = request.Path.FooId;
3434
_ = request.Header.Bar;
3535

36-
switch (request.TryMatchAcceptMediaType(Response.OK200.ContentMediaTypes, out var matchedMediaType))
36+
switch (request.TryMatchAcceptMediaType<Response.OK200>(out var matchedMediaType))
3737
{
3838
case false:
3939
case true when ReferenceEquals(matchedMediaType, Response.OK200.ApplicationJson.ContentMediaType):

tests/Example.OpenApi32/Paths/FooFooId/Put/Operation.Handler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal partial Task<Response> HandleAsync(Request request, CancellationToken c
3232
_ = request.Query.Fee;
3333
_ = request.Path.FooId;
3434

35-
switch (request.TryMatchAcceptMediaType(Response.OK200.ContentMediaTypes, out var matchedMediaType))
35+
switch (request.TryMatchAcceptMediaType<Response.OK200>(out var matchedMediaType))
3636
{
3737
case false:
3838
case true when ReferenceEquals(matchedMediaType, Response.OK200.AnyApplication.ContentMediaType):

0 commit comments

Comments
 (0)