From 88d97b06ae5cd7816f4297e67e4701e4c2e8af82 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 28 Jul 2018 22:54:59 +0900 Subject: [PATCH 1/3] Support oneOf --- scripts/templating/matrix_templates/units.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/scripts/templating/matrix_templates/units.py b/scripts/templating/matrix_templates/units.py index 88f7b86c..5930b0d0 100644 --- a/scripts/templating/matrix_templates/units.py +++ b/scripts/templating/matrix_templates/units.py @@ -286,7 +286,9 @@ def get_json_schema_object_fields(obj, enforce_title=False): def process_data_type(prop, required=False, enforce_title=True): prop = inherit_parents(prop) - prop_type = prop['type'] + prop_type = prop.get('oneOf', prop.get('type', [])) + assert prop_type + tables = [] enum_desc = None is_object = False @@ -317,6 +319,15 @@ def process_data_type(prop, required=False, enforce_title=True): tables = nested["tables"] enum_desc = nested["enum_desc"] + elif isinstance(prop_type, list): + prop_title = [] + for t in prop_type: + if isinstance(t, dict): + nested = process_data_type(t) + tables.extend(nested['tables']) + prop_title.append(nested['title']) + else: + prop_title.append(t) else: prop_title = prop_type From 9be78f3aa7f6b7577aef6891c5d47760203107da Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 29 Jul 2018 14:58:37 +0900 Subject: [PATCH 2/3] Better support enums when they come among oneOf types --- scripts/templating/matrix_templates/units.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/templating/matrix_templates/units.py b/scripts/templating/matrix_templates/units.py index 5930b0d0..90a87cd4 100644 --- a/scripts/templating/matrix_templates/units.py +++ b/scripts/templating/matrix_templates/units.py @@ -326,6 +326,10 @@ def process_data_type(prop, required=False, enforce_title=True): nested = process_data_type(t) tables.extend(nested['tables']) prop_title.append(nested['title']) + # Assuming there's at most one enum among type options + enum_desc = nested['enum_desc'] + if enum_desc: + enum_desc = "%s if the type is enum" % enum_desc else: prop_title.append(t) else: From b41eb26124de0f5c6b54fba4a9cb3939fb10adfe Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 12 Aug 2018 16:52:07 +0900 Subject: [PATCH 3/3] Document OpenAPI extension Signed-off-by: Alexey Rusakov --- api/openapi_extensions.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/api/openapi_extensions.md b/api/openapi_extensions.md index 9f4745fd..339452ba 100644 --- a/api/openapi_extensions.md +++ b/api/openapi_extensions.md @@ -21,3 +21,25 @@ Example: name: fields... type: string ``` + +## Using oneOf to provide type alternatives + + + +`oneOf` (available in JSON Schema and Swagger/OpenAPI v3 but not in v2) +is used in cases when a simpler type specification as a list of types +doesn't work, as in the following example: +``` + properties: + old: # compliant with old Swagger + type: + - string + - object # Cannot specify a schema here + new: # uses oneOf extension + oneOf: + - type: string + - type: object + title: CustomSchemaForTheWin + properties: + ... +```