Merge pull request #1503 from QMatrixClient/support_oneOf
Support oneOf
This commit is contained in:
commit
669d526ea7
2 changed files with 38 additions and 1 deletions
|
@ -21,3 +21,25 @@ Example:
|
|||
name: fields...
|
||||
type: string
|
||||
```
|
||||
|
||||
## Using oneOf to provide type alternatives
|
||||
|
||||
<!-- TODO: Remove this section after upgrading to OpenAPI v3 -->
|
||||
|
||||
`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:
|
||||
...
|
||||
```
|
||||
|
|
|
@ -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,19 @@ 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'])
|
||||
# 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:
|
||||
prop_title = prop_type
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue