Documenting response headers added by an interceptor

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.

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.

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(...))

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.:

val myEndpoints: List[AnyEndpoint] = ???

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

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

Wow, it works indeed!

Thank you very much, Adam!