Skip to content

Commit f3e7e8d

Browse files
committed
Fix recursive types, improve tests
1 parent 073ad8f commit f3e7e8d

16 files changed

Lines changed: 236 additions & 106 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/OCamlClientCodegen.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,8 @@ public ModelsMap postProcessModels(ModelsMap objs) {
267267
for (List<CodegenProperty> propList : allPropertyLists) {
268268
for (CodegenProperty prop : propList) {
269269
if (selfRefPropNames.contains(prop.name)) {
270-
// Replace "ModelName.t" with just "t" in all relevant fields
271-
prop.dataType = "t";
272-
prop.datatypeWithEnum = "t";
273-
if (prop.baseType != null) {
274-
prop.baseType = "t";
275-
}
276-
if (prop.complexType != null) {
277-
prop.complexType = "t";
278-
}
279-
280-
// If it's a container type (e.g., array), update items as well
281270
if (prop.isContainer && prop.items != null) {
271+
// For containers, update items and reconstruct the container type
282272
prop.items.dataType = "t";
283273
prop.items.datatypeWithEnum = "t";
284274
if (prop.items.baseType != null) {
@@ -287,6 +277,27 @@ public ModelsMap postProcessModels(ModelsMap objs) {
287277
if (prop.items.complexType != null) {
288278
prop.items.complexType = "t";
289279
}
280+
281+
// Reconstruct the container type based on the updated items
282+
if (prop.isArray) {
283+
prop.dataType = "t list";
284+
prop.datatypeWithEnum = "t list";
285+
} else if (prop.isMap) {
286+
prop.dataType = "(string * t) list";
287+
prop.datatypeWithEnum = "(string * t) list";
288+
}
289+
} else {
290+
// For non-containers, just replace the type directly
291+
prop.dataType = "t";
292+
prop.datatypeWithEnum = "t";
293+
}
294+
295+
// Update baseType and complexType for all cases
296+
if (prop.baseType != null) {
297+
prop.baseType = "t";
298+
}
299+
if (prop.complexType != null) {
300+
prop.complexType = "t";
290301
}
291302
}
292303
}

modules/openapi-generator/src/test/resources/3_0/ocaml/direct-recursion.yaml

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,86 @@ info:
1414
cannot use it for OCaml testing.
1515
version: 1.0.0
1616
title: OCaml Direct Recursion Test
17-
paths:
18-
/tree:
19-
get:
20-
operationId: getTree
21-
description: Get a tree structure
22-
responses:
23-
'200':
24-
description: Success
25-
content:
26-
application/json:
27-
schema:
28-
$ref: '#/components/schemas/Tree'
29-
/node:
30-
post:
31-
operationId: createNode
32-
description: Create a node
33-
requestBody:
34-
required: true
35-
content:
36-
application/json:
37-
schema:
38-
$ref: '#/components/schemas/Node'
39-
responses:
40-
'201':
41-
description: Created
42-
content:
43-
application/json:
44-
schema:
45-
$ref: '#/components/schemas/Node'
17+
paths: {}
4618
components:
4719
schemas:
48-
Tree:
20+
ArrayRecursion:
4921
type: object
50-
description: A tree with optional children (direct self-reference)
22+
description: Test recursive type within an array (optional)
5123
properties:
5224
value:
5325
type: integer
5426
format: int32
5527
children:
5628
type: array
5729
items:
58-
$ref: '#/components/schemas/Tree'
30+
$ref: '#/components/schemas/ArrayRecursion'
5931
required:
6032
- value
6133

62-
Node:
34+
DirectRecursion:
6335
type: object
64-
description: A node with an optional self-reference
36+
description: Test direct self-reference (not in container, optional)
6537
properties:
6638
id:
6739
type: string
6840
next:
69-
$ref: '#/components/schemas/Node'
41+
$ref: '#/components/schemas/DirectRecursion'
7042
required:
7143
- id
44+
45+
MapRecursion:
46+
type: object
47+
description: Test recursive type within a map (optional)
48+
properties:
49+
name:
50+
type: string
51+
childMap:
52+
type: object
53+
additionalProperties:
54+
$ref: '#/components/schemas/MapRecursion'
55+
required:
56+
- name
57+
58+
RequiredRecursion:
59+
type: object
60+
description: Test required self-reference field
61+
properties:
62+
value:
63+
type: string
64+
parent:
65+
$ref: '#/components/schemas/RequiredRecursion'
66+
required:
67+
- value
68+
- parent
69+
70+
RequiredArrayRecursion:
71+
type: object
72+
description: Test required array of self-references
73+
properties:
74+
id:
75+
type: integer
76+
siblings:
77+
type: array
78+
items:
79+
$ref: '#/components/schemas/RequiredArrayRecursion'
80+
required:
81+
- id
82+
- siblings
83+
84+
MultipleRecursiveFields:
85+
type: object
86+
description: Test multiple recursive fields in the same model
87+
properties:
88+
name:
89+
type: string
90+
left:
91+
$ref: '#/components/schemas/MultipleRecursiveFields'
92+
right:
93+
$ref: '#/components/schemas/MultipleRecursiveFields'
94+
children:
95+
type: array
96+
items:
97+
$ref: '#/components/schemas/MultipleRecursiveFields'
98+
required:
99+
- name

samples/client/petstore/ocaml-additional-properties/.openapi-generator/FILES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.ocamlformat
2-
.openapi-generator-ignore
32
README.md
43
dune
54
dune-project

samples/client/petstore/ocaml-enum-in-composed-schema/.openapi-generator/FILES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.ocamlformat
2-
.openapi-generator-ignore
32
README.md
43
dune
54
dune-project

samples/client/petstore/ocaml-fake-petstore/.openapi-generator/FILES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.ocamlformat
2-
.openapi-generator-ignore
32
README.md
43
dune
54
dune-project

samples/client/petstore/ocaml-oneOf-primitive/.openapi-generator/FILES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.ocamlformat
2-
.openapi-generator-ignore
32
README.md
43
dune
54
dune-project
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
.ocamlformat
2-
.openapi-generator-ignore
32
README.md
43
dune
54
dune-project
65
recursion_test.opam
7-
src/apis/default_api.ml
8-
src/apis/default_api.mli
9-
src/models/node.ml
10-
src/models/tree.ml
6+
src/models/array_recursion.ml
7+
src/models/direct_recursion.ml
8+
src/models/map_recursion.ml
9+
src/models/multiple_recursive_fields.ml
10+
src/models/required_array_recursion.ml
11+
src/models/required_recursion.ml
1112
src/support/enums.ml
1213
src/support/jsonSupport.ml
1314
src/support/request.ml

samples/client/petstore/ocaml-recursion-test/src/apis/default_api.ml

Lines changed: 0 additions & 36 deletions
This file was deleted.

samples/client/petstore/ocaml-recursion-test/src/apis/default_api.mli

Lines changed: 0 additions & 9 deletions
This file was deleted.

samples/client/petstore/ocaml-recursion-test/src/models/tree.ml renamed to samples/client/petstore/ocaml-recursion-test/src/models/array_recursion.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Generated by: https://openapi-generator.tech
55
*
6-
* Schema Tree.t : A tree with optional children (direct self-reference)
6+
* Schema Array_recursion.t : Test recursive type within an array (optional)
77
*)
88

99

@@ -16,13 +16,13 @@
1616

1717

1818
; [@key "value"]
19-
children: t
19+
children: t list
2020
[@default []]
2121

2222
; [@key "children"]
2323
} [@@deriving yojson { strict = false }, show, eq ];;
2424

25-
(** A tree with optional children (direct self-reference) *)
25+
(** Test recursive type within an array (optional) *)
2626
let create (value : int32) : t = {
2727
value = value;
2828
children = [];

0 commit comments

Comments
 (0)