Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[OCaml] various enhancements #3483

Merged
merged 6 commits into from
Jul 29, 2019
Merged
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: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ OpenAPI Generator allows generation of API client libraries (SDK generation), se

| | Languages/Frameworks |
|-|-|
**API clients** | **ActionScript**, **Ada**, **Apex**, **Bash**, **C**, **C#** (.net 2.0, 3.5 or later), **C++** (cpp-restsdk, Qt5, Tizen), **Clojure**, **Dart (1.x, 2.x)**, **Elixir**, **Elm**, **Eiffel**, **Erlang**, **Go**, **Groovy**, **Haskell** (http-client, Servant), **Java** (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured, Spring 5 Web Client), **Kotlin**, **Lua**, **Node.js/JavaScript** (ES5, ES6, AngularJS with Google Closure Compiler annotations, Flow types) **Objective-C**, **Perl**, **PHP**, **PowerShell**, **Python**, **R**, **Ruby**, **Rust** (rust, rust-server), **Scala** (akka, http4s, scalaz, swagger-async-httpclient), **Swift** (2.x, 3.x, 4.x), **Typescript** (AngularJS, Angular (2.x - 7.x), Aurelia, Axios, Fetch, Inversify, jQuery, Node, Rxjs)
**API clients** | **ActionScript**, **Ada**, **Apex**, **Bash**, **C**, **C#** (.net 2.0, 3.5 or later), **C++** (cpp-restsdk, Qt5, Tizen), **Clojure**, **Dart (1.x, 2.x)**, **Elixir**, **Elm**, **Eiffel**, **Erlang**, **Go**, **Groovy**, **Haskell** (http-client, Servant), **Java** (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured, Spring 5 Web Client), **Kotlin**, **Lua**, **Node.js/JavaScript** (ES5, ES6, AngularJS with Google Closure Compiler annotations, Flow types), **Objective-C**, **OCaml**, **Perl**, **PHP**, **PowerShell**, **Python**, **R**, **Ruby**, **Rust** (rust, rust-server), **Scala** (akka, http4s, scalaz, swagger-async-httpclient), **Swift** (2.x, 3.x, 4.x), **Typescript** (AngularJS, Angular (2.x - 7.x), Aurelia, Axios, Fetch, Inversify, jQuery, Node, Rxjs)
**Server stubs** | **Ada**, **C#** (ASP.NET Core, NancyFx), **C++** (Pistache, Restbed, Qt5 QHTTPEngine), **Erlang**, **F#** (Giraffe), **Go** (net/http, Gin), **Haskell** (Servant), **Java** (MSF4J, Spring, Undertow, JAX-RS: CDI, CXF, Inflector, RestEasy, Play Framework, [PKMST](https://github.com/ProKarma-Inc/pkmst-getting-started-examples)), **Kotlin** (Spring Boot, Ktor), **PHP** (Laravel, Lumen, Slim, Silex, [Symfony](https://symfony.com/), [Zend Expressive](https://github.com/zendframework/zend-expressive)), **Python** (Flask), **NodeJS**, **Ruby** (Sinatra, Rails5), **Rust** (rust-server), **Scala** ([Finch](https://github.com/finagle/finch), [Lagom](https://github.com/lagom/lagom), [Play](https://www.playframework.com/), Scalatra)
**API documentation generators** | **HTML**, **Confluence Wiki**
**Configuration files** | [**Apache2**](https://httpd.apache.org/)
Expand Down Expand Up @@ -680,6 +680,7 @@ Here is a list of template creators:
* JMeter: @davidkiss
* Kotlin: @jimschubert [:heart:](https://www.patreon.com/jimschubert)
* Lua: @daurnimator
* OCaml: @cgensoul
* Perl: @wing328 [:heart:](https://www.patreon.com/wing328)
* PHP (Guzzle): @baartosz
* PowerShell: @beatcracker
Expand Down
4 changes: 2 additions & 2 deletions bin/ocaml-client-petstore.sh → bin/ocaml-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ then
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DdebugOperations -DloggerPath=conf/log4j.properties"
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"

args="generate -t modules/openapi-generator/src/main/resources/ocaml-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g ocaml-client -o samples/client/petstore/ocaml-client --additional-properties packageName=petstore_client $@"
args="generate -t modules/openapi-generator/src/main/resources/ocaml -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g ocaml -o samples/client/petstore/ocaml --additional-properties packageName=petstore_client $@"

echo "java ${JAVA_OPTS} -jar ${executable} ${args}"
java $JAVA_OPTS -jar $executable $args
10 changes: 10 additions & 0 deletions bin/windows/ocaml-petstore.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g ocaml -o samples\client\petstore\ocaml

java %JAVA_OPTS% -jar %executable% %ags%
2 changes: 1 addition & 1 deletion docs/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The following generators are available:
- [kotlin](generators/kotlin.md)
- [lua](generators/lua.md)
- [objc](generators/objc.md)
- [ocaml-client](generators/ocaml-client.md)
- [ocaml](generators/ocaml.md)
- [perl](generators/perl.md)
- [php](generators/php.md)
- [powershell](generators/powershell.md)
Expand Down
13 changes: 13 additions & 0 deletions docs/generators/ocaml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

---
id: generator-opts-client-ocaml
title: Config Options for ocaml
sidebar_label: ocaml
---

| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CodegenType getTag() {
}

public String getName() {
return "ocaml-client";
return "ocaml";
}

public String getHelp() {
Expand All @@ -68,13 +68,13 @@ public String getHelp() {

public OCamlClientCodegen() {
super();
outputFolder = "generated-code/ocaml-client";
outputFolder = "generated-code/ocaml";
modelTemplateFiles.put("model.mustache", ".ml");

// default HIDE_GENERATION_TIMESTAMP to true
hideGenerationTimestamp = Boolean.TRUE;

embeddedTemplateDir = templateDir = "ocaml-client";
embeddedTemplateDir = templateDir = "ocaml";

setReservedWordsLowerCase(
Arrays.asList(
Expand All @@ -95,6 +95,7 @@ public OCamlClientCodegen() {

supportingFiles.add(new SupportingFile("dune.mustache", "", "dune"));
supportingFiles.add(new SupportingFile("dune-project.mustache", "", "dune-project"));
supportingFiles.add(new SupportingFile("readme.mustache", "", "README.md"));

defaultIncludes = new HashSet<>(
Arrays.asList(
Expand All @@ -106,7 +107,7 @@ public OCamlClientCodegen() {
"char",
"string",
"list"
)
)
);

languageSpecificPrimitives = new HashSet<>(
Expand Down Expand Up @@ -249,7 +250,7 @@ private boolean isEnumSchema(Schema schema) {

private void collectEnumSchemas(String parentName, String sName, Schema schema) {
if (schema instanceof ArraySchema) {
collectEnumSchemas(parentName, sName, ((ArraySchema)schema).getItems());
collectEnumSchemas(parentName, sName, ((ArraySchema) schema).getItems());
} else if (schema instanceof MapSchema && schema.getAdditionalProperties() instanceof Schema) {
collectEnumSchemas(parentName, sName, (Schema) schema.getAdditionalProperties());
} else if (isEnumSchema(schema)) {
Expand Down Expand Up @@ -348,7 +349,7 @@ private void computeEnumUniqNames() {
}
if (!hasDefName) {
int i = 0;
String candidate;;
String candidate;
while (definitiveNames.containsKey(candidate = sanitizeOCamlTypeName(nameCandidates.get(0) + "_" + i))) {
i++;
}
Expand All @@ -361,7 +362,7 @@ private void computeEnumUniqNames() {

private void collectEnumSchemas(OpenAPI openAPI) {
Components components = openAPI.getComponents();
if (components != null && components.getSchemas() != null && !components.getSchemas().isEmpty()) {
if (components != null && components.getSchemas() != null && !components.getSchemas().isEmpty()) {
collectEnumSchemas(null, components.getSchemas());
}

Expand Down
35 changes: 35 additions & 0 deletions modules/openapi-generator/src/main/resources/ocaml/readme.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# {{{projectName}}}
{{#appDescription}}
{{{appDescription}}}
{{/appDescription}}

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

- API version: {{appVersion}}
- Package version: {{packageVersion}}
{{^hideGenerationTimestamp}}
- Build date: {{generatedDate}}
{{/hideGenerationTimestamp}}
- Build package: {{generatorClass}}
{{#infoUrl}}
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
{{/infoUrl}}

## Requirements.

OCaml 4.x

## Installation

Please run the following commands to build the package `{{{packageName}}}`:

```sh
opam install ppx_deriving_yojson cohttp ppx_deriving cohttp-lwt-unix
eval $(opam env)
dune build
```

## Getting Started

TODO

23 changes: 23 additions & 0 deletions samples/client/petstore/ocaml/.openapi-generator-ignore
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
1 change: 1 addition & 0 deletions samples/client/petstore/ocaml/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.1.0-SNAPSHOT
26 changes: 26 additions & 0 deletions samples/client/petstore/ocaml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.

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
- Build package: org.openapitools.codegen.languages.OCamlClientCodegen

## Requirements.

OCaml 4.x

## Installation

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

```sh
opam install ppx_deriving_yojson cohttp ppx_deriving cohttp-lwt-unix
dune build
```

## Getting Started

TODO

9 changes: 9 additions & 0 deletions samples/client/petstore/ocaml/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/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 1.10)
(name petstore_client)
15 changes: 15 additions & 0 deletions samples/client/petstore/ocaml/petstore_client.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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" ]
build: ["dune" "build" "-p" name]
80 changes: 80 additions & 0 deletions samples/client/petstore/ocaml/src/apis/pet_api.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
(*
* This file has been generated by the OCamlClientCodegen generator for openapi-generator.
*
* Generated by: https://openapi-generator.tech
*
*)

let add_pet body =
let open Lwt in
let uri = Request.build_uri "/pet" in
let headers = Request.default_headers in
let body = Request.write_json_body Pet.to_yojson body in
Cohttp_lwt_unix.Client.call `POST uri ~headers ~body >>= fun (resp, body) ->
Request.handle_unit_response resp

let delete_pet pet_id api_key =
let open Lwt in
let uri = Request.build_uri "/pet/{petId}" in
let headers = Request.default_headers in
let headers = Cohttp.Header.add headers "api_key" (api_key) in
let uri = Request.replace_path_param uri "petId" (Int64.to_string pet_id) in
Cohttp_lwt_unix.Client.call `DELETE uri ~headers >>= fun (resp, body) ->
Request.handle_unit_response resp

let find_pets_by_status status =
let open Lwt in
let uri = Request.build_uri "/pet/findByStatus" in
let headers = Request.default_headers in
let uri = Uri.add_query_param 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 uri = Request.build_uri "/pet/findByTags" in
let headers = Request.default_headers in
let uri = Uri.add_query_param uri ("tags", 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 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
Cohttp_lwt_unix.Client.call `GET uri ~headers >>= fun (resp, body) ->
Request.read_json_body_as (JsonSupport.unwrap Pet.of_yojson) resp body

let update_pet body =
let open Lwt in
let uri = Request.build_uri "/pet" in
let headers = Request.default_headers in
let body = Request.write_json_body Pet.to_yojson body in
Cohttp_lwt_unix.Client.call `PUT uri ~headers ~body >>= fun (resp, body) ->
Request.handle_unit_response resp

let update_pet_with_form pet_id name status =
let open Lwt 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
let body = Request.init_form_encoded_body () in
let body = Request.add_form_encoded_body_param body ("name", name) in
let body = Request.add_form_encoded_body_param body ("status", status) in
let body = Request.finalize_form_encoded_body body in
Cohttp_lwt_unix.Client.call `POST uri ~headers ~body >>= fun (resp, body) ->
Request.handle_unit_response resp

let upload_file pet_id additional_metadata file =
let open Lwt 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
let body = Request.init_form_encoded_body () in
let body = Request.add_form_encoded_body_param body ("additional_metadata", additional_metadata) in
let body = Request.add_form_encoded_body_param body ("file", file) in
let body = Request.finalize_form_encoded_body body in
Cohttp_lwt_unix.Client.call `POST uri ~headers ~body >>= fun (resp, body) ->
Request.read_json_body_as (JsonSupport.unwrap Api_response.of_yojson) resp body

15 changes: 15 additions & 0 deletions samples/client/petstore/ocaml/src/apis/pet_api.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(*
* This file has been generated by the OCamlClientCodegen generator for openapi-generator.
*
* Generated by: https://openapi-generator.tech
*
*)

val add_pet : Pet.t -> unit Lwt.t
val delete_pet : int64 -> string -> unit Lwt.t
val find_pets_by_status : Enums.pet_status list -> Pet.t list Lwt.t
val find_pets_by_tags : string list -> Pet.t list Lwt.t
val get_pet_by_id : int64 -> Pet.t Lwt.t
val update_pet : Pet.t -> unit Lwt.t
val update_pet_with_form : int64 -> string -> string -> unit Lwt.t
val upload_file : int64 -> string -> string -> Api_response.t Lwt.t
38 changes: 38 additions & 0 deletions samples/client/petstore/ocaml/src/apis/store_api.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(*
* This file has been generated by the OCamlClientCodegen generator for openapi-generator.
*
* Generated by: https://openapi-generator.tech
*
*)

let delete_order order_id =
let open Lwt in
let uri = Request.build_uri "/store/order/{orderId}" in
let headers = Request.default_headers in
let uri = Request.replace_path_param uri "orderId" (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 uri = Request.build_uri "/store/inventory" in
let headers = Request.default_headers 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 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 body =
let open Lwt in
let uri = Request.build_uri "/store/order" in
let headers = Request.default_headers in
let body = Request.write_json_body Order.to_yojson body in
Cohttp_lwt_unix.Client.call `POST uri ~headers ~body >>= fun (resp, body) ->
Request.read_json_body_as (JsonSupport.unwrap Order.of_yojson) resp body

Loading