You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 0342ddc
Browse filesBrowse the repository at this point in the historyBrowse files
Add :md and :markdown formatters that render an object's to_markdown (#3025)
Rails 8.1 registers text/markdown as Mime[:md] and adds
`render markdown: obj`, which calls obj.to_markdown. Grape::Formatter::Markdown
is the same contract for Grape: an object answering to_markdown is
rendered with it and a String is returned as is. Anything else raises
Grape::Exceptions::InvalidFormatter, so a client that negotiated Markdown
for a Hash body gets the default format instead of its #to_s.
.md and .markdown are both common Markdown extensions, so :md and
:markdown name the same format. Declaring either content type answers
both extensions and both ?format= spellings: the Formatter middleware
resolves the other name through ContentTypes::FORMAT_SYNONYMS to the one
the API declared, so a formatter, parser or error formatter declared for
that name applies too. The lookup runs only when the requested format is
not declared, so the common path is unchanged.
Grape::Formatter::Md, an empty subclass of Markdown, registers the same
rendering under :md. Each class names its own format in the
InvalidFormatter message, derived from the class name the way the
registry derives its key.
Neither is a default content type; an API opts in with
`content_type :md, 'text/markdown; charset=utf-8'`. Errors keep the text
error formatter, and a text/markdown request body passes through
unparsed.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,7 @@
9
9
*[#2960](https://github.com/ruby-grape/grape/pull/2960): Lint every response of an API once under `lint!`, including the router's 404 and mounted Rack apps, instead of inside each endpoint's stack - [@ericproulx](https://github.com/ericproulx).
10
10
*[#2966](https://github.com/ruby-grape/grape/pull/2966): Require Ruby 3.3.1, the first 3.3 release that parses anonymous argument forwarding from inside a block - [@ericproulx](https://github.com/ericproulx).
11
11
*[#3008](https://github.com/ruby-grape/grape/pull/3008): Add the grape-on-rack app as a live-server memory profiling harness - [@ericproulx](https://github.com/ericproulx).
12
+
*[#3025](https://github.com/ruby-grape/grape/pull/3025): Add `:md` and `:markdown` formatters that render an object's `to_markdown`, for APIs that declare a `text/markdown` content type - [@ericproulx](https://github.com/ericproulx).
12
13
*[#3029](https://github.com/ruby-grape/grape/pull/3029): Add a built-in `:jsonl` (JSON Lines) format that streams an Enumerator body - [@ericproulx](https://github.com/ericproulx).
Copy file name to clipboardExpand all lines: README.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3351,6 +3351,30 @@ Built-in formatters are the following.
3351
3351
*`:serializable_hash`: use object's `serializable_hash` when available, otherwise fallback to `:json`
3352
3352
*`:binary`: data will be returned "as is"
3353
3353
*`:jsonl`: one line per element of an Array or Enumerator, otherwise one line, each rendered as `:json` renders it. Not a default content type, see [JSON Lines](#json-lines)
3354
+
*`:md`, `:markdown`: use object's `to_markdown` when available, return a String "as is", otherwise raise `Grape::Exceptions::InvalidFormatter`. Not default content types, see [Serving Markdown](#serving-markdown)
3355
+
3356
+
#### Serving Markdown
3357
+
3358
+
`:md` and `:markdown` are not among the default content types. Declare either one to answer clients that ask for Markdown, such as LLM agents sending `Accept: text/markdown`.
3359
+
3360
+
```ruby
3361
+
classAPI < Grape::API
3362
+
content_type :json, 'application/json'
3363
+
content_type :md, 'text/markdown; charset=utf-8'
3364
+
default_format :json
3365
+
3366
+
get '/pages/:id'do
3367
+
Page.find(params[:id]) # renders with Page#to_markdown
3368
+
end
3369
+
end
3370
+
```
3371
+
3372
+
*`GET /pages/1.md`, `GET /pages/1.markdown`, `GET /pages/1?format=md`, `?format=markdown` and `GET /pages/1` with an `Accept: text/markdown` header answer `text/markdown`.
3373
+
*`:md` and `:markdown` name the same format: declaring either one answers both extensions, under the name declared. A request for `.markdown` on the API above negotiates `:md`, so a `formatter :md` of your own renders it too.
3374
+
*`to_markdown` is the method Rails' `render markdown:` calls (Rails 8.1), so an object written for a Rails controller renders the same way in Grape.
3375
+
* Grape does not convert anything else to Markdown. When the client negotiated Markdown, a body without `to_markdown` that is not a String answers in the default format instead; when the API pins `format :md`, it answers `500`.
3376
+
*[RFC 7763](https://www.rfc-editor.org/rfc/rfc7763) makes `charset` a required parameter of `text/markdown`, hence `charset=utf-8`.
3377
+
* Errors are rendered as plain text, which is valid Markdown.
Copy file name to clipboardExpand all lines: UPGRADING.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,15 @@ Grape now renders, parses and reports errors in JSON Lines under `:jsonl` ([#302
13
13
14
14
A formatter, parser or error formatter you register for `:jsonl` with `formatter`, `parser` or `error_formatter` still takes precedence.
15
15
16
+
#### Formats named `:md` and `:markdown` have a built-in formatter
17
+
18
+
`Grape::Formatter::Md` renders the `:md` format and `Grape::Formatter::Markdown` the `:markdown` one ([#3025](https://github.com/ruby-grape/grape/pull/3025)). Neither is a default content type, so only an API that declares one is affected. See [Serving Markdown](README.md#serving-markdown).
19
+
20
+
* An API that declared `content_type :md` (or `:markdown`) without a matching `formatter` used to send its bodies unchanged. A String still is. An object answering `to_markdown` is now rendered with it, and any other object raises `Grape::Exceptions::InvalidFormatter`: it answers in the default format when the client negotiated Markdown, and `500` otherwise.
21
+
*`:md` and `:markdown` now name the same format. An API that declared one of them used to answer the other extension (and `?format=`) in its default format, or `406`; it now answers it in Markdown, under the name it declared.
22
+
* A `formatter :md` or `formatter :markdown` of your own still takes precedence.
23
+
* A formatter class of your own named `Md` or `Markdown` that inherits `Grape::Formatter::Base` now replaces Grape's globally, with a warning that `md` or `markdown` is already registered. Rename it, and declare it with `formatter :md, YourFormatter` (or `:markdown`) in the APIs that use it.
24
+
16
25
#### `logger` applies to the API it is set on
17
26
18
27
`logger` kept the logger in settings shared by the whole process, so setting it on one API replaced it on every other: two APIs that each set a logger both logged to whichever was set last.
0 commit comments