Skip to content

WebhookCodeScanningAlertFixedPropAlert.fixed_at typed as Missing[None] but GitHub sends a datetime string #275

@donicrosby

Description

@donicrosby

Description

The generated model WebhookCodeScanningAlertFixedPropAlert (in githubkit/versions/v2022_11_28/models/group_0546.py) defines the fixed_at field as:

fixed_at: Missing[None] = Field(
    default=UNSET,
    description="The time that the alert was fixed in ISO 8601 format: `YYYY-MM-DDTHH:MM:SSZ`.",
)

This means the field can only be UNSET or None. However, when GitHub delivers a code_scanning_alert webhook with action: "fixed", the alert.fixed_at field contains an ISO 8601 datetime string (e.g. "2025-06-15T10:30:00Z"), which causes a Pydantic ValidationError.

Expected

fixed_at should be typed as Missing[Union[datetime, None]] (or equivalently Missing[Optional[datetime]]), consistent with how created_at and dismissed_at are typed on the same model.

Root Cause

This is an upstream issue in the GitHub REST API OpenAPI description. The webhook schema for code_scanning_alert (action fixed) defines fixed_at with only type: null.

I've filed a Schema Inaccuracy issue upstream: github/rest-api-description#6058

Workaround

A runtime monkey-patch can be applied before any webhook parsing occurs. Both __annotations__ and model_fields must be patched because the source uses from __future__ import annotations (so model_rebuild re-resolves from the string annotation), and the core schema generator reads model_fields. The parent model must also be rebuilt so its compiled Pydantic-core validator picks up the new child schema:

from datetime import datetime
from githubkit.typing import Missing
from githubkit.versions.latest.models import (
    WebhookCodeScanningAlertFixed,
    WebhookCodeScanningAlertFixedPropAlert,
)

_FIXED_AT_TYPE = Missing[datetime]
WebhookCodeScanningAlertFixedPropAlert.__annotations__["fixed_at"] = _FIXED_AT_TYPE
WebhookCodeScanningAlertFixedPropAlert.model_fields["fixed_at"].annotation = _FIXED_AT_TYPE
WebhookCodeScanningAlertFixedPropAlert.model_rebuild(force=True)
WebhookCodeScanningAlertFixed.model_rebuild(force=True)

Note: patching only model_fields (without __annotations__) does not work — model_rebuild(force=True) re-resolves from the string annotation and overwrites the change.

Environment

  • githubkit version: 0.14.5
  • Python: 3.12
  • Pydantic: v2

Metadata

Metadata

Assignees

No one assigned

    Labels

    WebHookbugSomething isn't workingschemaschema related

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions