apple / swift-openapi-generator

Generate Swift client and server code from an OpenAPI document.
https://swiftpackageindex.com/apple/swift-openapi-generator/documentation
Apache License 2.0
1.45k stars 121 forks source link

Generating Debug Output #616

Closed paulhdk closed 3 months ago

paulhdk commented 3 months ago

Question

Is it possible to somehow generate debug output, e.g., printing JSON data that is sent in the final request by the generated code?

I'm currently trying to debug an HTTP 400 response, and I wasn't able to find something in the other issue re: debug output.

Many thanks!

paulhdk commented 3 months ago

UPDATE: I solved it in the meantime by setting up a local nginx server, updating the openapi.yaml accordingly, and intercepting the requests for inspection.

simonjbeaumont commented 3 months ago

@paulhdk Sorry I didn't get to this sooner; I was out on vacation.

Glad you found a solution to your problem.

To answer your original question, though: you can achieve this in Swift OpenAPI by making use of a middleware. We have an example project that shows how to do this:

https://github.com/apple/swift-openapi-generator/tree/main/Examples/logging-middleware-swift-log-example

A word of caution when implementing logging middleware:

  1. Be cautious about logging sensitive information (e.g. PII and/or credentials that may be in HTTP header fields).
  2. Understand the implications of buffering, or otherwise, in a middleware implementation.