Skip to content

Commit ea9c970

Browse files
committed
Add content length header
1 parent 7604846 commit ea9c970

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/JsonApiOperationDocumentationFilter.cs

+20
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ private static void ApplyGetPrimary(OpenApiOperation operation, ResourceType res
165165
SetOperationRemarks(operation, TextCompareETag);
166166
SetResponseDescription(operation.Responses, HttpStatusCode.OK, TextCompletedSuccessfully);
167167
SetResponseHeaderETag(operation.Responses, HttpStatusCode.OK);
168+
SetResponseHeaderContentLength(operation.Responses, HttpStatusCode.OK);
168169
SetResponseDescription(operation.Responses, HttpStatusCode.NotModified, TextNotModified);
169170
SetResponseHeaderETag(operation.Responses, HttpStatusCode.NotModified);
170171
}
@@ -194,6 +195,7 @@ private static void ApplyGetPrimary(OpenApiOperation operation, ResourceType res
194195
SetOperationRemarks(operation, TextCompareETag);
195196
SetResponseDescription(operation.Responses, HttpStatusCode.OK, TextCompletedSuccessfully);
196197
SetResponseHeaderETag(operation.Responses, HttpStatusCode.OK);
198+
SetResponseHeaderContentLength(operation.Responses, HttpStatusCode.OK);
197199
SetResponseDescription(operation.Responses, HttpStatusCode.NotModified, TextNotModified);
198200
SetResponseHeaderETag(operation.Responses, HttpStatusCode.NotModified);
199201
}
@@ -291,6 +293,7 @@ relationship is HasOneAttribute
291293
SetOperationRemarks(operation, TextCompareETag);
292294
SetResponseDescription(operation.Responses, HttpStatusCode.OK, TextCompletedSuccessfully);
293295
SetResponseHeaderETag(operation.Responses, HttpStatusCode.OK);
296+
SetResponseHeaderContentLength(operation.Responses, HttpStatusCode.OK);
294297
SetResponseDescription(operation.Responses, HttpStatusCode.NotModified, TextNotModified);
295298
SetResponseHeaderETag(operation.Responses, HttpStatusCode.NotModified);
296299
}
@@ -330,6 +333,7 @@ relationship is HasOneAttribute
330333

331334
SetOperationRemarks(operation, TextCompareETag);
332335
SetResponseDescription(operation.Responses, HttpStatusCode.OK, TextCompletedSuccessfully);
336+
SetResponseHeaderContentLength(operation.Responses, HttpStatusCode.OK);
333337
SetResponseHeaderETag(operation.Responses, HttpStatusCode.OK);
334338
SetResponseDescription(operation.Responses, HttpStatusCode.NotModified, TextNotModified);
335339
SetResponseHeaderETag(operation.Responses, HttpStatusCode.NotModified);
@@ -476,6 +480,22 @@ private static void SetResponseHeaderETag(OpenApiResponses responses, HttpStatus
476480
};
477481
}
478482

483+
private static void SetResponseHeaderContentLength(OpenApiResponses responses, HttpStatusCode statusCode)
484+
{
485+
OpenApiResponse response = GetOrAddResponse(responses, statusCode);
486+
487+
response.Headers[HeaderNames.ContentLength] = new OpenApiHeader
488+
{
489+
Description = "Size of the response body in bytes",
490+
Required = true,
491+
Schema = new OpenApiSchema
492+
{
493+
Type = "integer",
494+
},
495+
Example = new OpenApiInteger(322),
496+
};
497+
}
498+
479499
private static void SetResponseHeaderLocation(OpenApiResponses responses, HttpStatusCode statusCode)
480500
{
481501
OpenApiResponse response = GetOrAddResponse(responses, statusCode);

test/OpenApiTests/DocComments/DocCommentsTests.cs

+6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public async Task Endpoints_are_documented()
107107
responsesElement.Should().HaveProperty("200.description", "The operation completed successfully.");
108108
responsesElement.Should().HaveProperty("200.headers.ETag.description", "ETag identifying the version of the fetched resource.");
109109
responsesElement.Should().HaveProperty("200.headers.ETag.example", "\"33a64df551425fcc55e4d42a148795d9f25f89d4\"");
110+
responsesElement.Should().HaveProperty("200.headers.Content-Length.description", "Size of the response body in bytes");
110111
responsesElement.Should().HaveProperty("304.description", "The resource was not modified.");
111112
responsesElement.Should().HaveProperty("400.description", "The query string is invalid.");
112113
});
@@ -189,6 +190,7 @@ public async Task Endpoints_are_documented()
189190
responsesElement.Should().HaveProperty("200.description", "The operation completed successfully.");
190191
responsesElement.Should().HaveProperty("200.headers.ETag.description", "ETag identifying the version of the fetched resource.");
191192
responsesElement.Should().HaveProperty("200.headers.ETag.example", "\"33a64df551425fcc55e4d42a148795d9f25f89d4\"");
193+
responsesElement.Should().HaveProperty("200.headers.Content-Length.description", "Size of the response body in bytes");
192194
responsesElement.Should().HaveProperty("304.description", "The resource was not modified.");
193195
responsesElement.Should().HaveProperty("400.description", "The query string is invalid.");
194196
responsesElement.Should().HaveProperty("404.description", "The skyscraper does not exist.");
@@ -295,6 +297,7 @@ public async Task Endpoints_are_documented()
295297
responsesElement.Should().HaveProperty("200.description", "The operation completed successfully.");
296298
responsesElement.Should().HaveProperty("200.headers.ETag.description", "ETag identifying the version of the fetched resource.");
297299
responsesElement.Should().HaveProperty("200.headers.ETag.example", "\"33a64df551425fcc55e4d42a148795d9f25f89d4\"");
300+
responsesElement.Should().HaveProperty("200.headers.Content-Length.description", "Size of the response body in bytes");
298301
responsesElement.Should().HaveProperty("304.description", "The resource was not modified.");
299302
responsesElement.Should().HaveProperty("400.description", "The query string is invalid.");
300303
responsesElement.Should().HaveProperty("404.description", "The skyscraper does not exist.");
@@ -355,6 +358,7 @@ public async Task Endpoints_are_documented()
355358
responsesElement.Should().HaveProperty("200.description", "The operation completed successfully.");
356359
responsesElement.Should().HaveProperty("200.headers.ETag.description", "ETag identifying the version of the fetched resource.");
357360
responsesElement.Should().HaveProperty("200.headers.ETag.example", "\"33a64df551425fcc55e4d42a148795d9f25f89d4\"");
361+
responsesElement.Should().HaveProperty("200.headers.Content-Length.description", "Size of the response body in bytes");
358362
responsesElement.Should().HaveProperty("304.description", "The resource was not modified.");
359363
responsesElement.Should().HaveProperty("400.description", "The query string is invalid.");
360364
responsesElement.Should().HaveProperty("404.description", "The skyscraper does not exist.");
@@ -438,6 +442,7 @@ public async Task Endpoints_are_documented()
438442
responsesElement.Should().HaveProperty("200.description", "The operation completed successfully.");
439443
responsesElement.Should().HaveProperty("200.headers.ETag.description", "ETag identifying the version of the fetched resource.");
440444
responsesElement.Should().HaveProperty("200.headers.ETag.example", "\"33a64df551425fcc55e4d42a148795d9f25f89d4\"");
445+
responsesElement.Should().HaveProperty("200.headers.Content-Length.description", "Size of the response body in bytes");
441446
responsesElement.Should().HaveProperty("304.description", "The resource was not modified.");
442447
responsesElement.Should().HaveProperty("400.description", "The query string is invalid.");
443448
responsesElement.Should().HaveProperty("404.description", "The skyscraper does not exist.");
@@ -498,6 +503,7 @@ public async Task Endpoints_are_documented()
498503
responsesElement.Should().HaveProperty("200.description", "The operation completed successfully.");
499504
responsesElement.Should().HaveProperty("200.headers.ETag.description", "ETag identifying the version of the fetched resource.");
500505
responsesElement.Should().HaveProperty("200.headers.ETag.example", "\"33a64df551425fcc55e4d42a148795d9f25f89d4\"");
506+
responsesElement.Should().HaveProperty("200.headers.Content-Length.description", "Size of the response body in bytes");
501507
responsesElement.Should().HaveProperty("304.description", "The resource was not modified.");
502508
responsesElement.Should().HaveProperty("400.description", "The query string is invalid.");
503509
responsesElement.Should().HaveProperty("404.description", "The skyscraper does not exist.");

0 commit comments

Comments
 (0)