Skip to content

Commit d257b09

Browse files
committed
refactor: simplify writing response headers
1 parent 01ac4be commit d257b09

4 files changed

Lines changed: 19 additions & 30 deletions

File tree

src/OpenAPI.WebApiGenerator/ApiGenerator.cs

Lines changed: 3 additions & 4 deletions
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(openApiVersion, rootNamespace);
91+
var httpResponseExtensionsGenerator = new HttpResponseExtensionsGenerator(rootNamespace);
9292
httpResponseExtensionsGenerator.GenerateHttpResponseExtensionsClass().AddTo(context);
9393

9494
var apiConfigurationGenerator = new ApiConfigurationGenerator(rootNamespace);
@@ -189,7 +189,7 @@ private static void GenerateCode(SourceProductionContext context, (
189189
var responseHeaderSchema = openApiResponseVisitor.GetSchemaReference(header);
190190
var typeDeclaration = schemaGenerator.Generate(responseHeaderSchema);
191191
return new ResponseHeaderGenerator(name, header, typeDeclaration,
192-
httpResponseExtensionsGenerator);
192+
openApiVersion);
193193
}).ToList() ?? [];
194194

195195
return new ResponseContentGenerator(
@@ -200,8 +200,7 @@ private static void GenerateCode(SourceProductionContext context, (
200200

201201
var responseGenerator = new ResponseGenerator(
202202
responseBodyGenerators,
203-
httpResponseExtensionsGenerator,
204-
openApiVersion);
203+
httpResponseExtensionsGenerator);
205204
var responseSourceCode =
206205
responseGenerator.GenerateResponseClass(
207206
operationNamespace,

src/OpenAPI.WebApiGenerator/CodeGeneration/HttpResponseExtensionsGenerator.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1-
using Microsoft.OpenApi;
2-
using OpenAPI.WebApiGenerator.OpenApi;
3-
4-
namespace OpenAPI.WebApiGenerator.CodeGeneration;
1+
namespace OpenAPI.WebApiGenerator.CodeGeneration;
52

63
internal sealed class HttpResponseExtensionsGenerator(
7-
OpenApiSpecVersion openApiVersion,
84
string @namespace)
95
{
106
private const string HttpResponseExtensionsClassName = "HttpResponseExtensions";
117
public string Namespace => @namespace;
128

13-
internal string GetResponseHeaderSpecificationAsJson(
14-
IOpenApiHeader header,
15-
string name) =>
16-
// 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
17-
$$"""
18-
{
19-
"name": "{{name}}",
20-
"in": "header",
21-
{{header.Serialize(openApiVersion).ToString().TrimStart('{').TrimStart()}}
22-
""";
23-
9+
2410
internal static string CreateWriteBodyInvocation(
2511
string responseVariableName,
2612
string contentVariableName) =>

src/OpenAPI.WebApiGenerator/CodeGeneration/ResponseGenerator.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using Microsoft.OpenApi;
43
using OpenAPI.WebApiGenerator.Extensions;
5-
using OpenAPI.WebApiGenerator.OpenApi;
64

75
namespace OpenAPI.WebApiGenerator.CodeGeneration;
86

97
internal sealed class ResponseGenerator(
108
List<ResponseContentGenerator> responseBodyGenerators,
11-
HttpResponseExtensionsGenerator httpResponseExtensionsGenerator,
12-
OpenApiSpecVersion openApiSpecVersion)
9+
HttpResponseExtensionsGenerator httpResponseExtensionsGenerator)
1310
{
1411
public SourceCode GenerateResponseClass(string @namespace, string path)
1512
{
@@ -23,9 +20,7 @@ public SourceCode GenerateResponseClass(string @namespace, string path)
2320
namespace {{@namespace}};
2421
2522
internal abstract partial class Response
26-
{
27-
private const string OpenApiVersion = "{{openApiSpecVersion.GetParameterVersion()}}";
28-
{{Enumerable.Range(1, 5).AggregateToString(i =>
23+
{{{Enumerable.Range(1, 5).AggregateToString(i =>
2924
$$"""
3025
protected int Validate{{i}}xxStatusCode(int code)
3126
=> (code >= {{i}}00 && code <= {{i}}99) ? code : throw new InvalidOperationException($"Expected {{i}}xx status code, got {code}");

src/OpenAPI.WebApiGenerator/CodeGeneration/ResponseHeaderGenerator.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
using Corvus.Json.CodeGeneration.CSharp;
33
using Microsoft.OpenApi;
44
using OpenAPI.WebApiGenerator.Extensions;
5+
using OpenAPI.WebApiGenerator.OpenApi;
56

67
namespace OpenAPI.WebApiGenerator.CodeGeneration;
78

89
internal sealed class ResponseHeaderGenerator(
910
string name,
1011
IOpenApiHeader header,
1112
TypeDeclaration typeDeclaration,
12-
HttpResponseExtensionsGenerator httpResponseExtensionsGenerator)
13+
OpenApiSpecVersion openApiSpecVersion)
1314
{
1415
private readonly string _propertyName = name.ToPascalCase();
1516
private readonly string _requiredDirective = header.Required ? "required" : string.Empty;
@@ -27,11 +28,19 @@ internal string GenerateProperty() =>
2728

2829
internal string GenerateWriteDirective(string responseVariableName)
2930
{
30-
var headerSpecificationAsJson = httpResponseExtensionsGenerator.GetResponseHeaderSpecificationAsJson(header, name);
31+
// 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
32+
var headerSpecificationAsJson =
33+
$$"""
34+
{
35+
"name": "{{name}}",
36+
"in": "header",
37+
{{header.Serialize(openApiSpecVersion).ToString().TrimStart('{').TrimStart()}}
38+
""";
39+
3140
return
3241
$""""
3342
{responseVariableName}.WriteResponseHeader(
34-
OpenApiVersion,
43+
"{openApiSpecVersion.GetParameterVersion()}",
3544
"""
3645
{headerSpecificationAsJson.Indent(4).TrimStart()}
3746
""",

0 commit comments

Comments
 (0)