Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/samples-ocaml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ on:
push:
paths:
- 'samples/client/petstore/ocaml/**'
- 'samples/client/petstore/ocaml-fake-petstore/**'
pull_request:
paths:
- 'samples/client/petstore/ocaml/**'
- 'samples/client/petstore/ocaml-fake-petstore/**'

jobs:
build:
Expand All @@ -17,6 +19,7 @@ jobs:
matrix:
sample:
- 'samples/client/petstore/ocaml/'
- 'samples/client/petstore/ocaml-fake-petstore/'
steps:
- uses: actions/checkout@v5
- name: Set-up OCaml
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ samples/openapi3/client/petstore/go/privatekey.pem

## OCaml
samples/client/petstore/ocaml/_build/
samples/client/petstore/ocaml-fake-petstore/_build/

# jetbrain http client
samples/client/jetbrains/adyen/checkout71/http/client/Apis/http-client.private.env.json
6 changes: 6 additions & 0 deletions bin/configs/ocaml-fake-petstore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
generatorName: ocaml
outputDir: samples/client/petstore/ocaml-fake-petstore
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/ocaml
additionalProperties:
packageName: petstore_client
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public OCamlClientCodegen() {
typeMapping.put("short", "int");
typeMapping.put("char", "char");
typeMapping.put("float", "float");
typeMapping.put("decimal", "string");
typeMapping.put("double", "float");
typeMapping.put("integer", "int32");
typeMapping.put("number", "float");
Expand All @@ -179,6 +180,7 @@ public OCamlClientCodegen() {
typeMapping.put("any", "Yojson.Safe.t");
typeMapping.put("file", "string");
typeMapping.put("ByteArray", "string");
typeMapping.put("AnyType", "Yojson.Safe.t");
// lib
typeMapping.put("string", "string");
typeMapping.put("UUID", "string");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let {{{operationId}}} {{^hasParams}}(){{/hasParams}}{{#allParams}}{{> to_param}}
let uri = Request.{{> to_optional_prefix}}replace_path_param uri "{{{baseName}}}" {{> to_string}} {{{paramName}}} in
{{/pathParams}}
{{#queryParams}}
let uri = Request.{{> to_optional_prefix}}add_query_param{{#isArray}}_list{{/isArray}} uri "{{{baseName}}}" {{> to_string}} {{{paramName}}} in
let uri = Request.{{> to_optional_prefix}}add_query_param{{#isArray}}_list{{/isArray}}{{#isExplode}}{{#isFormStyle}}{{#isMap}}_exploded_form_object{{/isMap}}{{/isFormStyle}}{{/isExplode}} uri "{{{baseName}}}" {{> to_string}} {{{paramName}}} in
{{/queryParams}}
{{#hasAuthMethods}}
{{#authMethods}}
Expand All @@ -42,7 +42,10 @@ let {{{operationId}}} {{^hasParams}}(){{/hasParams}}{{#allParams}}{{> to_param}}
{{/authMethods}}
{{/hasAuthMethods}}
{{#bodyParams}}
let body = Request.{{#isFreeFormObject}}write_json_body{{/isFreeFormObject}}{{#isByteArray}}write_string_body{{/isByteArray}}{{^isFreeFormObject}}{{^isByteArray}}write_as_json_body{{/isByteArray}}{{/isFreeFormObject}} {{> to_json}} {{{paramName}}} in
let body = Request.
{{#isByteArray}}write_string_body {{{paramName}}}{{/isByteArray}}
{{^isByteArray}}write_as_json_body {{> to_json}} {{{paramName}}}{{/isByteArray}}
in
{{/bodyParams}}
{{^hasBodyParam}}
{{#hasFormParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ let of_int32 x = `Intlit (Int32.to_string x)

let of_int64 x = `Intlit (Int64.to_string x)

let of_list_of of_f l = `List (List.map of_f l)
let of_list_of of_f l = `List (Stdlib.List.map of_f l)

let of_map_of of_f l = `Assoc (List.map (fun (k, v) -> (k, of_f v)) l)
let of_map_of of_f l = `Assoc (Stdlib.List.map (fun (k, v) -> (k, of_f v)) l)
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let write_json_body payload =
let write_as_json_body to_json payload = write_json_body (to_json payload)

let handle_response resp on_success_handler =
match Cohttp_lwt.Response.status resp with
match Cohttp.Response.status resp with
| #Cohttp.Code.success_status -> on_success_handler ()
| s -> failwith ("Server responded with status " ^ Cohttp.Code.(reason_phrase_of_code (code_of_status s)))

Expand All @@ -52,13 +52,13 @@ let read_json_body_as_list resp body =
Lwt.(read_json_body resp body >|= Yojson.Safe.Util.to_list)

let read_json_body_as_list_of of_json resp body =
Lwt.(read_json_body_as_list resp body >|= List.map of_json)
Lwt.(read_json_body_as_list resp body >|= Stdlib.List.map of_json)

let read_json_body_as_map resp body =
Lwt.(read_json_body resp body >|= Yojson.Safe.Util.to_assoc)

let read_json_body_as_map_of of_json resp body =
Lwt.(read_json_body_as_map resp body >|= List.map (fun (s, v) -> (s, of_json v)))
Lwt.(read_json_body_as_map resp body >|= Stdlib.List.map (fun (s, v) -> (s, of_json v)))

let replace_string_path_param uri param_name param_value =
let regexp = Str.regexp (Str.quote ("{" ^ param_name ^ "}")) in
Expand All @@ -80,6 +80,19 @@ let add_query_param_list uri param_name to_string param_value =
let maybe_add_query_param uri param_name to_string param_value =
option_fold (add_query_param uri param_name to_string) uri param_value

(** Corresponds to:
- [style = form]
- [explode = true]
- type [object]

See https://swagger.io/docs/specification/v3_0/serialization/#query-parameters
*)
let add_query_param_exploded_form_object uri _param_name to_string param_value =
Stdlib.List.fold_left
(fun uri (param_name, param_value) -> add_query_param uri param_name to_string param_value)
uri
param_value

let init_form_encoded_body () = ""

let add_form_encoded_body_param params param_name to_string param_value =
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
{{#isArray}}{{#items}}(JsonSupport.of_list_of {{> to_json}}){{/items}}{{/isArray}}{{#isMap}}{{#items}}(JsonSupport.of_map_of {{> to_json}}){{/items}}{{/isMap}}{{#isString}}JsonSupport.of_string{{/isString}}{{#isLong}}JsonSupport.of_int64{{/isLong}}{{#isInteger}}JsonSupport.of_int32{{/isInteger}}{{#isFloat}}JsonSupport.of_float{{/isFloat}}{{#isNumber}}JsonSupport.of_float{{/isNumber}}{{#isDouble}}JsonSupport.of_float{{/isDouble}}{{#isBoolean}}JsonSupport.of_bool{{/isBoolean}}{{^isEnum}}{{#isModel}}{{#vendorExtensions.x-model-module}}{{{vendorExtensions.x-model-module}}}.to_yojson{{/vendorExtensions.x-model-module}}{{^vendorExtensions.x-model-module}}{{{baseType}}}.to_yojson{{/vendorExtensions.x-model-module}}{{/isModel}}{{/isEnum}}{{^isModel}}{{^isContainer}}{{#isEnum}}Enums.{{{datatypeWithEnum}}}_to_yojson{{/isEnum}}{{/isContainer}}{{/isModel}}
{{#isArray}}
{{#items}}
(JsonSupport.of_list_of {{> to_json}})
{{/items}}
{{/isArray}}
{{#isMap}}
{{#items}}
(JsonSupport.of_map_of {{> to_json}})
{{/items}}
{{/isMap}}
{{#isString}}JsonSupport.of_string{{/isString}}
{{#isLong}}JsonSupport.of_int64{{/isLong}}
{{#isInteger}}JsonSupport.of_int32{{/isInteger}}
{{#isFloat}}JsonSupport.of_float{{/isFloat}}
{{#isNumber}}JsonSupport.of_float{{/isNumber}}
{{#isDouble}}JsonSupport.of_float{{/isDouble}}
{{#isBoolean}}JsonSupport.of_bool{{/isBoolean}}
{{^isEnum}}
{{#isModel}}
{{#vendorExtensions.x-model-module}}
{{{vendorExtensions.x-model-module}}}.to_yojson
{{/vendorExtensions.x-model-module}}
{{^vendorExtensions.x-model-module}}
{{{baseType}}}.to_yojson
{{/vendorExtensions.x-model-module}}
{{/isModel}}
{{/isEnum}}
{{^isModel}}
{{^isContainer}}
{{#isEnum}}
Enums.{{{datatypeWithEnum}}}_to_yojson
{{/isEnum}}
{{/isContainer}}
{{/isModel}}
{{#isBinary}}JsonSupport.of_string{{/isBinary}}
{{#isAnyType}}(fun x -> x){{/isAnyType}}
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
{{#isContainer}}{{#items}}(List.map {{> to_string}}){{/items}}{{/isContainer}}{{^isEnum}}{{#isLong}}Int64.to_string{{/isLong}}{{#isInteger}}Int32.to_string{{/isInteger}}{{#isFloat}}string_of_float{{/isFloat}}{{#isNumber}}string_of_float{{/isNumber}}{{#isDouble}}string_of_float{{/isDouble}}{{#isBoolean}}string_of_bool{{/isBoolean}}{{#isFile}}(fun x -> x){{/isFile}}{{#isDate}}(fun x -> x){{/isDate}}{{#isDateTime}}(fun x -> x){{/isDateTime}}{{#isString}}(fun x -> x){{/isString}}{{#isByteArray}}(fun x -> x){{/isByteArray}}{{#isModel}}{{{vendorExtensions.x-model-module}}}.show{{/isModel}}{{/isEnum}}{{^isModel}}{{^isContainer}}{{#isEnum}}Enums.show_{{{datatypeWithEnum}}}{{/isEnum}}{{/isContainer}}{{/isModel}}
{{#isArray}}
{{#items}}(Stdlib.List.map {{> to_string}}){{/items}}
{{/isArray}}
{{#isMap}}{{! For maps, only transform the value, the call site will take care of properly transforming the key-value pairs since it depends on the context. }}
{{#items}}{{> to_string}}{{/items}}
{{/isMap}}
{{^isEnum}}
{{#isLong}}Int64.to_string{{/isLong}}
{{#isInteger}}Int32.to_string{{/isInteger}}
{{#isFloat}}string_of_float{{/isFloat}}
{{#isNumber}}string_of_float{{/isNumber}}
{{#isDouble}}string_of_float{{/isDouble}}
{{#isBoolean}}string_of_bool{{/isBoolean}}
{{#isFile}}(fun x -> x){{/isFile}}
{{#isDate}}(fun x -> x){{/isDate}}
{{#isDateTime}}(fun x -> x){{/isDateTime}}
{{#isString}}(fun x -> x){{/isString}}
{{#isByteArray}}(fun x -> x){{/isByteArray}}
{{#isModel}}{{{vendorExtensions.x-model-module}}}.show{{/isModel}}
{{/isEnum}}
{{^isModel}}
{{^isContainer}}
{{#isEnum}}Enums.show_{{{datatypeWithEnum}}}{{/isEnum}}
{{#isEnumRef}}(failwith "Unsupported: enum reference"){{/isEnumRef}}
{{/isContainer}}
{{/isModel}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
README.md
dune
dune-project
petstore_client.opam
src/apis/another_fake_api.ml
src/apis/another_fake_api.mli
src/apis/default_api.ml
src/apis/default_api.mli
src/apis/fake_api.ml
src/apis/fake_api.mli
src/apis/fake_classname_tags123_api.ml
src/apis/fake_classname_tags123_api.mli
src/apis/pet_api.ml
src/apis/pet_api.mli
src/apis/store_api.ml
src/apis/store_api.mli
src/apis/user_api.ml
src/apis/user_api.mli
src/models/additional_properties_class.ml
src/models/all_of_with_single_ref.ml
src/models/animal.ml
src/models/api_response.ml
src/models/array_of_array_of_number_only.ml
src/models/array_of_number_only.ml
src/models/array_test.ml
src/models/capitalization.ml
src/models/cat.ml
src/models/category.ml
src/models/child_with_nullable.ml
src/models/class_model.ml
src/models/client.ml
src/models/deprecated_object.ml
src/models/dog.ml
src/models/enum_arrays.ml
src/models/enum_test.ml
src/models/fake_big_decimal_map_200_response.ml
src/models/file.ml
src/models/file_schema_test_class.ml
src/models/foo.ml
src/models/format_test.ml
src/models/has_only_read_only.ml
src/models/health_check_result.ml
src/models/list.ml
src/models/map_test.ml
src/models/mixed_properties_and_additional_properties_class.ml
src/models/model_200_response.ml
src/models/model__foo_get_default_response.ml
src/models/model__special_model_name_.ml
src/models/name.ml
src/models/nullable_class.ml
src/models/number_only.ml
src/models/object_with_deprecated_fields.ml
src/models/order.ml
src/models/outer_composite.ml
src/models/outer_object_with_enum_property.ml
src/models/parent_with_nullable.ml
src/models/pet.ml
src/models/read_only_first.ml
src/models/return.ml
src/models/tag.ml
src/models/test_inline_freeform_additional_properties_request.ml
src/models/user.ml
src/support/enums.ml
src/support/jsonSupport.ml
src/support/request.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.15.0-SNAPSHOT
33 changes: 33 additions & 0 deletions samples/client/petstore/ocaml-fake-petstore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \'' \\

This OCaml package is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project:

- API version: 1.0.0
- Package version: 1.0.0
- Generator version: 7.15.0-SNAPSHOT
- Build package: org.openapitools.codegen.languages.OCamlClientCodegen

## Requirements.

OCaml 5.x

## Installation

Please run the following commands to build the package `petstore_client`:

```sh
opam install . --deps-only --with-test
eval $(opam env)
dune build
```

## Getting Started

The generated directory structure is:
- `src/apis`: contains several modules, each with several functions. Each function is an API endpoint.
- `src/models`: contains several modules. Each module contains:
- a type `t` representing an input and/or output schema of the OpenAPI spec
- a smart constructor `create` for this type
- `src/support`: various modules used by the generated APIs and Models

9 changes: 9 additions & 0 deletions samples/client/petstore/ocaml-fake-petstore/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(include_subdirs unqualified)
(library
(name petstore_client)
(public_name petstore_client)
(flags (:standard -w -27))
(libraries str cohttp-lwt-unix lwt yojson ppx_deriving_yojson.runtime)
(preprocess (pps ppx_deriving_yojson ppx_deriving.std))
(wrapped true)
)
2 changes: 2 additions & 0 deletions samples/client/petstore/ocaml-fake-petstore/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 1.10)
(name petstore_client)
24 changes: 24 additions & 0 deletions samples/client/petstore/ocaml-fake-petstore/petstore_client.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
opam-version: "2.0"
name: "petstore_client"
version: "1.0.0"
synopsis: ""
description: """
Longer description
"""
maintainer: "Name <email>"
authors: "Name <email>"
license: ""
homepage: ""
bug-reports: ""
dev-repo: ""
depends: [
"ocaml"
"ocamlfind"
"dune"
"ppx_deriving_yojson"
"conf-libev"
"lwt"
"cohttp-lwt-unix" {< "6.0.0"}
"cohttp-async" {< "6.0.0"}
]
build: ["dune" "build" "-p" name]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(*
* This file has been generated by the OCamlClientCodegen generator for openapi-generator.
*
* Generated by: https://openapi-generator.tech
*
*)

let call_123_test_special_tags ~client_t =
let open Lwt.Infix in
let uri = Request.build_uri "/another-fake/dummy" in
let headers = Request.default_headers in
let body = Request.

write_as_json_body






Client.to_yojson


client_t
in
Cohttp_lwt_unix.Client.call `PATCH uri ~headers ~body >>= fun (resp, body) ->
Request.read_json_body_as (JsonSupport.unwrap Client.of_yojson) resp body

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(*
* This file has been generated by the OCamlClientCodegen generator for openapi-generator.
*
* Generated by: https://openapi-generator.tech
*
*)

val call_123_test_special_tags : client_t:Client.t -> Client.t Lwt.t
Loading
Loading