templatetags: render JSON schema oneOf required properties (#69049)
This commit is contained in:
parent
bfa272e3d6
commit
ffa717479f
|
@ -138,7 +138,7 @@ def render_json_schema(schema):
|
|||
if 'anyOf' in schema:
|
||||
return many_of('anyOf', schema['anyOf'])
|
||||
|
||||
if 'oneOf' in schema:
|
||||
if 'oneOf' in schema and not schema.get('type') == 'object':
|
||||
return many_of('oneOf', schema['oneOf'])
|
||||
|
||||
if 'allOf' in schema:
|
||||
|
@ -193,6 +193,7 @@ def render_json_schema(schema):
|
|||
pattern_properties = schema.pop('patternProperties', {})
|
||||
required_keys = schema.pop('required', [])
|
||||
additional_properties = schema.pop('additionalProperties', True)
|
||||
one_of = schema.pop('oneOf', [])
|
||||
if unflatten:
|
||||
s += format_html(', <em class="unflatten">{}</em>', _('unflatten'))
|
||||
if merge_extra:
|
||||
|
@ -240,6 +241,8 @@ def render_json_schema(schema):
|
|||
for key in keys:
|
||||
sub = properties.get(key, {}).copy()
|
||||
render_property_schema(format_html('<tt>{0}</tt>', key), sub)
|
||||
if one_of and one_of[0].get('required'):
|
||||
s += many_of('oneOf', one_of)
|
||||
|
||||
if pattern_properties:
|
||||
s += format_html('<li><span>{0}</span>', _('Pattern properties'))
|
||||
|
@ -262,6 +265,9 @@ def render_json_schema(schema):
|
|||
enum = schema.pop('enum', [])
|
||||
if enum and not schema:
|
||||
return mark_safe(' | '.join([format_html('<tt>{}</tt>', json.dumps(el)) for el in enum]))
|
||||
required_keys = schema.pop('required', [])
|
||||
if required_keys and not schema:
|
||||
return format_html('<em>{0} {1!r}</em>', _('required'), ', '.join(required_keys))
|
||||
return format_html('<em>{0} {1!r}</em>', _('unknown validation'), original_schema)
|
||||
|
||||
|
||||
|
|
|
@ -76,3 +76,18 @@ def test_render_pattern_description():
|
|||
|
||||
schema['properties']['filename']['pattern_description'] = 'efg'
|
||||
assert 'abc' not in render_json_schema(schema) and 'efg' in render_json_schema(schema)
|
||||
|
||||
|
||||
def test_render_oneof_property_required():
|
||||
schema = {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'a': {'type': 'string'},
|
||||
'b': {'type': 'string'},
|
||||
},
|
||||
'oneOf': [
|
||||
{'required': ['a']},
|
||||
{'required': ['b']},
|
||||
],
|
||||
}
|
||||
assert "<b>oneOf</b> [ <em>required 'a'</em> | <em>required 'b'</em> ]" in render_json_schema(schema)
|
||||
|
|
Loading…
Reference in New Issue