From 0e3e6403133e734568f448c982a6399ff8e4faf7 Mon Sep 17 00:00:00 2001 From: Nicolas BACQUEY Date: Thu, 7 Jul 2022 11:56:30 +0200 Subject: [PATCH] Lib_openapi: fix null handling with Combine constructor This commit fixes a bug where Null elements in Json schemas would not be processed correctly when being translated to OpenAPI schemas. The version of OpenAPI we use does not handle null elements in disjunctions, so instead we use a `nullable` property on said disjunctions, and don't convert null elements from Json schemas to OpenAPI. The problem was that null elements in disjunctions (i.e. the `Combine` element kind) can be represented either by an element with the `Null` kind, or by the following object: ``` { /* None */ "none": null } ``` This commit adds the correct processing of the latter representation to the `convert_element` function. --- src/lib_openapi/convert.ml | 49 ++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/lib_openapi/convert.ml b/src/lib_openapi/convert.ml index 5c43cb4fa216..0bd24de58f43 100644 --- a/src/lib_openapi/convert.ml +++ b/src/lib_openapi/convert.ml @@ -88,20 +88,43 @@ let rec convert_element (element : Json_schema.element) : Openapi.Schema.t = | Combine (combinator, elements) -> assert (element.enum = None) ; assert (combinator = One_of) ; - let null = - List.exists - (function - | e -> ( - match e.Json_schema.kind with Null -> true | _ -> false)) - elements - in - let elements = - List.filter - (function - | e -> ( - match e.Json_schema.kind with Null -> false | _ -> true)) - elements + (* Null elements in a [Combine] kind can either be represented with [Null] kind, + or with the following [Object]: + { /* None */ + "none": null } + Function [is_null] catches both those cases. + + FIXME: #3484 + Remove this encoding of [null] from Octez + *) + let is_null e = + let open Json_schema in + match e with + | {kind = Null; _} + | { + kind = + Object + { + properties = [("none", {kind = Null; _}, true, None)]; + pattern_properties = []; + additional_properties = None; + min_properties = 0; + max_properties = None; + schema_dependencies = []; + property_dependencies = []; + }; + title = Some "None"; + description = None; + default = None; + format = None; + enum = None; + id = None; + } -> + true + | _ -> false in + let null = List.exists is_null elements in + let elements = List.filter (fun e -> not @@ is_null e) elements in fun ?title ?description ?(nullable = false) () -> Openapi.Schema.one_of ?title -- GitLab