# Documenting response headers added by an interceptor

**URL:** <https://softwaremill.community/t/documenting-response-headers-added-by-an-interceptor/201>\
**Category:** tapir\
**Created:** [May 10, 2023, 4:47pm UTC](https://softwaremill.community/t/documenting-response-headers-added-by-an-interceptor/201 "2023-05-10T16:47:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anton.filimonov](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/anton.filimonov/32/142_2.png) [@anton.filimonov](https://softwaremill.community/u/anton.filimonov)\
**Post date:** [May 10, 2023, 4:47pm UTC](https://softwaremill.community/t/documenting-response-headers-added-by-an-interceptor/201/1 "2023-05-10T16:47:27Z")

</div>

I’ve created an interceptor that generates a new request-id and adds an additional X-Request-Id header to the response. And I want to see the header info in the Swagger response definitions.

The `endpoint.out(header(...))` option is not suitable, it will add a new dynamic or fixed header value to the response. I’ve tried a hack to use a fixed header and substitute it in the interceptor with the real value. It works, but it’s still an ugly hack and my tests based on the sttp-client are failing since the expected fixed header value is not equal to the generated value.

---

<div class="post-metadata">

**Author:** ![kciesielski](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/kciesielski/32/133_2.png) [@kciesielski](https://softwaremill.community/u/kciesielski)\
**Post date:** [May 11, 2023, 5:58am UTC](https://softwaremill.community/t/documenting-response-headers-added-by-an-interceptor/201/2 "2023-05-11T05:58:01Z")

</div>

Still a workaround, but how about an optional header? (`.endpoint.out(header[Option[T]](...)`). This may help with the tests and expectations. The specification will not be accurate, but maybe it’s acceptable in this direction, where `required: false` but in reality it’s always set.

---

<div class="post-metadata">

**Author:** ![anton.filimonov](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/anton.filimonov/32/142_2.png) [@anton.filimonov](https://softwaremill.community/u/anton.filimonov)\
**Post date:** [May 11, 2023, 7:48am UTC](https://softwaremill.community/t/documenting-response-headers-added-by-an-interceptor/201/3 "2023-05-11T07:48:02Z")

</div>

Thanks for looking at it!  
The optional header can be a short-term fix. Once I’ll need to add not one but N headers it will be really annoying to support.

I think it would be nice to have an idiomatic approach for such cases, something like `endpoint.out(interceptorHeader(...))`

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [May 11, 2023, 8:09pm UTC](https://softwaremill.community/t/documenting-response-headers-added-by-an-interceptor/201/4 "2023-05-11T20:09:52Z")

</div>

If you have a list of endpoints for which you want to generate documentation, you can modify that list before passing it to the documentation interpreter. E.g.:

```auto
val myEndpoints: List[AnyEndpoint] = ???

val myEndpointsWithHeader = myEndpoints.map(_.out(header(...)))

val swaggerEndpoints = SwaggerInterpreter().fromEndpoints[Future](myEndpointsWithHeader, "My App", "1.0")

```

---

<div class="post-metadata">

**Author:** ![anton.filimonov](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/anton.filimonov/32/142_2.png) [@anton.filimonov](https://softwaremill.community/u/anton.filimonov)\
**Post date:** [May 12, 2023, 1:37pm UTC](https://softwaremill.community/t/documenting-response-headers-added-by-an-interceptor/201/5 "2023-05-12T13:37:17Z")

</div>

Wow, it works indeed!

Thank you very much, Adam!
