diff --git a/ironic/api/controllers/v1/utils.py b/ironic/api/controllers/v1/utils.py index 04525ff651..8de2d156df 100644 --- a/ironic/api/controllers/v1/utils.py +++ b/ironic/api/controllers/v1/utils.py @@ -86,11 +86,13 @@ STANDARD_TRAITS = os_traits.get_traits() CUSTOM_TRAIT_PATTERN = "^%s[A-Z0-9_]+$" % os_traits.CUSTOM_NAMESPACE CUSTOM_TRAIT_REGEX = re.compile(CUSTOM_TRAIT_PATTERN) -TRAITS_SCHEMA = {'anyOf': [ - {'type': 'string', 'minLength': 1, 'maxLength': 255, - 'pattern': CUSTOM_TRAIT_PATTERN}, - {'type': 'string', 'enum': STANDARD_TRAITS}, -]} +TRAITS_SCHEMA = { + 'type': 'string', 'minLength': 1, 'maxLength': 255, + 'anyOf': [ + {'pattern': CUSTOM_TRAIT_PATTERN}, + {'enum': STANDARD_TRAITS}, + ] +} LOCAL_LINK_BASE_SCHEMA = { 'type': 'object', diff --git a/ironic/common/args.py b/ironic/common/args.py index 94cfe8841b..bd13e3eafd 100755 --- a/ironic/common/args.py +++ b/ironic/common/args.py @@ -211,12 +211,17 @@ def _validate_schema(name, value, schema): try: jsonschema.validate(value, schema) except jsonschema.exceptions.ValidationError as e: - - # The error message includes the whole schema which can be very - # large and unhelpful, so truncate it to be brief and useful - error_msg = ' '.join(str(e).split("\n")[:3])[:-1] - raise exception.InvalidParameterValue( - _('Schema error for %s: %s') % (name, error_msg)) + error_msg = _('Schema error for %s: %s') % (name, e.message) + # Sometimes the root message is too generic, try to find a possible + # root cause: + cause = None + current = e + while current.context: + current = jsonschema.exceptions.best_match(current.context) + cause = current.message + if cause is not None: + error_msg += _('. Possible root cause: %s') % cause + raise exception.InvalidParameterValue(error_msg) return value diff --git a/releasenotes/notes/jsonschema-4.8-1146d103b877cffd.yaml b/releasenotes/notes/jsonschema-4.8-1146d103b877cffd.yaml new file mode 100644 index 0000000000..75c0a6c507 --- /dev/null +++ b/releasenotes/notes/jsonschema-4.8-1146d103b877cffd.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixes API error messages with jsonschema>=4.8. A possible root cause is + now detected for generic schema errors.