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

Ability to customize formatting of payloads #32

Open
ndreynolds opened this issue Feb 18, 2018 · 5 comments
Open

Ability to customize formatting of payloads #32

ndreynolds opened this issue Feb 18, 2018 · 5 comments

Comments

@ndreynolds
Copy link

Currently in the default and swagger writers, the request and response bodies are just assumed to be JSON and passed through the Poison.encode/1 with the pretty: true option.

In order to support other use cases (e.g., XML, other JSON libs) or just more control of the formatting, it would be great if it were possible to configure this without re-implementing the entire writer.

This could be done via options to the writer:

config :bureaucrat,
  writer: {Bureaucrat.MarkdownWriter, payload_formatter: MyHelper}

Along with a default implementation based on the current strategy.

Also open to other suggestions. I can work on a PR if that's something you'd be willing to include.

@mbuhot
Copy link
Contributor

mbuhot commented Feb 19, 2018

How about using the content-type header of the response to select the formatter?
It could be made pluggable if necessary, mapping mime types to the formatter function.

@ndreynolds
Copy link
Author

Yeah, I think selecting based on the content-type would make a lot of sense. Would also nicely enable mixed content types within an API. I'll experiment with it some and try to come up with a PR.

@OpakAlex
Copy link
Member

OpakAlex commented Jun 7, 2018

i think it's done?

@jangnezda
Copy link
Contributor

Slightly tangential - had issue when trying to document a file upload endpoint. The swagger library can be used normally for file uploads and the resulting swagger file is correct. However, various writers in bureaucrat fail when running tests, because they want to json encode a custom struct (Plug.Upload), example with Jason encoder:

** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for %Plug.Upload{content_type: "audio/mp4", filename: "sample.m4a", path: "test/fixtures/sample.m4a"} of type Plug.Upload (a struct), Jason.Encoder protocol must always be explicitly implemented.

Plug.Upload is nothing fancy, just a struct with a couple of fields and for anyone wondering how to fix it, add following to your test setup/helpers code:

  defimpl Jason.Encoder, for: Plug.Upload do
    def encode(value, opts) do
      Jason.Encode.map(Map.from_struct(value), opts)
    end
  end

@OpakAlex
Copy link
Member

OpakAlex commented Jan 6, 2020

nice tip, thanks!

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

No branches or pull requests

4 participants