Skip to content

Commit 4d4fda6

Browse files
committed
Expose max-aliases and allow-recursive options to parse-string
`max-aliases` exposes `setMaxAliasesForCollections` `allow-recursive` exposes `setAllowRecursiveKeys`
1 parent f385f74 commit 4d4fda6

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/clojure/clj_yaml/core.clj

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns clj-yaml.core
22
(:require [flatland.ordered.map :refer (ordered-map)]
33
[flatland.ordered.set :refer (ordered-set)])
4-
(:import (org.yaml.snakeyaml Yaml DumperOptions DumperOptions$FlowStyle)
4+
(:import (org.yaml.snakeyaml Yaml DumperOptions DumperOptions$FlowStyle LoaderOptions)
55
(org.yaml.snakeyaml.constructor Constructor SafeConstructor BaseConstructor)
66
(org.yaml.snakeyaml.representer Representer)
77
(org.yaml.snakeyaml.error Mark)
@@ -27,17 +27,37 @@
2727
(doto (default-dumper-options)
2828
(.setDefaultFlowStyle (flow-styles flow-style))))
2929

30+
(defn ^LoaderOptions default-loader-options
31+
[]
32+
(LoaderOptions.))
33+
34+
(defn ^LoaderOptions make-loader-options
35+
[& {:keys [max-aliases-for-collections allow-recursive-keys]}]
36+
(let [loader (default-loader-options)]
37+
(when max-aliases-for-collections
38+
(.setMaxAliasesForCollections loader max-aliases-for-collections))
39+
(when allow-recursive-keys
40+
(.setAllowRecursiveKeys loader allow-recursive-keys))
41+
loader))
42+
3043
(defn ^Yaml make-yaml
3144
"Make a yaml encoder/decoder with some given options."
32-
[& {:keys [dumper-options unsafe mark]}]
33-
(let [^BaseConstructor constructor
34-
(if unsafe (Constructor.)
35-
(if mark (MarkedConstructor.) (SafeConstructor.)))
45+
[& {:keys [dumper-options unsafe mark max-aliases-for-collections allow-recursive-keys]}]
46+
(let [loader (make-loader-options :max-aliases-for-collections max-aliases-for-collections
47+
:allow-recursive-keys allow-recursive-keys)
48+
^BaseConstructor constructor
49+
(if unsafe (Constructor. loader)
50+
(if mark
51+
;; construct2ndStep isn't implemented by MarkedConstructor,
52+
;; causing an exception to be thrown before loader options are
53+
;; used
54+
(MarkedConstructor.)
55+
(SafeConstructor. loader)))
3656
;; TODO: unsafe marked constructor
3757
dumper (if dumper-options
3858
(make-dumper-options :flow-style (:flow-style dumper-options))
3959
(default-dumper-options))]
40-
(Yaml. constructor (Representer.) dumper)))
60+
(Yaml. constructor (Representer.) dumper loader)))
4161

4262
(defrecord Marked
4363
[start end unmark])
@@ -126,5 +146,9 @@
126146
(encode data)))
127147

128148
(defn parse-string
129-
[^String string & {:keys [unsafe mark keywords] :or {keywords true}}]
130-
(decode (.load (make-yaml :unsafe unsafe :mark mark) string) keywords))
149+
[^String string & {:keys [unsafe mark keywords max-aliases-for-collections allow-recursive-keys] :or {keywords true}}]
150+
(decode (.load (make-yaml :unsafe unsafe
151+
:mark mark
152+
:max-aliases-for-collections max-aliases-for-collections
153+
:allow-recursive-keys allow-recursive-keys)
154+
string) keywords))

test/clj_yaml/core_test.clj

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
(:require [clojure.test :refer (deftest testing is)]
33
[clojure.string :as string]
44
[clj-yaml.core :refer [parse-string unmark generate-string]])
5-
(:import [java.util Date]))
5+
(:import [java.util Date]
6+
(org.yaml.snakeyaml.error YAMLException)))
67

78
(def nested-hash-yaml
89
"root:\n childa: a\n childb: \n grandchild: \n greatgrandchild: bar\n")
@@ -206,5 +207,16 @@ the-bin: !!binary 0101")
206207
(cons "a: &a [\"a\",\"a\"]")
207208
(string/join "\n")))
208209

209-
(deftest too-many-aliases-works
210-
(is (parse-string too-many-aliases)))
210+
(deftest max-aliases-for-collections-works
211+
(is (thrown? YAMLException (parse-string too-many-aliases)))
212+
(is (parse-string too-many-aliases :max-aliases-for-collections 51)))
213+
214+
(def recursive-yaml "
215+
---
216+
&A
217+
- *A: *A
218+
")
219+
220+
(deftest allow-recursive-works
221+
(is (thrown? YAMLException (parse-string recursive-yaml)))
222+
(is (parse-string recursive-yaml :allow-recursive-keys true)))

0 commit comments

Comments
 (0)