Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[OCaml] new client generator #3446

Merged
merged 40 commits into from
Jul 28, 2019
Merged

Conversation

cgensoul
Copy link
Contributor

#1159 Add OCaml client code generation support

This PR answers #1159 and adds OCaml Client code generation to openapi-generator. The generated OCaml project relies on the dune tool for the build process and uses :

  • cohttp as the http client lib using the lwt_unix backend (lwt is a future lib for OCaml, a competing future lib is Async which Cohttp supports as well but this generator only target the lwt implementation for now)
  • yojson as the JSon serialization lib
  • ppx_deriving_yojson for automatic generation of JSon serializers and deserializers for generated types
  • ppx_deriving.std for automatic generation of string convertion functions for generated types

It supports headers params, path params, query params, form params and body params. It currently has no support for authentication, nor file upload.

It generates one model file per OAS definition except for OAS enum definitions for which no model file is generated. Enums are treated globally and are all generated in a enums.ml file that contains one type per distinct enumeration present in the input file, even for enumerations that are not proper definitions but just local restrictions on a string parameter of an operation.

Note that generating one model file per OAS definition will work as long as there are no mutually recursive OAS definitions in the input file. Otherwise the generated OCaml code will not compile since it will generate mutually recursive compilation units which is not supported by the language.

For APIs files, both an implementation (.ml) and an interface (.mli) files are generated.

Christophe Gensoul and others added 28 commits July 24, 2019 18:46
…n sérialize les variants comme des tableaux JSON.
…sous forme d'un tableau JSON contenant une unique string.
… plusieurs énumérations partagent des valeurs communes ce que ne permettent pas les variants ordinaires au sein d'un même module.
…t prendre en compte les pathParams, les queryParams, les headersParams, les bodyParams et la réponse JSON. Manque le support du multipart, du form encoded et des mécanismes d'authentification.
…isation d'un fichier OAS plus gros et complexe que Petstore.
…exception Failure pour avoir des types plus simples et non dépendants de Pppx_deriving_yoson_runtime dans les APIs générées.
…ake-endpoints-models-for-testing.yaml generate properly.
…enerated identifiers to support all the corner cases present in modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml.
…/3_0/petstore-with-fake-endpoints-models-for-testing.yaml.
…he case of ArraySchema parameters. This allows correct processing of modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml.
…meters in the case of MapSchema parameters (if this type of parameter can is allowed).
@@ -0,0 +1,703 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cgensoul
Copy link
Contributor Author

Finally it was so easy to fix that I did it anyway ;)

@wing328
Copy link
Member

wing328 commented Jul 26, 2019

@cgensoul 👍 so is it ready for review and merge into master?

@wing328
Copy link
Member

wing328 commented Jul 26, 2019

Are there some commands to make sure the OCaml Petstore sample compiles?

An auto-generated README would be nice so that OCaml beginners will find it easy to use the OCaml library.

…ly one accepted value so that those fields can be deserialized even if the value is absent of the json payload.
@cgensoul
Copy link
Contributor Author

The command to compile the lib once all the tools (opam first then dune through opam is the easiest path) and dependencies (through opam as well) are installed is :

dune build

at the root of the generated code directory.

I'll add a README pointing to installation instructions of the tools and dependencies next week. There is an example program using the library committed in the repository in the samples/client/petstore/ocaml-client/bin directory. It's called test.ml and has an accompanying dune build file. I'll clean it up and reference it in the README.

Regarding merging to master, it can be merged without any risk since it's all new code and no existing classes have been modified by this PR but I think proper handling of response codes should be added before it's really useful to anyone. So maybe it's best to wait a bit to save some reviewing work ?

@cgensoul
Copy link
Contributor Author

I added handling of response codes ;)

@wing328
Copy link
Member

wing328 commented Jul 28, 2019

Tried running dune build locally and I got:

➜  ocaml-client git:(cgensoul-ocaml-client) dune build


File "dune", line 6, characters 18-33:
6 |    (libraries str cohttp-lwt-unix lwt yojson ppx_deriving_yojson.runtime)
                      ^^^^^^^^^^^^^^^
Error: Library "cohttp-lwt-unix" not found.
Hint: try: dune external-lib-deps --missing @@default
File "dune", line 7, characters 20-39:
7 |    (preprocess (pps ppx_deriving_yojson ppx_deriving.std))
                        ^^^^^^^^^^^^^^^^^^^
Error: Library "ppx_deriving_yojson" not found.
Hint: try: dune external-lib-deps --missing @@default

Do I need to run other commands in order to install those libraries locally?

@cgensoul
Copy link
Contributor Author

cgensoul commented Jul 28, 2019 via email

@wing328
Copy link
Member

wing328 commented Jul 28, 2019

Thanks for the tips. It works for me after I run opam install ppx_deriving_yojson cohttp ppx_deriving cohttp-lwt-unix (wihtout .std)

I would recommend including the opam install command in the readme as well.

@cgensoul
Copy link
Contributor Author

cgensoul commented Jul 28, 2019 via email

@wing328 wing328 changed the title Ocaml client [OCaml] new client generator Jul 28, 2019
@wing328 wing328 merged commit 1713c76 into OpenAPITools:master Jul 28, 2019
@wing328
Copy link
Member

wing328 commented Jul 28, 2019

@cgensoul thanks for the new generator. Do you have a Twitter account so that I can tag you in my tweet to promote the new generator?

@cgensoul
Copy link
Contributor Author

cgensoul commented Jul 28, 2019 via email

@wing328
Copy link
Member

wing328 commented Jul 28, 2019

I've filed #3483 for various enhancements.

@wing328
Copy link
Member

wing328 commented Aug 6, 2019

Note

This generator is still in beta and may subject to breaking changes (without fallbacks) in the future.

@wing328
Copy link
Member

wing328 commented Aug 10, 2019

@cgensoul thanks for the PR, which has been included in the 4.1.0 release: https://twitter.com/oas_generator/status/1160000504455319553

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants