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

Look for examples in all possible locations #2076

Merged
merged 8 commits into from
Mar 5, 2025

Conversation

zecakeh
Copy link
Contributor

@zecakeh zecakeh commented Feb 21, 2025

We used to only look for examples in a few (sometimes arbitrary) places, and we didn't support showing several examples in most cases. This is intended to fix this. In the process we try to deduplicate code to make sure that we use the same logic everywhere.

This can be reviewed commit by commit:

  1. Deduplicate code to render examples. Since there is some logic necessary for "JSONification", notably decoding < and > inside examples, it's better to keep it in a single place. We also centralize the support for non-JSON examples by using the no-highlight value.
  2. Change the json-schema/resolve-example partial to return a slice of examples, and to look also at the examples key, which is the correct key for examples in JSON Schema.
  3. Deduplicate code between requests and responses by identifying what the OpenAPI spec calls "Media Type Objects". In the process we also look in more places, like the examples key once again, given that the example key is deprecated. And we unify the order of the locations where we look, by using the full examples before we try to generate one from the examples of the fields. This produces only a few changes in the rendered spec (see below).
  4. Fix a few SS API examples that are now picked up to be rendered in the spec, that contain a room_id field although it is not in the corresponding definition.
  5. Only show empty examples if the object is empty. Fixes Empty objects and null in registration arrays #1811.

Closes #1157.

Changes in the rendered spec

The only changes are located in the SS API. Other than a few minor changes in the values of fields (like using example.org instead of matrix.org) in several examples, the 2 following examples for the /invite endpoints have significant changes:

PUT /_matrix/federation/v1/invite/{roomId}/{eventId}'s request body example:

Before:

{
  "content": {
    "membership": "invite"
  },
  "origin": "matrix.org",
  "origin_server_ts": 1234567890,
  "sender": "@someone:example.org",
  "state_key": "@joe:elsewhere.com",
  "type": "m.room.member",
  "unsigned": {
    "invite_room_state": [
      {
        "content": {
          "name": "Example Room"
        },
        "sender": "@bob:example.org",
        "state_key": "",
        "type": "m.room.name"
      },
      {
        "content": {
          "join_rule": "invite"
        },
        "sender": "@bob:example.org",
        "state_key": "",
        "type": "m.room.join_rules"
      }
    ]
  }
}

After:

{
  "content": {
    "membership": "invite"
  },
  "origin": "example.org",
  "origin_server_ts": 1549041175876,
  "sender": "@someone:example.org",
  "signatures": {
    "example.com": {
      "ed25519:key_version": "SomeSignatureHere"
    }
  },
  "state_key": "@joe:elsewhere.com",
  "type": "m.room.member"
}

PUT /_matrix/federation/v2/invite/{roomId}/{eventId}'s request body example:

Before:

{
  "event": {
    "content": {
      "membership": "invite"
    },
    "origin": "matrix.org",
    "origin_server_ts": 1234567890,
    "sender": "@someone:example.org",
    "state_key": "@joe:elsewhere.com",
    "type": "m.room.member"
  },
  "invite_room_state": [
    {
      "content": {
        "name": "Example Room"
      },
      "sender": "@bob:example.org",
      "state_key": "",
      "type": "m.room.name"
    },
    {
      "content": {
        "join_rule": "invite"
      },
      "sender": "@bob:example.org",
      "state_key": "",
      "type": "m.room.join_rules"
    }
  ],
  "room_version": "2"
}

After:

{
  "event": {
    "content": {
      "membership": "invite"
    },
    "origin": "example.org",
    "origin_server_ts": 1549041175876,
    "sender": "@someone:example.org",
    "signatures": {
      "example.com": {
        "ed25519:key_version": "SomeSignatureHere"
      }
    },
    "state_key": "@joe:elsewhere.com",
    "type": "m.room.member"
  },
  "room_version": "2"
}

In both cases, the invite_room_state is removed, and signatures is added. I believe that the old example is better so we could just remove those examples.

Pull Request Checklist

Preview: https://pr2076--matrix-spec-previews.netlify.app

That way if we fix the rendering in one place, we fix it everywhere.

Signed-off-by: Kévin Commaille <[email protected]>
These are common to requests and responses and can be found in the
`content` field.

This also adds more locations to where we can find examples and makes
sure that the order of where we look for them is consistent.

Signed-off-by: Kévin Commaille <[email protected]>
They all had a `room_id` field, which doesn't match the definition.

Signed-off-by: Kévin Commaille <[email protected]>
@zecakeh zecakeh requested a review from a team as a code owner February 21, 2025 14:37
@richvdh
Copy link
Member

richvdh commented Mar 4, 2025

In both cases, the invite_room_state is removed, and signatures is added. I believe that the old example is better so we could just remove those examples.

yes, the loss of invite_room_state isn't ideal here.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm otherwise

zecakeh added 2 commits March 4, 2025 20:15
Signed-off-by: Kévin Commaille <[email protected]>
@zecakeh
Copy link
Contributor Author

zecakeh commented Mar 4, 2025

I removed the global examples of the PUT /invite endpoints to use the ones generated from the fields, like before.

@zecakeh zecakeh requested a review from richvdh March 4, 2025 19:21
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@richvdh richvdh merged commit 3adbfa3 into matrix-org:main Mar 5, 2025
12 checks passed
@zecakeh zecakeh deleted the examples branch March 5, 2025 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants