Skip to content

Commit 8f5213d

Browse files
committed
Fix: Correctly encode/decode maps
1 parent ac643b5 commit 8f5213d

20 files changed

Lines changed: 425 additions & 402 deletions

modules/openapi-generator/src/main/resources/ocaml/json.mustache

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,16 @@ let of_int64 x = `Intlit (Int64.to_string x)
5353
let of_list_of of_f l = `List (Stdlib.List.map of_f l)
5454

5555
let of_map_of of_f l = `Assoc (Stdlib.List.map (fun (k, v) -> (k, of_f v)) l)
56+
57+
let to_map_of of_f json =
58+
match json with
59+
| `Assoc l ->
60+
Stdlib.List.fold_right
61+
(fun (k, json) acc ->
62+
match (of_f json, acc) with
63+
| Stdlib.Result.Ok parsed_v, Stdlib.Result.Ok tl ->
64+
Stdlib.Result.Ok ((k, parsed_v) :: tl)
65+
| Stdlib.Result.Error e, _ -> Stdlib.Result.Error e
66+
| _, Stdlib.Result.Error e -> Stdlib.Result.Error e)
67+
l (Stdlib.Result.Ok [])
68+
| _ -> Stdlib.Result.Error "Expected"

modules/openapi-generator/src/main/resources/ocaml/model-record.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type t = {
2828
{{^required}} option [@default None]{{/required}}
2929
{{/isContainer}}
3030
{{#isArray}}{{^required}} [@default []]{{/required}}{{/isArray}}
31-
{{#isMap}}{{^required}} [@default []]{{/required}}{{/isMap}}
31+
{{#isMap}}{{^required}} [@default []] [@to_yojson JsonSupport.of_map_of [%to_yojson: {{{items.datatypeWithEnum}}}]] [@of_yojson JsonSupport.to_map_of [%of_yojson: {{{items.datatypeWithEnum}}}]] {{/required}}{{/isMap}}
3232
; [@key "{{{baseName}}}"]
3333
{{/isEnum}}
3434
{{/vars}}

modules/openapi-generator/src/main/resources/ocaml/to_json.mustache

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,32 @@
88
(JsonSupport.of_map_of {{> to_json}})
99
{{/items}}
1010
{{/isMap}}
11-
{{#isString}}JsonSupport.of_string{{/isString}}
12-
{{#isLong}}JsonSupport.of_int64{{/isLong}}
13-
{{#isInteger}}JsonSupport.of_int32{{/isInteger}}
14-
{{#isFloat}}JsonSupport.of_float{{/isFloat}}
15-
{{#isNumber}}JsonSupport.of_float{{/isNumber}}
16-
{{#isDouble}}JsonSupport.of_float{{/isDouble}}
17-
{{#isBoolean}}JsonSupport.of_bool{{/isBoolean}}
18-
{{^isEnum}}
19-
{{#isModel}}
20-
{{#vendorExtensions.x-model-module}}
21-
{{{vendorExtensions.x-model-module}}}.to_yojson
22-
{{/vendorExtensions.x-model-module}}
23-
{{^vendorExtensions.x-model-module}}
24-
{{{baseType}}}.to_yojson
25-
{{/vendorExtensions.x-model-module}}
11+
{{^isContainer}}
12+
{{#isString}}JsonSupport.of_string{{/isString}}
13+
{{#isLong}}JsonSupport.of_int64{{/isLong}}
14+
{{#isInteger}}JsonSupport.of_int32{{/isInteger}}
15+
{{#isFloat}}JsonSupport.of_float{{/isFloat}}
16+
{{#isNumber}}JsonSupport.of_float{{/isNumber}}
17+
{{#isDouble}}JsonSupport.of_float{{/isDouble}}
18+
{{#isBoolean}}JsonSupport.of_bool{{/isBoolean}}
19+
{{^isEnum}}
20+
{{#isModel}}
21+
{{#vendorExtensions.x-model-module}}
22+
{{{vendorExtensions.x-model-module}}}.to_yojson
23+
{{/vendorExtensions.x-model-module}}
24+
{{^vendorExtensions.x-model-module}}
25+
{{{baseType}}}.to_yojson
26+
{{/vendorExtensions.x-model-module}}
27+
{{/isModel}}
28+
{{/isEnum}}
29+
{{^isModel}}
30+
{{^isContainer}}
31+
{{#isEnum}}
32+
Enums.{{{datatypeWithEnum}}}_to_yojson
33+
{{/isEnum}}
34+
{{/isContainer}}
2635
{{/isModel}}
27-
{{/isEnum}}
28-
{{^isModel}}
29-
{{^isContainer}}
30-
{{#isEnum}}
31-
Enums.{{{datatypeWithEnum}}}_to_yojson
32-
{{/isEnum}}
33-
{{/isContainer}}
34-
{{/isModel}}
35-
{{#isBinary}}JsonSupport.of_string{{/isBinary}}
36-
{{#isAnyType}}(fun x -> x){{/isAnyType}}
36+
{{#isBinary}}JsonSupport.of_string{{/isBinary}}
37+
{{#isAnyType}}(fun x -> x){{/isAnyType}}
38+
{{#isFreeFormObject}}(fun x -> x){{/isFreeFormObject}}
39+
{{/isContainer}}

samples/client/petstore/ocaml-fake-petstore/src/apis/another_fake_api.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ let call_123_test_special_tags ~client_t =
1111
let headers = Request.default_headers in
1212
let body = Request.
1313

14-
write_as_json_body
15-
16-
17-
18-
19-
20-
21-
Client.to_yojson
22-
23-
14+
write_as_json_body
15+
16+
17+
18+
19+
20+
21+
Client.to_yojson
22+
23+
24+
2425
client_t
2526
in
2627
Cohttp_lwt_unix.Client.call `PATCH uri ~headers ~body >>= fun (resp, body) ->

0 commit comments

Comments
 (0)