Skip to content

Commit 0f13dfe

Browse files
authored
[OCaml] Fix default optional list (#20789)
* Cleanup the existing model template * `{{^isContainer}}foobar{{/isContainer}}{{^isContainer}}` is equivalent to `{{^isContainer}}foobar` * Add indentations to make it easier to read the template. The generated code is uglier, but it is ok since users are encouraged to reformat anyway * Add a default value for non-required lists Closes #20777
1 parent c96d308 commit 0f13dfe

File tree

7 files changed

+156
-29
lines changed

7 files changed

+156
-29
lines changed

modules/openapi-generator/src/main/resources/ocaml/model.mustache

+23-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,31 @@ type t = {
1616
(* {{{.}}} *)
1717
{{/description}}
1818
{{#isEnum}}
19-
{{{name}}}: {{^isMap}}Enums.{{/isMap}}{{{datatypeWithEnum}}}{{^isContainer}}{{#required}}{{#defaultValue}}[@default {{{.}}}]{{/defaultValue}}{{/required}}{{/isContainer}}{{^isContainer}}{{#required}}{{#isNullable}} option [@default {{#defaultValue}}Some({{{.}}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}]{{/isNullable}}{{/required}}{{/isContainer}}{{^isContainer}}{{^required}} option [@default {{#defaultValue}}Some({{{.}}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}]{{/required}}{{/isContainer}}; [@key "{{{baseName}}}"]
19+
{{{name}}}: {{^isMap}}Enums.{{/isMap}}{{{datatypeWithEnum}}}
20+
{{^isContainer}}
21+
{{#required}}
22+
{{#defaultValue}}[@default {{{.}}}]{{/defaultValue}}
23+
{{#isNullable}} option [@default
24+
{{#defaultValue}}Some({{{.}}}){{/defaultValue}}
25+
{{^defaultValue}}None{{/defaultValue}}
26+
]
27+
{{/isNullable}}
28+
{{/required}}
29+
{{^required}} option [@default
30+
{{#defaultValue}}Some({{{.}}}){{/defaultValue}}
31+
{{^defaultValue}}None{{/defaultValue}}
32+
]
33+
{{/required}}
34+
{{/isContainer}}; [@key "{{{baseName}}}"]
2035
{{/isEnum}}
2136
{{^isEnum}}
22-
{{{name}}}: {{{datatypeWithEnum}}}{{^isContainer}}{{#required}}{{#isNullable}} option{{/isNullable}}{{/required}}{{/isContainer}}{{^isContainer}}{{^required}} option [@default None]{{/required}}{{/isContainer}}; [@key "{{{baseName}}}"]
37+
{{{name}}}: {{{datatypeWithEnum}}}
38+
{{^isContainer}}
39+
{{#required}}{{#isNullable}} option{{/isNullable}}{{/required}}
40+
{{^required}} option [@default None]{{/required}}
41+
{{/isContainer}}
42+
{{#isArray}}{{^required}} [@default []]{{/required}}{{/isArray}}
43+
; [@key "{{{baseName}}}"]
2344
{{/isEnum}}
2445
{{/vars}}
2546
} [@@deriving yojson { strict = false }, show ];;

samples/client/petstore/ocaml/src/models/api_response.ml

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@
77
*)
88

99
type t = {
10-
code: int32 option [@default None]; [@key "code"]
11-
_type: string option [@default None]; [@key "type"]
12-
message: string option [@default None]; [@key "message"]
10+
code: int32
11+
12+
option [@default None]
13+
14+
; [@key "code"]
15+
_type: string
16+
17+
option [@default None]
18+
19+
; [@key "type"]
20+
message: string
21+
22+
option [@default None]
23+
24+
; [@key "message"]
1325
} [@@deriving yojson { strict = false }, show ];;
1426

1527
(** Describes the result of uploading an image resource *)

samples/client/petstore/ocaml/src/models/category.ml

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
*)
88

99
type t = {
10-
id: int64 option [@default None]; [@key "id"]
11-
name: string option [@default None]; [@key "name"]
10+
id: int64
11+
12+
option [@default None]
13+
14+
; [@key "id"]
15+
name: string
16+
17+
option [@default None]
18+
19+
; [@key "name"]
1220
} [@@deriving yojson { strict = false }, show ];;
1321

1422
(** A category for a pet *)

samples/client/petstore/ocaml/src/models/order.ml

+31-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,38 @@
77
*)
88

99
type t = {
10-
id: int64 option [@default None]; [@key "id"]
11-
pet_id: int64 option [@default None]; [@key "petId"]
12-
quantity: int32 option [@default None]; [@key "quantity"]
13-
ship_date: string option [@default None]; [@key "shipDate"]
10+
id: int64
11+
12+
option [@default None]
13+
14+
; [@key "id"]
15+
pet_id: int64
16+
17+
option [@default None]
18+
19+
; [@key "petId"]
20+
quantity: int32
21+
22+
option [@default None]
23+
24+
; [@key "quantity"]
25+
ship_date: string
26+
27+
option [@default None]
28+
29+
; [@key "shipDate"]
1430
(* Order Status *)
15-
status: Enums.status option [@default None]; [@key "status"]
16-
complete: bool option [@default None]; [@key "complete"]
31+
status: Enums.status
32+
option [@default
33+
34+
None
35+
]
36+
; [@key "status"]
37+
complete: bool
38+
39+
option [@default None]
40+
41+
; [@key "complete"]
1742
} [@@deriving yojson { strict = false }, show ];;
1843

1944
(** An order for a pets from the pet store *)

samples/client/petstore/ocaml/src/models/pet.ml

+27-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,34 @@
77
*)
88

99
type t = {
10-
id: int64 option [@default None]; [@key "id"]
11-
category: Category.t option [@default None]; [@key "category"]
12-
name: string; [@key "name"]
13-
photo_urls: string list; [@key "photoUrls"]
14-
tags: Tag.t list; [@key "tags"]
10+
id: int64
11+
12+
option [@default None]
13+
14+
; [@key "id"]
15+
category: Category.t
16+
17+
option [@default None]
18+
19+
; [@key "category"]
20+
name: string
21+
22+
23+
24+
; [@key "name"]
25+
photo_urls: string list
26+
27+
; [@key "photoUrls"]
28+
tags: Tag.t list
29+
[@default []]
30+
; [@key "tags"]
1531
(* pet status in the store *)
16-
status: Enums.pet_status option [@default None]; [@key "status"]
32+
status: Enums.pet_status
33+
option [@default
34+
35+
None
36+
]
37+
; [@key "status"]
1738
} [@@deriving yojson { strict = false }, show ];;
1839

1940
(** A pet for sale in the pet store *)

samples/client/petstore/ocaml/src/models/tag.ml

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
*)
88

99
type t = {
10-
id: int64 option [@default None]; [@key "id"]
11-
name: string option [@default None]; [@key "name"]
10+
id: int64
11+
12+
option [@default None]
13+
14+
; [@key "id"]
15+
name: string
16+
17+
option [@default None]
18+
19+
; [@key "name"]
1220
} [@@deriving yojson { strict = false }, show ];;
1321

1422
(** A tag for a pet *)

samples/client/petstore/ocaml/src/models/user.ml

+40-8
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,47 @@
77
*)
88

99
type t = {
10-
id: int64 option [@default None]; [@key "id"]
11-
username: string option [@default None]; [@key "username"]
12-
first_name: string option [@default None]; [@key "firstName"]
13-
last_name: string option [@default None]; [@key "lastName"]
14-
email: string option [@default None]; [@key "email"]
15-
password: string option [@default None]; [@key "password"]
16-
phone: string option [@default None]; [@key "phone"]
10+
id: int64
11+
12+
option [@default None]
13+
14+
; [@key "id"]
15+
username: string
16+
17+
option [@default None]
18+
19+
; [@key "username"]
20+
first_name: string
21+
22+
option [@default None]
23+
24+
; [@key "firstName"]
25+
last_name: string
26+
27+
option [@default None]
28+
29+
; [@key "lastName"]
30+
email: string
31+
32+
option [@default None]
33+
34+
; [@key "email"]
35+
password: string
36+
37+
option [@default None]
38+
39+
; [@key "password"]
40+
phone: string
41+
42+
option [@default None]
43+
44+
; [@key "phone"]
1745
(* User Status *)
18-
user_status: int32 option [@default None]; [@key "userStatus"]
46+
user_status: int32
47+
48+
option [@default None]
49+
50+
; [@key "userStatus"]
1951
} [@@deriving yojson { strict = false }, show ];;
2052

2153
(** A User who is purchasing from the pet store *)

0 commit comments

Comments
 (0)