Skip to content

Commit 7f431e7

Browse files
committed
fix(response): write response header specifications using the correct format
1 parent ea938cd commit 7f431e7

4 files changed

Lines changed: 45 additions & 65 deletions

File tree

src/OpenAPI.WebApiGenerator/ApiGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static void GenerateCode(SourceProductionContext context, (
8888
rootNamespace);
8989
httpRequestExtensionsGenerator.GenerateHttpRequestExtensionsClass().AddTo(context);
9090

91-
var httpResponseExtensionsGenerator = new HttpResponseExtensionsGenerator(rootNamespace);
91+
var httpResponseExtensionsGenerator = new HttpResponseExtensionsGenerator(openApiVersion, rootNamespace);
9292
httpResponseExtensionsGenerator.GenerateHttpResponseExtensionsClass().AddTo(context);
9393

9494
var apiConfigurationGenerator = new ApiConfigurationGenerator(rootNamespace);

src/OpenAPI.WebApiGenerator/CodeGeneration/HttpResponseExtensionsGenerator.cs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
1-
using OpenAPI.WebApiGenerator.Extensions;
1+
using System;
2+
using System.IO;
3+
using Microsoft.OpenApi;
4+
using OpenAPI.WebApiGenerator.Extensions;
25

36
namespace OpenAPI.WebApiGenerator.CodeGeneration;
47

58
internal sealed class HttpResponseExtensionsGenerator(
9+
OpenApiSpecVersion openApiVersion,
610
string @namespace)
711
{
812
private const string HttpResponseExtensionsClassName = "HttpResponseExtensions";
913
public string Namespace => @namespace;
1014

11-
internal string CreateWriteHeaderInvocation(
12-
string responseVariableName,
13-
string headerSpecificationAsJson,
14-
string headerName,
15-
string headerValueVariableName,
16-
bool isRequired)
15+
internal string GetResponseHeaderSpecificationAsJson(
16+
IOpenApiHeader header,
17+
string name)
1718
{
18-
return
19-
$""""
20-
{responseVariableName}.WriteResponseHeader(
21-
"""
22-
{headerSpecificationAsJson.Indent(4)}
23-
""",
24-
"{headerName}",
25-
{headerValueVariableName},
26-
{isRequired.ToString().ToLowerInvariant()})
27-
"""";
19+
using var textWriter = new StringWriter();
20+
var jsonWriter = new OpenApiJsonWriter(textWriter, new OpenApiJsonWriterSettings
21+
{
22+
InlineLocalReferences = true
23+
});
24+
Action<IOpenApiWriter> serialize = openApiVersion switch
25+
{
26+
OpenApiSpecVersion.OpenApi3_1 => header.SerializeAsV31,
27+
OpenApiSpecVersion.OpenApi3_0 => header.SerializeAsV3,
28+
OpenApiSpecVersion.OpenApi2_0 => header.SerializeAsV2,
29+
_ => throw new NotSupportedException(
30+
$"OpenAPI version {Enum.GetName(typeof(OpenApiSpecVersion), openApiVersion)} not supported")
31+
};
32+
serialize(jsonWriter);
33+
textWriter.Flush();
34+
35+
// Response header specification is a subset of the parameter specification, so we add the missing properties to be able to use the parameter value parser
36+
return
37+
$$"""
38+
{
39+
"name": "{{name}}",
40+
"in": "header",
41+
{{textWriter.GetStringBuilder().ToString().TrimStart('{').TrimStart()}}
42+
""";
2843
}
2944

3045
internal string CreateWriteBodyInvocation(
Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.IO;
2-
using System.Linq;
3-
using Corvus.Json.CodeGeneration;
1+
using Corvus.Json.CodeGeneration;
42
using Corvus.Json.CodeGeneration.CSharp;
53
using Microsoft.OpenApi;
64
using OpenAPI.WebApiGenerator.Extensions;
@@ -29,28 +27,16 @@ internal string GenerateProperty() =>
2927

3028
internal string GenerateWriteDirective(string responseVariableName)
3129
{
32-
using var textWriter = new StringWriter();
33-
var jsonWriter = new OpenApiJsonWriter(textWriter, new OpenApiJsonWriterSettings
34-
{
35-
InlineLocalReferences = true
36-
});
37-
header.SerializeAsV2(jsonWriter);
38-
textWriter.Flush();
39-
40-
// Response header specification is a subset of the parameter specification, so we add the missing properties to be able to use the parameter value parser
41-
var headerSpecificationAsJson =
42-
$$"""
43-
{
44-
"name": "{{name}}",
45-
"in": "header",
46-
{{textWriter.GetStringBuilder().ToString().TrimStart('{').TrimStart()}}
47-
""";
48-
49-
return $"{httpResponseExtensionsGenerator.CreateWriteHeaderInvocation(
50-
responseVariableName,
51-
headerSpecificationAsJson,
52-
name,
53-
$"Headers.{_propertyName}",
54-
header.Required)};";
30+
var headerSpecificationAsJson = httpResponseExtensionsGenerator.GetResponseHeaderSpecificationAsJson(header, name);
31+
return
32+
$""""
33+
{responseVariableName}.WriteResponseHeader(
34+
"""
35+
{headerSpecificationAsJson.Indent(4).TrimStart()}
36+
""",
37+
"{name}",
38+
Headers.{_propertyName},
39+
{header.Required.ToString().ToLowerInvariant()});
40+
"""";
5541
}
5642
}

src/OpenAPI.WebApiGenerator/Extensions/OpenApiSchemaExtensions.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)