Skip to content

WebhookCodeScanningAlertFixedPropAlert.state typed as None | Literal["fixed"] but GitHub sends "dismissed" #276

@donicrosby

Description

@donicrosby

Description

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

state: Union[None, Literal["fixed"]] = Field(
    description="State of a code scanning alert. ..."
)

This means the field can only be None or "fixed". However, when GitHub delivers a code_scanning_alert webhook with action: "fixed" for an alert that was previously dismissed, the alert.state field is "dismissed", which causes a Pydantic ValidationError.

Expected

state should be typed as Union[None, Literal["fixed", "dismissed"]], matching what GitHub actually sends.

Error

pydantic_core._pydantic_core.ValidationError: 1 validation error for
tagged-union[...,WebhookCodeScanningAlertFixed,...]
fixed.alert.state
  Input should be 'fixed' [type=literal_error, input_value='dismissed', input_type=str]

Root Cause

This is an upstream issue in the GitHub REST API OpenAPI description. The webhook schema for code_scanning_alert (action fixed) defines alert.state with only null | "fixed", but GitHub sends "dismissed" when a previously-dismissed alert is also fixed in code.

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

Workaround

A runtime monkey-patch can be applied before any webhook parsing occurs, following the same pattern as the fixed_at workaround in #275:

from typing import Literal

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

_STATE_TYPE = None | Literal["fixed", "dismissed"]
WebhookCodeScanningAlertFixedPropAlert.__annotations__["state"] = _STATE_TYPE
WebhookCodeScanningAlertFixedPropAlert.model_fields["state"].annotation = _STATE_TYPE
WebhookCodeScanningAlertFixedPropAlert.model_rebuild(force=True)
WebhookCodeScanningAlertFixed.model_rebuild(force=True)

Note: 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. The parent model must also be rebuilt so its compiled Pydantic-core validator picks up the new child schema.

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