Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
solita-antti-mottonen committed Oct 26, 2023
1 parent 5b307a9 commit 7e2c178
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 7 additions & 3 deletions etp-backend/src/main/clj/solita/etp/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.ring.middleware.parameters :as parameters]
[reitit.spec :as rs]
[reitit.swagger :as swagger]
[schema.coerce]
[reitit.swagger-ui :as swagger-ui]
[ring.middleware.cookies :as cookies]
[schema.core]
Expand Down Expand Up @@ -178,13 +178,17 @@
:handler (openapi/create-openapi-handler)}}]
(openapi-id "Palveluväylä" palveluvayla/routes)]])

(def default-string-coercion-options-with-project-specific-ones
"Add more schemas that support coercion to default configuration in addition to those supported by schema coercion out of the box"
(assoc-in reitit.coercion.schema/default-options [:matchers :string :default] (some-fn (some-fn {schema.common/AcceptLanguage (schema.coerce/safe schema.common/parse-accept-language)})
(get-in reitit.coercion.schema/default-options [:matchers :string :default]))))

(def route-opts
{;; Uncomment line below to see diffs of requests in middleware chain
;;:reitit.middleware/transform dev/print-request-diffs
:exception pretty/exception
:validate rs/validate
:data {:coercion (reitit.coercion.schema/create (assoc-in reitit.coercion.schema/default-options [:matchers :string :default] (some-fn (some-fn {schema.common/AcceptLanguage schema.common/parse-locales})
(get-in reitit.coercion.schema/default-options [:matchers :string :default]))))
:data {:coercion (reitit.coercion.schema/create default-string-coercion-options-with-project-specific-ones)
:muuntaja m/instance
:middleware [openapi/openapi-feature
parameters/parameters-middleware
Expand Down
16 changes: 11 additions & 5 deletions etp-backend/src/main/clj/solita/etp/schema/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,16 @@

(def Language (schema/enum "fi" "sv"))

(schema/defschema WeightedLocale [(schema/one (schema/constrained schema/Str #(re-matches #"(?i)([*]|[a-z]{2,3})" %)) "lang") (schema/one (schema/constrained schema/Num #(and (>= % 0) (<= % 1))) "weight")])
(schema/defschema AcceptLanguage [WeightedLocale])

(defn parse-lang [s]
(schema/defschema WeightedLocale
"A single locale with weight for Accept-Language header"
[(schema/one (schema/constrained schema/Str #(re-matches #"(?i)([*]|[a-z]{2,3})" %)) "lang") (schema/one (schema/constrained schema/Num #(and (>= % 0) (<= % 1))) "weight")])
(schema/defschema AcceptLanguage
"Define a schema for Accept-Language header. The header can contain multiple locales with preferred weights, for example Accept-Language: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5"
[WeightedLocale])

(defn parse-lang
"Parse a language from language tag i.e. drop subtags such as region separated by -"
[s]
(-> s (str/split #"-") first))

(defn parse-locale [s]
Expand All @@ -210,5 +216,5 @@
1 [(parse-lang s) 1.0]
2 [(-> parts first parse-lang) (Double/parseDouble (second parts))])))

(defn parse-locales [s]
(defn parse-accept-language [s]
(map parse-locale (str/split s #",")))

0 comments on commit 7e2c178

Please sign in to comment.