Skip to content

Commit 368f79e

Browse files
committed
fix null dereferences in ensure expected content type method
1 parent 6ff59f0 commit 368f79e

3 files changed

Lines changed: 6 additions & 4 deletions

File tree

src/OpenAPI.WebApiGenerator/CodeGeneration/ResponseGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public SourceCode GenerateResponseClass(string @namespace, string path)
1414
$$"""
1515
#nullable enable
1616
using Corvus.Json;
17+
using System.Net.Http.Headers;
1718
using System.Text.Json;
1819
using {{httpResponseExtensionsGenerator.Namespace}};
1920
@@ -47,8 +48,9 @@ protected void EnsureExpectedContentType(MediaTypeHeaderValue contentType, Media
4748
{
4849
"*/*" => true,
4950
not null when expectedContentType.MediaType.EndsWith("*") =>
50-
contentType.MediaType?.StartsWith(expectedContentType.MediaType.TrimEnd('*'), StringComparison.OrdinalIgnoreCase),
51-
_ => contentType.MediaType.Equals(expectedContentType.MediaType, StringComparison.OrdinalIgnoreCase)
51+
contentType.MediaType?.StartsWith(expectedContentType.MediaType.TrimEnd('*'), StringComparison.OrdinalIgnoreCase) ?? false,
52+
not null => contentType.MediaType?.Equals(expectedContentType.MediaType, StringComparison.OrdinalIgnoreCase) ?? false,
53+
_ => false
5254
};
5355
5456
if (valid)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal partial Task<Response> HandleAsync(Request request, CancellationToken c
2727
_ = request.Header.Bar;
2828

2929
var response = new Response.OK200(Components.Schemas.FooProperties.Create(
30-
name: request.Body.ApplicationJson?.Name))
30+
name: request.Body.ApplicationJson?.Name), "application/json")
3131
{
3232
Headers = new Response.OK200.ResponseHeaders
3333
{

tests/Example.OpenApi32/openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
}
4949
},
5050
"content": {
51-
"application/json": {
51+
"application/*": {
5252
"schema": {
5353
"$ref": "#/components/schemas/FooProperties"
5454
}

0 commit comments

Comments
 (0)