Skip to content

Support "unevaluatedProperties" #2252

Open
@evalott100

Description

I have a jsonschema

{
    "title": "resource",
    "type": "object",
    "allOf": [
        { "$ref": "#/$defs/PartialResource" }
    ],
    "$defs": {
        "PartialResource": {
            "title": "partial_resource",
            "properties": {
                "spec": {
                    "title": "spec",
                    "type": "string"
                },
                "resource_kwargs": {
                    "title": "resource_kwargs",
                    "type": "object"
                },
                "resource_path": {
                    "title": "resource_path",
                    "type": "string"
                },
                "root": {
                    "title": "root",
                    "type": "string"
                },
                "uid": {
                    "title": "uid",
                    "type": "string"
                }
            },
            "required": [
                "resource_kwargs",
                "resource_path",
                "root",
                "spec",
                "uid"
            ]
        }
    },
    "properties": {
        "path_semantics": {
            "title": "path_semantics",
            "type": "string",
            "enum": [
                "posix",
                "windows"
            ]
        },
        "run_start": {
            "title": "run_start",
            "type": "string"
        }
    },
    "unevaluatedProperties": false
}

which generates a BaseModel:

class PartialResource(BaseModel):
    spec: str = Field(..., title="spec")
    resource_kwargs: Dict[str, Any] = Field(..., title="resource_kwargs")
    resource_path: str = Field(..., title="resource_path")
    root: str = Field(..., title="root")
    uid: str = Field(..., title="uid")


class Resource(PartialResource):
    path_semantics: Optional[PathSemantics] = Field(None, title="path_semantics")
    run_start: Optional[str] = Field(None, title="run_start")

The "unevaluatedProperties" in the schema works in the same way as "additionalProperties", but allows the elements declared in subschema.

For analogous behaviour in the generated BaseModel, we could change

def _get_config_extra(self) -> Optional[Literal["'allow'", "'forbid'"]]:
additionalProperties = self.extra_template_data.get('additionalProperties')
allow_extra_fields = self.extra_template_data.get('allow_extra_fields')
if additionalProperties is not None or allow_extra_fields:
return (
"'allow'" if additionalProperties or allow_extra_fields else "'forbid'"
)
return None

to also check for "unevaluatedProperties": false and use "forbid".

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions