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

Support "unevaluatedProperties" #2252

Open
evalott100 opened this issue Jan 9, 2025 · 0 comments
Open

Support "unevaluatedProperties" #2252

evalott100 opened this issue Jan 9, 2025 · 0 comments

Comments

@evalott100
Copy link

evalott100 commented Jan 9, 2025

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant