Skip to content

Commit

Permalink
[OCaml] Code generation fixes (#12395)
Browse files Browse the repository at this point in the history
* [ocaml] Open Lwt.Infix rather than Lwt

The Lwt module has functions that might shadow parameters, and all the
functions we use from Lwt are in Lwt.Infix too.

    File "src/apis/image_api.ml", line 13, characters 69-72:
    13 |     let uri = Request.maybe_add_query_param uri "all" string_of_bool all in
                                                                              ^^^
    Error: This expression has type 'a t list -> 'a list t
           but an expression was expected of type bool option

* [ocaml] update petstore samples
  • Loading branch information
MisterDA authored May 26, 2022
1 parent 54dca39 commit 5cce050
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{{#operations}}
{{#operation}}
let {{{operationId}}} {{^hasParams}}(){{/hasParams}}{{#allParams}}{{> to_param}}{{^-last}} {{/-last}}{{#-last}}{{^required}} (){{/required}}{{/-last}}{{/allParams}} =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "{{{path}}}" in
let headers = Request.default_headers in
{{#hasAuthMethods}}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/ocaml/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.1-SNAPSHOT
6.0.0-SNAPSHOT
1 change: 0 additions & 1 deletion samples/client/petstore/ocaml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ dune build
## Getting Started

TODO

16 changes: 8 additions & 8 deletions samples/client/petstore/ocaml/src/apis/pet_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
*)

let add_pet ~pet_t =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/pet" in
let headers = Request.default_headers in
let body = Request.write_as_json_body Pet.to_yojson pet_t in
Cohttp_lwt_unix.Client.call `POST uri ~headers ~body >>= fun (resp, body) ->
Request.read_json_body_as (JsonSupport.unwrap Pet.of_yojson) resp body

let delete_pet ~pet_id ?api_key () =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/pet/{petId}" in
let headers = Request.default_headers in
let headers = Request.maybe_add_header headers "api_key" (fun x -> x) api_key in
Expand All @@ -23,23 +23,23 @@ let delete_pet ~pet_id ?api_key () =
Request.handle_unit_response resp

let find_pets_by_status ~status =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/pet/findByStatus" in
let headers = Request.default_headers in
let uri = Request.add_query_param_list uri "status" (List.map Enums.show_pet_status) status in
Cohttp_lwt_unix.Client.call `GET uri ~headers >>= fun (resp, body) ->
Request.read_json_body_as_list_of (JsonSupport.unwrap Pet.of_yojson) resp body

let find_pets_by_tags ~tags =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/pet/findByTags" in
let headers = Request.default_headers in
let uri = Request.add_query_param_list uri "tags" (List.map (fun x -> x)) tags in
Cohttp_lwt_unix.Client.call `GET uri ~headers >>= fun (resp, body) ->
Request.read_json_body_as_list_of (JsonSupport.unwrap Pet.of_yojson) resp body

let get_pet_by_id ~pet_id =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/pet/{petId}" in
let headers = Request.default_headers in
let headers = Cohttp.Header.add headers "api_key" Request.api_key in
Expand All @@ -48,15 +48,15 @@ let get_pet_by_id ~pet_id =
Request.read_json_body_as (JsonSupport.unwrap Pet.of_yojson) resp body

let update_pet ~pet_t =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/pet" in
let headers = Request.default_headers in
let body = Request.write_as_json_body Pet.to_yojson pet_t in
Cohttp_lwt_unix.Client.call `PUT uri ~headers ~body >>= fun (resp, body) ->
Request.read_json_body_as (JsonSupport.unwrap Pet.of_yojson) resp body

let update_pet_with_form ~pet_id ?name ?status () =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/pet/{petId}" in
let headers = Request.default_headers in
let uri = Request.replace_path_param uri "petId" Int64.to_string pet_id in
Expand All @@ -68,7 +68,7 @@ let update_pet_with_form ~pet_id ?name ?status () =
Request.handle_unit_response resp

let upload_file ~pet_id ?additional_metadata ?file () =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/pet/{petId}/uploadImage" in
let headers = Request.default_headers in
let uri = Request.replace_path_param uri "petId" Int64.to_string pet_id in
Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/ocaml/src/apis/store_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
*)

let delete_order ~order_id =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/store/order/{orderId}" in
let headers = Request.default_headers in
let uri = Request.replace_path_param uri "orderId" (fun x -> x) order_id in
Cohttp_lwt_unix.Client.call `DELETE uri ~headers >>= fun (resp, body) ->
Request.handle_unit_response resp

let get_inventory () =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/store/inventory" in
let headers = Request.default_headers in
let headers = Cohttp.Header.add headers "api_key" Request.api_key in
Cohttp_lwt_unix.Client.call `GET uri ~headers >>= fun (resp, body) ->
Request.read_json_body_as_map_of (JsonSupport.to_int32) resp body

let get_order_by_id ~order_id =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/store/order/{orderId}" in
let headers = Request.default_headers in
let uri = Request.replace_path_param uri "orderId" Int64.to_string order_id in
Cohttp_lwt_unix.Client.call `GET uri ~headers >>= fun (resp, body) ->
Request.read_json_body_as (JsonSupport.unwrap Order.of_yojson) resp body

let place_order ~order_t =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/store/order" in
let headers = Request.default_headers in
let body = Request.write_as_json_body Order.to_yojson order_t in
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/ocaml/src/apis/user_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*)

let create_user ~user_t =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/user" in
let headers = Request.default_headers in
let headers = Cohttp.Header.add headers "api_key" Request.api_key in
Expand All @@ -15,7 +15,7 @@ let create_user ~user_t =
Request.handle_unit_response resp

let create_users_with_array_input ~user =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/user/createWithArray" in
let headers = Request.default_headers in
let headers = Cohttp.Header.add headers "api_key" Request.api_key in
Expand All @@ -24,7 +24,7 @@ let create_users_with_array_input ~user =
Request.handle_unit_response resp

let create_users_with_list_input ~user =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/user/createWithList" in
let headers = Request.default_headers in
let headers = Cohttp.Header.add headers "api_key" Request.api_key in
Expand All @@ -33,7 +33,7 @@ let create_users_with_list_input ~user =
Request.handle_unit_response resp

let delete_user ~username =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/user/{username}" in
let headers = Request.default_headers in
let headers = Cohttp.Header.add headers "api_key" Request.api_key in
Expand All @@ -42,15 +42,15 @@ let delete_user ~username =
Request.handle_unit_response resp

let get_user_by_name ~username =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/user/{username}" in
let headers = Request.default_headers in
let uri = Request.replace_path_param uri "username" (fun x -> x) username in
Cohttp_lwt_unix.Client.call `GET uri ~headers >>= fun (resp, body) ->
Request.read_json_body_as (JsonSupport.unwrap User.of_yojson) resp body

let login_user ~username ~password =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/user/login" in
let headers = Request.default_headers in
let uri = Request.add_query_param uri "username" (fun x -> x) username in
Expand All @@ -59,15 +59,15 @@ let login_user ~username ~password =
Request.read_json_body_as (JsonSupport.to_string) resp body

let logout_user () =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/user/logout" in
let headers = Request.default_headers in
let headers = Cohttp.Header.add headers "api_key" Request.api_key in
Cohttp_lwt_unix.Client.call `GET uri ~headers >>= fun (resp, body) ->
Request.handle_unit_response resp

let update_user ~username ~user_t =
let open Lwt in
let open Lwt.Infix in
let uri = Request.build_uri "/user/{username}" in
let headers = Request.default_headers in
let headers = Cohttp.Header.add headers "api_key" Request.api_key in
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/ocaml/src/support/request.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ let handle_response resp on_success_handler =
| #Cohttp.Code.success_status -> on_success_handler ()
| s -> failwith ("Server responded with status " ^ Cohttp.Code.(reason_phrase_of_code (code_of_status s)))

let handle_unit_response resp = handle_response resp (fun () -> Lwt.return ())
let handle_unit_response resp = handle_response resp (fun () -> Lwt.return ())

let read_json_body resp body =
handle_response resp (fun () ->
(Lwt.(Cohttp_lwt.Body.to_string body >|= Yojson.Safe.from_string)))
Expand Down

0 comments on commit 5cce050

Please sign in to comment.