Skip to content

Commit 4f5a664

Browse files
committed
OCaml: Fix enums in anyOf
1 parent 067bd6b commit 4f5a664

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,37 @@ public ModelsMap postProcessModels(ModelsMap objs) {
295295
}
296296
}
297297
}
298+
299+
// Fix enum references in composed schemas (anyOf, oneOf, allOf)
300+
if (cm.getComposedSchemas() != null) {
301+
fixEnumReferencesInComposedSchemas(cm.getComposedSchemas().getAnyOf());
302+
fixEnumReferencesInComposedSchemas(cm.getComposedSchemas().getOneOf());
303+
fixEnumReferencesInComposedSchemas(cm.getComposedSchemas().getAllOf());
304+
}
298305
}
299306

300307
return objs;
301308
}
302309

310+
private void fixEnumReferencesInComposedSchemas(List<CodegenProperty> schemas) {
311+
if (schemas == null) {
312+
return;
313+
}
314+
315+
for (CodegenProperty schema : schemas) {
316+
// If this schema is an enum, add Enums. prefix to datatypeWithEnum
317+
if (schema.isEnum) {
318+
if (!schema.datatypeWithEnum.startsWith("Enums.")) {
319+
schema.datatypeWithEnum = "Enums." + schema.datatypeWithEnum;
320+
}
321+
// Also update dataType for the variant constructor
322+
if (!schema.dataType.startsWith("Enums.")) {
323+
schema.dataType = "Enums." + schema.dataType;
324+
}
325+
}
326+
}
327+
}
328+
303329
private void enrichPropertiesWithEnumDefaultValues(List<CodegenProperty> properties) {
304330
for (CodegenProperty property : properties) {
305331
if (property.get_enum() != null && property.get_enum().size() == 1) {

samples/client/petstore/ocaml-enum-in-composed-schema/src/models/test_model_optional_url.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
type t =
1010
| AnyOf0 of string
11-
| AnyOf1 of any_of_1
11+
| AnyOf1 of Enums.any_of_1
1212
[@@deriving show, eq];;
1313

1414
let to_yojson = function
1515
| AnyOf0 v -> [%to_yojson: string] v
16-
| AnyOf1 v -> [%to_yojson: any_of_1] v
16+
| AnyOf1 v -> [%to_yojson: Enums.any_of_1] v
1717

1818
(* Manual implementations because the derived one encodes into a tuple list where the first element is the constructor name. *)
1919

@@ -22,7 +22,7 @@
2222
[%of_yojson: string] json
2323
|> Stdlib.Result.to_option
2424
|> Stdlib.Option.map (fun v -> AnyOf0 v);
25-
[%of_yojson: any_of_1] json
25+
[%of_yojson: Enums.any_of_1] json
2626
|> Stdlib.Result.to_option
2727
|> Stdlib.Option.map (fun v -> AnyOf1 v);
2828
]

0 commit comments

Comments
 (0)