Skip to content

Conversation

@Leinnan
Copy link
Contributor

@Leinnan Leinnan commented Feb 27, 2025

Objective

It does not resolves issue for full support for OpenRPC for bevy_remote, but it is a first step in that direction. Connected to the #16744 issue.

Solution

  • Adds rpc.discover endpoint to the bevy_remote which follows https://spec.open-rpc.org/#openrpc-document For now in methods array only the name, which is the endpoint address is populated.
  • Moves json_schema structs into new module inside bevy_remote.

Testing

Tested the commands by running the BRP sample( cargo run --example server --features="bevy_remote") and with these curl command:

curl -X POST -d '{ "jsonrpc": "2.0", "id": 1, "method": "rpc.discover"}' 127.0.0.1:15702 | jq .

The output is:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "info": {
      "title": "Bevy Remote Protocol",
      "version": "0.16.0-dev"
    },
    "methods": [
      {
        "name": "bevy/mutate_component",
        "params": []
      },
      {
        "name": "bevy/insert",
        "params": []
      },
      {
        "name": "bevy/get",
        "params": []
      },
      {
        "name": "bevy/spawn",
        "params": []
      },
      {
        "name": "bevy/get+watch",
        "params": []
      },
      {
        "name": "bevy/destroy",
        "params": []
      },
      {
        "name": "bevy/list",
        "params": []
      },
      {
        "name": "bevy/mutate_resource",
        "params": []
      },
      {
        "name": "bevy/reparent",
        "params": []
      },
      {
        "name": "bevy/registry/schema",
        "params": []
      },
      {
        "name": "bevy/get_resource",
        "params": []
      },
      {
        "name": "bevy/query",
        "params": []
      },
      {
        "name": "bevy/remove_resource",
        "params": []
      },
      {
        "name": "rpc.discover",
        "params": []
      },
      {
        "name": "bevy/insert_resource",
        "params": []
      },
      {
        "name": "bevy/list_resources",
        "params": []
      },
      {
        "name": "bevy/remove",
        "params": []
      },
      {
        "name": "bevy/list+watch",
        "params": []
      }
    ],
    "openrpc": "1.3.2",
    "servers": [
      {
        "name": "Server",
        "url": "127.0.0.1:15702"
      }
    ]
  }
}

@Leinnan
Copy link
Contributor Author

Leinnan commented Feb 27, 2025

Could be interested in this: @mweatherley and @purplg

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Dev-Tools Tools used to debug Bevy applications. S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 27, 2025
Comment on lines +824 to +827
let servers = match (
world.get_resource::<crate::http::HostAddress>(),
world.get_resource::<crate::http::HostPort>(),
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a very minor nit, but I guess it bothers me slightly that this isn't transparent to the transport used for BRP; I think ideally in the future (e.g. when we eventually include more detailed method information in the OpenRPC document) we'd have the server info be separately registered by the transport plugin in some kind of resource in the internals.

Just a thought!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I just wasn’t confident enough in any solution to include it.

@Leinnan
Copy link
Contributor Author

Leinnan commented Mar 7, 2025

@alice-i-cecile Something missing or is it ready for merge?

@alice-i-cecile
Copy link
Member

This needs a second review: I would, but I've never worked with this flavor of thing before.

Copy link
Contributor

@villor villor left a comment

Choose a reason for hiding this comment

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

Overall happy with this kind of functionality! 🥳 Helps both with discoverability and things like generating API clients.

Curious about the use of 1.0.0-rc1 spec instead of the latest spec.

The information included for the methods is pretty sparse currently. It would be nice (maybe in a follow up) to extend it with more stuff, possibly by extending RemoteMethods to hold more metadata like:

  • Summary
  • Description
  • etc

Comment on lines 78 to 83
// /// Parameters for the RPC method
// #[serde(default)]
// pub params: Vec<Parameter>,
// /// The expected result of the method
// #[serde(skip_serializing_if = "Option::is_none")]
// pub result: Option<Parameter>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Any particular reason these were left out?
params is required in OpenRPC 1.3.2.
resultis required in OpenRPC 1.0.0-rc1.

Maybe these could be included as $refs using some of the json_schema code? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately it wasn't that easy: #16744 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, yeah it's a bit tricky that the methods are serde-based rather than bevy_reflect, but it also makes sense for usecases with non-reflectable data 🤔

There is https://github.com/GREsau/schemars which can generate JSON schemas for serde types. Not sure if we'd want to bring in another dep though.

I say include a (possibly empty) params either way though, just to be spec-compliant. There might be clients that rely on it being present.

Copy link
Contributor

@villor villor Mar 12, 2025

Choose a reason for hiding this comment

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

Another route could be to add manual schemas for the BRP types, which we already kind of do in module docs: https://dev-docs.bevyengine.org/src/bevy_remote/lib.rs.html#99

Ideally those would not be duplicated though, so if there was a way to write them in one place and have them available both through OpenRPC and in bevy docs that would be nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Argument about including params makes sense, done :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To have a full RPC Discover support the stuff you've described would be great. For the basic one I am aiming on atm what this PR provides is enough IMHO. But I would love to see it with proper params support.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fair, this is a great start! Approving now 😄

@Leinnan Leinnan requested a review from villor March 12, 2025 16:42
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 12, 2025
@alice-i-cecile
Copy link
Member

@Leinnan ping me when this is passing CI and I'll get this merged for you.

@Leinnan
Copy link
Contributor Author

Leinnan commented Mar 12, 2025

@alice-i-cecile ping ;)

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 12, 2025
Merged via the queue into bevyengine:main with commit 8f38ea3 Mar 12, 2025
31 checks passed
@Leinnan Leinnan deleted the brp_open_rpc branch March 13, 2025 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Dev-Tools Tools used to debug Bevy applications. C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants