Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Migration Guide to 2.0.0

Ambrose Bonnaire-Sergeant edited this pageApr 23, 2024 ·16 revisions

NOTE: Compojure-api 2.0.0 has not been released yet. For a preview, you can try the Compojure-api 2.0.0 alphas. What was supposed to become Compojure-api 1.2.0 will be released as 2.0.0.

About

Compojure-api 2.0.0 has the following breaking changes:

  • Compojure-api 2.0.0 requires Java 1.8 or later (because of Muuntaja).
  • We now use Muuntaja instead of ring-middleware-format;:format options need migrating
  • YAML and msgpack support is not enabled by default
  • Request and response coercion now depends on the content type.
  • swagger-ui andswagger-docs now take options map instead of varargs.
  • middleware has been disabled; useroute-middleware instead

The details are below.

NOTE: If you're using version1.x, please first upgrade to 1.1.13 (the last pre-2.0.0 version) before upgrading to 2.0.0. See alsoMigration Guide to 1.1.11. If you're using pre-1.0.0 version, seethe migration guide to 1.0.0.

Muuntaja

We now useMuuntaja instead ofring-middleware-format for format negotiation, encoding, and decoding. In benchmarks, Muuntaja is much faster than ring-middleware-format.

  • The api option:format has been removed. Use:formats instead. It should be either a Muuntaja instance, Muuntaja options map, ornil to disable Muuntaja.
  • Seethe instructions for configuring Muuntaja.
    • If you haven't configured:format, you can ignore this.
  • By default, support forapplication/yaml &application/msgpack have been dropped (smaller core). If you want to use them, do this:
    • add a dependency to[metosin/muuntaja-yaml "0.6.0-alpha1"] &/[metosin/muuntaja-msgpack "0.6.0-alpha1"]
    • configure yourapi to serve those formats (coercion is pre-set for both):
(require '[muuntaja.core:as m])(require '[muuntaja.format.yaml])(require '[muuntaja.format.msgpack])(api  {:formats (-> m/default-options)                (m/install muuntaja.format.yaml/format)                (m/install muuntaja.format.msgpack/format))}  ...)

Request and response coercion

Request and response coercion now depends on the format negotiated by Muuntaja. The old default was to coerce all requests and responses withjson-coercion-matcher. The new defaults are as follows:

FormatRequestResponse
application/ednvalidatevalidate
application/transit+jsonvalidatevalidate
application/transit+msgpackvalidatevalidate
application/jsonjson-coercion-matchervalidate
application/msgpackjson-coercion-matchervalidate
application/x-yamljson-coercion-matchervalidate

How does this affect my routes?

Before, this was ok:

(api  (GET"/tags" []:return {:tags #{s/Keyword}}    (ok {:tags #{"kikka","kakka", kukka"}})))

With 2.0.0, it will fail as the return value is not coerced, just validated by default. One can set back the JSON coercion either as default for all formats or for certain formats only:

(require '[compojure.api.coercion.schema:as cs]);; for all formats(defjson-coerce-all-responses  (cs/create-coercion    (assoc-in cs/default-options [:response:default] cs/json-coercion-matcher)));; just for application/json(defjson-coerce-responses  (cs/create-coercion    (assoc-in cs/default-options [:response:formats"application/json"] cs/json-coercion-matcher)))(api  {:coercion json-coerce-all-responses}  (GET"/tags" []:return {:tags #{s/Keyword}}    (ok {:tags #{"kikka","kakka", kukka"}})))
  • If you have customcoercions, they should continue to work as before.
  • default-coercion-matchers has been replaced bydefault-coercion-options.

middleware androute-middleware

middleware has been disabled because it dangerously applied the middleware even to requests that didn't match the contained routes. Replace it withcompojure.api.core/route-middleware, which only applies middleware when the request is matched against the contained routes.

Ifmiddleware is needed for backwards-compatibility reasons, set JVM property-Dcompojure.api.core.allow-dangerous-middleware=true.

Still having problems?

If you need help, find us on#ring-swagger in Clojurians Slack (join) oropen an issue. If this guide is missing something relevant, feel free to update it. All feedback welcome.

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp