Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix #281 #284

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions python_jsonschema_objects/classbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import sys

import jsonschema.exceptions
import referencing._core
import six

Expand Down Expand Up @@ -184,11 +185,23 @@ def __init__(self, **props):
# but only for the ones that have defaults set.
for name in self.__has_default__:
if name not in props:
default_value = copy.deepcopy(self.__propinfo__[name]["default"])
# "defaults" could come from either the 'default' keyword or the 'const' keyword
try:
default_value = self.__propinfo__[name]["default"]
except KeyError:
try:
default_value = self.__propinfo__[name]["const"]
except KeyError:
raise jsonschema.exceptions.SchemaError(
"Schema parsing error. Expected {0} to have default or const value".format(
name
)
)

logger.debug(
util.lazy_format("Initializing '{0}' to '{1}'", name, default_value)
)
setattr(self, name, default_value)
setattr(self, name, copy.deepcopy(default_value))

for prop in props:
try:
Expand Down Expand Up @@ -626,7 +639,7 @@ def _build_literal(self, nm, clsdata):
"__propinfo__": {
"__literal__": clsdata,
"__title__": clsdata.get("title"),
"__default__": clsdata.get("default"),
"__default__": clsdata.get("default") or clsdata.get("const"),
}
},
)
Expand Down Expand Up @@ -670,6 +683,17 @@ def _build_object(self, nm, clsdata, parents, **kw):
)
defaults.add(prop)

if "const" in detail:
logger.debug(
util.lazy_format(
"Setting const for {0}.{1} to: {2}",
nm,
prop,
detail["const"],
)
)
defaults.add(prop)

if detail.get("type", None) == "object":
uri = "{0}/{1}_{2}".format(nm, prop, "<anonymous>")
self.resolved[uri] = self.construct(uri, detail, (ProtocolBase,), **kw)
Expand Down Expand Up @@ -765,7 +789,10 @@ def _build_object(self, nm, clsdata, parents, **kw):
props["__prop_names__"] = name_translation

props["__propinfo__"] = properties
required = set.union(*[p.__required__ for p in parents])
if len(parents):
required = set.union(*[p.__required__ for p in parents])
else:
required = set()

if "required" in clsdata:
for prop in clsdata["required"]:
Expand Down
8 changes: 8 additions & 0 deletions python_jsonschema_objects/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def __init__(self, value, typ=None):

self.validate()

constval = self.const()
if constval is not None:
self._value = constval

def as_dict(self):
return self.for_json()

Expand All @@ -54,6 +58,10 @@ def for_json(self):
def default(cls):
return cls.__propinfo__.get("__default__")

@classmethod
def const(cls):
return cls.__propinfo__.get("__literal__", {}).get("const", None)

@classmethod
def propinfo(cls, propname):
if propname not in cls.__propinfo__:
Expand Down
6 changes: 6 additions & 0 deletions python_jsonschema_objects/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def enum(param, value, _):
raise ValidationError("{0} is not one of {1}".format(value, param))


@registry.register()
def const(param, value, _):
if value != param:
raise ValidationError("{0} is not constant {1}".format(value, param))


@registry.register()
def minimum(param, value, type_data):
exclusive = type_data.get("exclusiveMinimum")
Expand Down
Loading
Loading