Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging in lazy sequences may interfere with output of Content-Type: application/edn #131

Merged
merged 3 commits into from
Jan 9, 2025

Conversation

nvlass
Copy link
Contributor

@nvlass nvlass commented Feb 5, 2023

The case addressed in this PR is the following:

  1. The request handler response a lazy sequence (i.e. a map of some function function-with-logs on some range).
  2. The function-with-logs does some logging (either with prns or with clojure.tools.logging)
  3. The sequence is not realised until we read the body, in which case the edn output is mixed with the output of the logging in lazy sequence.

This can be easily avoided if all such responses are realised with a doall, so this pull request might not be a case at all. Forgetting to realise the sequences can, however, result in hard to spot errors.

The solution is not optimal, since it essentially replicates the functionality of pr and pr-on from clojure.core. Perhaps it should also include some tests from clojure.core to ensure that the function remains consistent with pr-str.

Commits:

  • Add pr-to-writer in edn.clj, Add test for lazy sequences

Copy link
Member

@opqdonut opqdonut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excellent change, but please simplify it by calling print-method directly!

(pr-str data)
^String charset))
(let [w (new java.io.StringWriter)]
(pr-to-writer w data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're only using arity 1 of pr-to-writer here, so you could just call (print-method data w) directly, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course, you are right! print-method is the way to go and it simplifies things! I'll update the PR. Thank you!

@opqdonut opqdonut merged commit 55a4387 into metosin:master Jan 9, 2025
4 checks passed
@opqdonut
Copy link
Member

opqdonut commented Jan 9, 2025

Thanks once again! Released as 0.6.11! 🎉

https://clojars.org/metosin/muuntaja/versions/0.6.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants