-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[Fix][OCaml] Properly handle free-forms and byte arrays #21681
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fbb46ed
Add OCaml fake-petstore to test corner cases
sir4ur0n 5215f21
Prefix List functions with Stdlib as the fake petstore generates a Li…
sir4ur0n a5f098a
Handle decimal and any types
sir4ur0n 675ba32
Indent to_json.mustache for easier maintenance
sir4ur0n 3ea004a
Indent api-impl.mustache a bit more for readability before fix
sir4ur0n 38060f9
Fix: do not call `to_json` for free forms and byte arrays
sir4ur0n 2d29839
Fix compilation for binary types
sir4ur0n 2bc64f7
Indent to_string.mustache
sir4ur0n 74c8e30
Add support for exploded form-style object query params
sir4ur0n 847d2c5
Add ocaml-fake-petstore to CI
sir4ur0n a9e6ca2
Fix free-form body params
sir4ur0n cad4d18
Cohttp_lwt.Response is deprecated, use Cohttp.Response instead
sir4ur0n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 36 additions & 1 deletion
37
modules/openapi-generator/src/main/resources/ocaml/to_json.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
27 changes: 26 additions & 1 deletion
27
modules/openapi-generator/src/main/resources/ocaml/to_string.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
23 changes: 23 additions & 0 deletions
23
samples/client/petstore/ocaml-fake-petstore/.openapi-generator-ignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
65 changes: 65 additions & 0 deletions
65
samples/client/petstore/ocaml-fake-petstore/.openapi-generator/FILES
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
samples/client/petstore/ocaml-fake-petstore/.openapi-generator/VERSION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7.15.0-SNAPSHOT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
samples/client/petstore/ocaml-fake-petstore/petstore_client.opam
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
28 changes: 28 additions & 0 deletions
28
samples/client/petstore/ocaml-fake-petstore/src/apis/another_fake_api.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
8 changes: 8 additions & 0 deletions
8
samples/client/petstore/ocaml-fake-petstore/src/apis/another_fake_api.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.