Fixed #3521. Response headers were not processed for enums collection. (#3526)

* Fixed #3521. Response headers were not processed for enums collection.

* Replace " by '' in text fields to allow these to be used in OCaml comments.

* Added support of ByteArray (i.e. base64 encoded data) body and free form object as request body. Added support of free form object in response body as well.

* Added result as a reserved word to prevent generating model files with this name (having a result.ml model file confuses dune into thinking some other model modules depend on Result when they don't).

* Updated samples to reflect the latest changes in the OCaml generator.
This commit is contained in:
cgensoul 2019-08-02 13:04:53 +02:00 committed by William Cheng
parent d0f0e17542
commit 1c36fa9fe3
8 changed files with 49 additions and 19 deletions

View File

@ -18,6 +18,7 @@ package org.openapitools.codegen.languages;
import com.google.common.base.Strings;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.responses.ApiResponse;
@ -87,7 +88,9 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig
"new", "nonrec", "object", "of", "open", "or",
"private", "rec", "sig", "struct", "then", "to",
"true", "try", "type", "val", "virtual", "when",
"while", "with"
"while", "with",
"result"
)
);
@ -141,7 +144,7 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig
typeMapping.put("object", "Yojson.Safe.t");
typeMapping.put("any", "Yojson.Safe.t");
typeMapping.put("file", "string");
typeMapping.put("ByteArray", "bytes");
typeMapping.put("ByteArray", "string");
// lib
typeMapping.put("string", "string");
typeMapping.put("UUID", "string");
@ -318,6 +321,13 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig
collectEnumSchemas(p, content.get(p).getSchema());
}
}
if (apiResponse.getHeaders() != null) {
Map<String, Header> headers = apiResponse.getHeaders();
for (String h : headers.keySet()) {
Header header = headers.get(h);
collectEnumSchemas(h, header.getSchema());
}
}
}
}
}
@ -668,6 +678,10 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig
param.vendorExtensions.put(X_MODEL_MODULE, param.dataType.substring(0, param.dataType.lastIndexOf('.')));
}
}
if ("Yojson.Safe.t".equals(operation.returnBaseType)) {
operation.vendorExtensions.put("x-returnFreeFormObject", true);
}
}
for (Map.Entry<String, String> e : enumUniqNames.entrySet()) {
@ -704,7 +718,7 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig
return input
.replace("*)", "*_)")
.replace("(*", "(_*")
.replace("\"", "\\\"");
.replace("\"", "''");
}
@Override

View File

@ -18,13 +18,13 @@ let {{{operationId}}} {{^hasParams}}(){{/hasParams}}{{#allParams}}{{{paramName}}
let uri = Request.replace_path_param uri "{{{baseName}}}" ({{> to_string}}{{{paramName}}}) in{{/pathParams}}{{#queryParams}}
let uri = Uri.add_query_param{{^isListContainer}}'{{/isListContainer}} uri ("{{{baseName}}}", {{> to_string}}{{{paramName}}}) in{{/queryParams}}{{#hasAuthMethods}}{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}}
let uri = Uri.add_query_param' uri ("{{{keyParamName}}}", Request.api_key) in{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}{{/hasAuthMethods}}{{#bodyParams}}
let body = Request.write_json_body {{> to_json}} {{{paramName}}} in{{/bodyParams}}{{^hasBodyParam}}{{#hasFormParams}}
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{{/bodyParams}}{{^hasBodyParam}}{{#hasFormParams}}
let body = Request.init_form_encoded_body () in{{/hasFormParams}}{{#formParams}}
let body = Request.add_form_encoded_body_param{{#isContainer}}s{{/isContainer}} body ("{{{paramName}}}", {{> to_string}}{{{paramName}}}) in{{/formParams}}{{#hasFormParams}}
let body = Request.finalize_form_encoded_body body in{{/hasFormParams}}{{/hasBodyParam}}
Cohttp_lwt_unix.Client.call `{{{httpMethod}}} uri ~headers {{#hasBodyParam}}~body {{/hasBodyParam}}{{^hasBodyParam}}{{#hasFormParams}}~body {{/hasFormParams}}{{/hasBodyParam}}>>= fun (resp, body) ->{{^returnType}}
Request.handle_unit_response resp{{/returnType}}{{#returnType}}
Request.read_json_body{{#returnContainer}}{{#isListContainer}}_as_list{{/isListContainer}}{{#isMapContainer}}_as_map{{/isMapContainer}}{{#returnBaseType}}_of{{/returnBaseType}}{{/returnContainer}}{{^returnContainer}}{{#returnBaseType}}_as{{/returnBaseType}}{{/returnContainer}} {{#returnType}}({{> of_json}}){{/returnType}} resp body{{/returnType}}
Request.read_json_body{{#returnContainer}}{{#isListContainer}}_as_list{{/isListContainer}}{{#isMapContainer}}_as_map{{/isMapContainer}}{{#returnBaseType}}{{^vendorExtensions.x-returnFreeFormObject}}_of{{/vendorExtensions.x-returnFreeFormObject}}{{/returnBaseType}}{{/returnContainer}}{{^returnContainer}}{{#returnBaseType}}{{^vendorExtensions.x-returnFreeFormObject}}_as{{/vendorExtensions.x-returnFreeFormObject}}{{/returnBaseType}}{{/returnContainer}} {{#returnType}}{{^vendorExtensions.x-returnFreeFormObject}}({{> of_json}}){{/vendorExtensions.x-returnFreeFormObject}}{{/returnType}} resp body{{/returnType}}
{{/operation}}
{{/operations}}

View File

@ -3,8 +3,13 @@ let base_url = "{{{basePath}}}"
let default_headers = Cohttp.Header.init_with "Content-Type" "application/json"
let build_uri operation_path = Uri.of_string (base_url ^ operation_path)
let write_json_body to_json payload =
to_json payload |> Yojson.Safe.to_string ~std:true |> Cohttp_lwt.Body.of_string
let write_string_body s = Cohttp_lwt.Body.of_string s
let write_json_body payload =
Cohttp_lwt.Body.of_string (Yojson.Safe.to_string payload ~std:true)
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
@ -26,8 +31,11 @@ let read_json_body_as_list resp body =
let read_json_body_as_list_of of_json resp body =
Lwt.(read_json_body_as_list resp body >|= 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 resp body >|= Yojson.Safe.Util.to_assoc >|= List.map (fun (s, v) -> (s, of_json v)))
Lwt.(read_json_body_as_map resp body >|= List.map (fun (s, v) -> (s, of_json v)))
let replace_path_param uri param_name param_value =
let regexp = Str.regexp (Str.quote ("{" ^ param_name ^ "}")) in

View File

@ -1 +1 @@
{{#isContainer}}{{#items}}{{#isEnum}}List.map {{> to_string}}{{/isEnum}}{{#isModel}}List.map {{> to_string}}{{/isModel}}{{/items}}{{/isContainer}}{{^isEnum}}{{#isString}}{{/isString}}{{#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}}{{#isByteArray}}Bytes.to_string {{/isByteArray}}{{#isModel}}{{{vendorExtensions.x-modelModule}}}.show {{/isModel}}{{/isEnum}}{{^isModel}}{{^isContainer}}{{#isEnum}}Enums.show_{{{datatypeWithEnum}}} {{/isEnum}}{{/isContainer}}{{/isModel}}
{{#isContainer}}{{#items}}{{#isEnum}}List.map {{> to_string}}{{/isEnum}}{{#isModel}}List.map {{> to_string}}{{/isModel}}{{/items}}{{/isContainer}}{{^isEnum}}{{#isString}}{{/isString}}{{#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}}{{#isByteArray}}{{/isByteArray}}{{#isModel}}{{{vendorExtensions.x-modelModule}}}.show {{/isModel}}{{/isEnum}}{{^isModel}}{{^isContainer}}{{#isEnum}}Enums.show_{{{datatypeWithEnum}}} {{/isEnum}}{{/isContainer}}{{/isModel}}

View File

@ -9,7 +9,7 @@ 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
let body = Request.write_as_json_body Pet.to_yojson body in
Cohttp_lwt_unix.Client.call `POST uri ~headers ~body >>= fun (resp, body) ->
Request.handle_unit_response resp
@ -50,7 +50,7 @@ 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
let body = Request.write_as_json_body Pet.to_yojson body in
Cohttp_lwt_unix.Client.call `PUT uri ~headers ~body >>= fun (resp, body) ->
Request.handle_unit_response resp

View File

@ -32,7 +32,7 @@ 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
let body = Request.write_as_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

View File

@ -9,7 +9,7 @@ let create_user body =
let open Lwt in
let uri = Request.build_uri "/user" in
let headers = Request.default_headers in
let body = Request.write_json_body User.to_yojson body in
let body = Request.write_as_json_body User.to_yojson body in
Cohttp_lwt_unix.Client.call `POST uri ~headers ~body >>= fun (resp, body) ->
Request.handle_unit_response resp
@ -17,7 +17,7 @@ let create_users_with_array_input body =
let open Lwt in
let uri = Request.build_uri "/user/createWithArray" in
let headers = Request.default_headers in
let body = Request.write_json_body (JsonSupport.of_list_of User.to_yojson) body in
let body = Request.write_as_json_body (JsonSupport.of_list_of User.to_yojson) body in
Cohttp_lwt_unix.Client.call `POST uri ~headers ~body >>= fun (resp, body) ->
Request.handle_unit_response resp
@ -25,7 +25,7 @@ let create_users_with_list_input body =
let open Lwt in
let uri = Request.build_uri "/user/createWithList" in
let headers = Request.default_headers in
let body = Request.write_json_body (JsonSupport.of_list_of User.to_yojson) body in
let body = Request.write_as_json_body (JsonSupport.of_list_of User.to_yojson) body in
Cohttp_lwt_unix.Client.call `POST uri ~headers ~body >>= fun (resp, body) ->
Request.handle_unit_response resp
@ -66,7 +66,7 @@ let update_user username body =
let uri = Request.build_uri "/user/{username}" in
let headers = Request.default_headers in
let uri = Request.replace_path_param uri "username" (username) in
let body = Request.write_json_body User.to_yojson body in
let body = Request.write_as_json_body User.to_yojson body in
Cohttp_lwt_unix.Client.call `PUT uri ~headers ~body >>= fun (resp, body) ->
Request.handle_unit_response resp

View File

@ -3,8 +3,13 @@ let base_url = "http://petstore.swagger.io/v2"
let default_headers = Cohttp.Header.init_with "Content-Type" "application/json"
let build_uri operation_path = Uri.of_string (base_url ^ operation_path)
let write_json_body to_json payload =
to_json payload |> Yojson.Safe.to_string ~std:true |> Cohttp_lwt.Body.of_string
let write_string_body s = Cohttp_lwt.Body.of_string s
let write_json_body payload =
Cohttp_lwt.Body.of_string (Yojson.Safe.to_string payload ~std:true)
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
@ -26,8 +31,11 @@ let read_json_body_as_list resp body =
let read_json_body_as_list_of of_json resp body =
Lwt.(read_json_body_as_list resp body >|= 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 resp body >|= Yojson.Safe.Util.to_assoc >|= List.map (fun (s, v) -> (s, of_json v)))
Lwt.(read_json_body_as_map resp body >|= List.map (fun (s, v) -> (s, of_json v)))
let replace_path_param uri param_name param_value =
let regexp = Str.regexp (Str.quote ("{" ^ param_name ^ "}")) in