|
4 | 4 | import logging
|
5 | 5 | import sys
|
6 | 6 |
|
| 7 | +import jsonschema.exceptions |
7 | 8 | import referencing._core
|
8 | 9 | import six
|
9 | 10 |
|
@@ -184,11 +185,23 @@ def __init__(self, **props):
|
184 | 185 | # but only for the ones that have defaults set.
|
185 | 186 | for name in self.__has_default__:
|
186 | 187 | if name not in props:
|
187 |
| - default_value = copy.deepcopy(self.__propinfo__[name]["default"]) |
| 188 | + # "defaults" could come from either the 'default' keyword or the 'const' keyword |
| 189 | + try: |
| 190 | + default_value = self.__propinfo__[name]["default"] |
| 191 | + except KeyError: |
| 192 | + try: |
| 193 | + default_value = self.__propinfo__[name]["const"] |
| 194 | + except KeyError: |
| 195 | + raise jsonschema.exceptions.SchemaError( |
| 196 | + "Schema parsing error. Expected {0} to have default or const value".format( |
| 197 | + name |
| 198 | + ) |
| 199 | + ) |
| 200 | + |
188 | 201 | logger.debug(
|
189 | 202 | util.lazy_format("Initializing '{0}' to '{1}'", name, default_value)
|
190 | 203 | )
|
191 |
| - setattr(self, name, default_value) |
| 204 | + setattr(self, name, copy.deepcopy(default_value)) |
192 | 205 |
|
193 | 206 | for prop in props:
|
194 | 207 | try:
|
@@ -626,7 +639,7 @@ def _build_literal(self, nm, clsdata):
|
626 | 639 | "__propinfo__": {
|
627 | 640 | "__literal__": clsdata,
|
628 | 641 | "__title__": clsdata.get("title"),
|
629 |
| - "__default__": clsdata.get("default"), |
| 642 | + "__default__": clsdata.get("default") or clsdata.get("const"), |
630 | 643 | }
|
631 | 644 | },
|
632 | 645 | )
|
@@ -670,6 +683,17 @@ def _build_object(self, nm, clsdata, parents, **kw):
|
670 | 683 | )
|
671 | 684 | defaults.add(prop)
|
672 | 685 |
|
| 686 | + if "const" in detail: |
| 687 | + logger.debug( |
| 688 | + util.lazy_format( |
| 689 | + "Setting const for {0}.{1} to: {2}", |
| 690 | + nm, |
| 691 | + prop, |
| 692 | + detail["const"], |
| 693 | + ) |
| 694 | + ) |
| 695 | + defaults.add(prop) |
| 696 | + |
673 | 697 | if detail.get("type", None) == "object":
|
674 | 698 | uri = "{0}/{1}_{2}".format(nm, prop, "<anonymous>")
|
675 | 699 | self.resolved[uri] = self.construct(uri, detail, (ProtocolBase,), **kw)
|
|
0 commit comments