dataclasses-jsonschema icon indicating copy to clipboard operation
dataclasses-jsonschema copied to clipboard

Add support for constant fields

Open s-knibbs opened this issue 5 years ago • 1 comments

In #62 basic support for 'Final' types was added. However the following form, which defines a constant vaued field is not currently supported:


@dataclass
class Rectangle(JsonSchemaMixin):
    max_width: Final = 20
    x: int
    y: int

This could generate one of the following schemas for the max_width field:

Draft 04:

{"enum": [20]}

Draft 06:

{"const": 20}

s-knibbs avatar Jun 29 '19 13:06 s-knibbs

A workaround that seems to work for me (my use case is demarshalling based on string types, so YMMV). The __post_init__ code may not be necessary, or maybe you want to go the other way (convert from enum to int, although the enum inherits from int so it should not be necessary).

class Constant(dict):
    def __getitem__(self, value):
        if not value in self:
            typ = type(value)
            self[value] = Enum(f'<Constant_{typ.__name__}:{value}>', type=typ, names=[('value', value)])
        return dict.__getitem__(self, value)
Constant = Constant()

@dataclass
class Rectangle(JsonSchemaMixin):
    max_width: Constant[20] = Constant[20].value
    x: int
    y: int
    def __post_init__(self):
        self.max_width = Constant[20](self.max_width)

This generates {"type": "integer", "enum": [20], "default": 20} which looks right.

jobh avatar Aug 21 '20 13:08 jobh