ion232 / lichess-api

A Rust API client for lichess.org
https://lichess.org/api
Apache License 2.0
9 stars 7 forks source link

Bug: `compat` field in board event stream ignored / wrongly parsed #72

Open rubdos opened 13 hours ago

rubdos commented 13 hours ago

As also stated in https://github.com/lichess-org/api/issues/243, the docs don't agree with the actual implementation of the compat field of the event stream on the board api.

This is an actual event on the stream:

{
  "type": "challenge",
  "challenge": {
    "id": "kObNNa13",
    "url": "https://lichess.org/kObNNa13",
    "status": "created",
    "challenger": {
      "id": "skogruben",
      "name": "SKOGruben",
      "rating": 1185,
      "title": null,
      "online": true,
      "lag": 4
    },
    "destUser": {
      "id": "battlingthebots",
      "name": "BattlingTheBots",
      "rating": 1500,
      "title": null,
      "provisional": true
    },
    "variant": {
      "key": "standard",
      "name": "Standard",
      "short": "Std"
    },
    "rated": false,
    "speed": "bullet",
    "timeControl": {
      "type": "clock",
      "limit": 120,
      "increment": 1,
      "show": "2+1"
    },
    "color": "random",
    "finalColor": "black",
    "perf": {
      "icon": "",
      "name": "Bullet"
    }
  },
  "compat": {
    "bot": false,
    "board": false
  }
}

The current API implementation expects the compat field on the challenge, but it is directly part of the event. I'm not sure how to correctly expose the field, since the API currently uses a generic nd-json parser that expects a type + field, without any extras (afaict).

ion232 commented 11 hours ago

Thanks for flagging this. The following may work. Needs testing though.

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Event {
    #[serde(flatten)]
    pub event: InnerEvent,
    pub compat: Compat,
}

// Same as previous event.
pub struct InnerEvent
rubdos commented 9 hours ago

Observations:

  1. That approach breaks the API (match on event should now have arms with InnerEvent types).
  2. Simply applying that patch still has the compat field in the ChallengeJson; should that get removed? It's not in the docs any more anyway.
  3. The compat field needs to be Optional; it's apparently not in the GameStart events: there it's actually in the game field...
  4. The approach works :-) I'll make a PR.