Skip to content

Commit 11f9a0c

Browse files
authored
Use Yojson.Safe instead of Yojson.Basic (#248)
1 parent 29eb6f1 commit 11f9a0c

File tree

9 files changed

+21
-23
lines changed

9 files changed

+21
-23
lines changed

CHANGES.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
- Add a package `opium-graphql` to easily create GraphQL server with Opium (#235)
99
- Add a function `App.run_multicore` that uses pre-forking and spawns multiple processes that will handle incoming requests (#239)
1010

11-
## Changed
12-
13-
- `Request.of_json/to_json` and `Response.of_json/to_json` now take a `Yojson.Basic.t` instead of a `Yojson.Safe.t`. (#235)
14-
1511
## Fixed
1612

1713
- Fix reading cookie values when multiple cookies are present in `Cookie` header (#246)

example/json_response/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dune exec example/json_response/main.exe
77
This is an example of a JSON response.
88

99
The server offers an endpoint `/` that serves a single JSON object.
10-
The JSON object is internally represented using `Yojson.Basic.t`,
10+
The JSON object is internally represented using `Yojson.Safe.t`,
1111
and populated with values from the `Sys` module.
1212
The function `Response.of_json` is used to serialize the JSON object and sets the correct content-type.
1313

example/json_response/main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
open Opium
22

33
let sys_json _req =
4-
let json : Yojson.Basic.t =
4+
let json : Yojson.Safe.t =
55
`Assoc [ "os-type", `String Sys.os_type; "ocaml-version", `String Sys.ocaml_version ]
66
in
77
Response.of_json json |> Lwt.return

opium-graphql/src/opium_graphql.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ end
9191

9292
module Schema = Graphql_lwt.Schema
9393

94+
let basic_to_safe json = json |> Yojson.Basic.to_string |> Yojson.Safe.from_string
95+
9496
let execute_query ctx schema variables operation_name query =
9597
match Graphql_parser.parse query with
9698
| Ok doc -> Schema.execute schema ctx ?variables ?operation_name doc
@@ -105,12 +107,12 @@ let execute_request schema ctx req =
105107
| Ok (query, variables, operation_name) ->
106108
let+ result = execute_query ctx schema variables operation_name query in
107109
(match result with
108-
| Ok (`Response data) -> Opium.Response.of_json ~status:`OK data
110+
| Ok (`Response data) -> data |> basic_to_safe |> Opium.Response.of_json ~status:`OK
109111
| Ok (`Stream stream) ->
110112
Graphql_lwt.Schema.Io.Stream.close stream;
111113
let body = "Subscriptions are only supported via websocket transport" in
112114
Opium.Response.of_plain_text ~status:`Bad_request body
113-
| Error err -> Opium.Response.of_json ~status:`Bad_request err)
115+
| Error err -> err |> basic_to_safe |> Opium.Response.of_json ~status:`Bad_request)
114116
;;
115117

116118
let make_handler

opium-graphql/test/request_test.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let suite =
4646
, fun () ->
4747
let body =
4848
Opium.Body.of_string
49-
(Yojson.Basic.to_string (`Assoc [ "query", `String "{ hello }" ]))
49+
(Yojson.Safe.to_string (`Assoc [ "query", `String "{ hello }" ]))
5050
in
5151
test_case
5252
~req:(Opium.Request.make ~headers:json_content_type ~body default_uri `POST)
@@ -81,7 +81,7 @@ let suite =
8181
, fun () ->
8282
let body =
8383
Opium.Body.of_string
84-
(Yojson.Basic.to_string
84+
(Yojson.Safe.to_string
8585
(`Assoc
8686
[ ( "query"
8787
, `String
@@ -98,7 +98,7 @@ let suite =
9898
, fun () ->
9999
let body =
100100
Opium.Body.of_string
101-
(Yojson.Basic.to_string
101+
(Yojson.Safe.to_string
102102
(`Assoc
103103
[ ( "query"
104104
, `String
@@ -121,7 +121,7 @@ let suite =
121121
, fun () ->
122122
let body =
123123
Opium.Body.of_string
124-
(Yojson.Basic.to_string
124+
(Yojson.Safe.to_string
125125
(`Assoc
126126
[ "query", `String "query A($name: String!) { hello(name: $name) }"
127127
; "variables", `Assoc [ "name", `String "world" ]
@@ -135,7 +135,7 @@ let suite =
135135
, fun () ->
136136
let body =
137137
Opium.Body.of_string
138-
(Yojson.Basic.to_string
138+
(Yojson.Safe.to_string
139139
(`Assoc
140140
[ "query", `String "query A($name: String!) { hello(name: $name) }" ]))
141141
in

opium/src/request.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let of_json ?version ?headers ?env ~body target meth =
2626
?env
2727
target
2828
meth
29-
(body |> Yojson.Basic.to_string)
29+
(body |> Yojson.Safe.to_string)
3030
;;
3131

3232
let of_urlencoded ?version ?headers ?env ~body target meth =
@@ -43,7 +43,7 @@ let of_urlencoded ?version ?headers ?env ~body target meth =
4343
let to_json_exn t =
4444
let open Lwt.Syntax in
4545
let* body = t.body |> Body.copy |> Body.to_string in
46-
Lwt.return @@ Yojson.Basic.from_string body
46+
Lwt.return @@ Yojson.Safe.from_string body
4747
;;
4848

4949
let to_json t =

opium/src/request.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ val of_json
157157
: ?version:Version.t
158158
-> ?headers:Headers.t
159159
-> ?env:Context.t
160-
-> body:Yojson.Basic.t
160+
-> body:Yojson.Safe.t
161161
-> string
162162
-> Method.t
163163
-> t
@@ -227,15 +227,15 @@ val to_plain_text : t -> string Lwt.t
227227
[body] will be:
228228
229229
{[ `Assoc [ "Hello", `String "World" ] ]} *)
230-
val to_json : t -> Yojson.Basic.t option Lwt.t
230+
val to_json : t -> Yojson.Safe.t option Lwt.t
231231

232232
(** {3 [to_json_exn]} *)
233233

234234
(** [to_json_exn t] parses the body of the request [t] as a JSON structure.
235235
236236
If the body of the request cannot be parsed as a JSON structure, an [Invalid_argument]
237237
exception is raised. Use {!to_json} to return an option instead. *)
238-
val to_json_exn : t -> Yojson.Basic.t Lwt.t
238+
val to_json_exn : t -> Yojson.Safe.t Lwt.t
239239

240240
(** {3 [to_urlencoded]} *)
241241

opium/src/response.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ let of_json ?version ?status ?reason ?headers ?env body =
158158
?reason
159159
?headers
160160
?env
161-
(body |> Yojson.Basic.to_string)
161+
(body |> Yojson.Safe.to_string)
162162
;;
163163

164164
let of_file ?version ?reason ?headers ?env ?mime fname =
@@ -196,7 +196,7 @@ let set_cache_control s t = add_header_or_replace ("Cache-Control", s) t
196196
let to_json_exn t =
197197
let open Lwt.Syntax in
198198
let* body = t.body |> Body.copy |> Body.to_string in
199-
Lwt.return @@ Yojson.Basic.from_string body
199+
Lwt.return @@ Yojson.Safe.from_string body
200200
;;
201201

202202
let to_json t =

opium/src/response.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ val of_json
8787
-> ?reason:string
8888
-> ?headers:Headers.t
8989
-> ?env:Context.t
90-
-> Yojson.Basic.t
90+
-> Yojson.Safe.t
9191
-> t
9292

9393
(** {3 [of_html]} *)
@@ -303,15 +303,15 @@ val redirect_to
303303
[body] will be:
304304
305305
{[ `Assoc [ "Hello", `String "World" ] ]} *)
306-
val to_json : t -> Yojson.Basic.t option Lwt.t
306+
val to_json : t -> Yojson.Safe.t option Lwt.t
307307

308308
(** {3 [to_json_exn]} *)
309309

310310
(** [to_json_exn t] parses the body of the response [t] as a JSON structure.
311311
312312
If the body of the response cannot be parsed as a JSON structure, an
313313
[Invalid_argument] exception is raised. Use {!to_json} to return an option instead. *)
314-
val to_json_exn : t -> Yojson.Basic.t Lwt.t
314+
val to_json_exn : t -> Yojson.Safe.t Lwt.t
315315

316316
(** {3 [to_plain_text]} *)
317317

0 commit comments

Comments
 (0)