Skip to content

Commit

Permalink
Improve validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Jun 19, 2024
1 parent 3e641a5 commit f853575
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 16 additions & 2 deletions pulp-glue/pulp_glue/common/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def validate_schema(self, schema: t.Any, name: str, value: t.Any) -> t.Any:
if value is None and schema.get("nullable", False):
return None

schema_type = schema.get("type", "string")
schema_type = schema.get("type")
allOf = schema.get("allOf")
anyOf = schema.get("anyOf")
oneOf = schema.get("oneOf")
Expand Down Expand Up @@ -343,7 +343,7 @@ def validate_schema(self, schema: t.Any, name: str, value: t.Any) -> t.Any:
found_valid = True
if not found_valid:
raise OpenAPIValidationError(
_("No schema in anyOf validated for {name}.").format(name=name)
_("No schema in oneOf validated for {name}.").format(name=name)
)
elif not_schema:
try:
Expand All @@ -354,17 +354,31 @@ def validate_schema(self, schema: t.Any, name: str, value: t.Any) -> t.Any:
raise OpenAPIValidationError(
_("Forbidden schema for {name} validated.").format(name=name)
)
elif schema_type is None:
# Schema type is not specified.
# JSONField
pass
elif schema_type == "object":
# Serializer
value = self.validate_object(schema, name, value)
elif schema_type == "array":
# ListField
value = self.validate_array(schema, name, value)
elif schema_type == "string":
# CharField
# TextField
# DateTimeField etc.
# ChoiceField
# FielField (binary data)
value = self.validate_string(schema, name, value)
elif schema_type == "integer":
# IntegerField
value = self.validate_integer(schema, name, value)
elif schema_type == "number":
# FloatField
value = self.validate_number(schema, name, value)
elif schema_type == "boolean":
# BooleanField
if not isinstance(value, bool):
raise OpenAPIValidationError(
_("'{name}' is expected to be a boolean.").format(name=name)
Expand Down
12 changes: 9 additions & 3 deletions pulp-glue/pulp_glue/rpm/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@
PulpRepositoryContext,
PulpRepositoryVersionContext,
PulpViewSetContext,
api_quirk,
)
from pulp_glue.common.i18n import get_translation

translation = get_translation(__package__)
_ = translation.gettext


@api_quirk(PluginRequirement("core", specifier="<3.55.0"))
def patch_rpm_copy_any_type(api: OpenAPI) -> None:
# Oder versions of spectacular did not declare JSONField properly.
rpm_copy_serializer = api.api_spec["components"]["schemas"]["Copy"]
rpm_copy_serializer["properties"]["config"].pop("type", None)


class PulpRpmACSContext(PulpACSContext):
PLUGIN = "rpm"
RESOURCE_TYPE = "rpm"
Expand Down Expand Up @@ -389,6 +397,4 @@ def copy(
"config": config,
"dependency_solving": dependency_solving,
}
# Validation fails because config is not a dict(), and
# drf-spectacular thinks all type:object refs must be dict
return self.call("copy", body=body, validate_body=False)
return self.call("copy", body=body)

0 comments on commit f853575

Please sign in to comment.