open-rpc / spec

The OpenRPC specification
https://spec.open-rpc.org
Apache License 2.0
168 stars 52 forks source link

Clarify oneOf usage if any #278

Closed antoine-mulet closed 4 years ago

antoine-mulet commented 4 years ago

I have been struggling to understand how oneOf can be used within params since it is mentioned in the documentation https://spec.open-rpc.org/#method-object. I have played a bit with the playground as well and the only way I found to make use of oneOf was inside the schema, which is standard json schema. Am I missing something?

I found issue #193 which was auto closed so I am wondering whether oneOf was somehow supported in the past but it's not anymore? If that's the case the documentation should probably be updated.

As a side note I can see a use case where one could want to use oneOf outside the schema. This would be to define some sort of polymorphic group of top level params e.g.

"params" :  { "oneOf" :[
        { "$ref": "#/components/schemas/Foo" },
        { "$ref": "#/components/schemas/Bar" } 
      ]
}

This is clearly not compatible with params being of type array so not sure the added complexity is worth the benefits but I am currently migrating an API to OpenRPC and I found myself in the need of doing something like this so maybe others would too.

This limitation is easily overcome by introducing another level of nesting as follow but it ultimately changes what your API looks like

"params" :  [
        { "name": "p", "schema": { "type" : "object", "oneOf": [
               { "$ref": "#/components/schemas/Foo" } ,
               {  "$ref": "#/components/schemas/Bar" }
      ]}
}

I hope it makes sense.

github-actions[bot] commented 4 years ago

Welcome to OpenRPC! Thank you for taking the time to create an issue. Please review the guidelines

georgeconstantinou commented 4 years ago

Having the same issue as described by @antoine-mulet.

BelfordZ commented 4 years ago

@antoine-mulet @georgeconstantinou Thank you both for raising this issue. It's something we experimented with early on and dropped, but as pointed out, we missed a reference to it in the spec. Anything referring to oneOf should be removed. Polymorphism as described should be pushed down to the JSON schema, since it already fully supports oneOf.

PRs are very welcomed. I'll make one later on today though.

BelfordZ commented 4 years ago

@antoine-mulet since it's ultimately your contribution, if you want to copy the PR and re-open, I'm happy to merge yours.

antoine-mulet commented 4 years ago

@BelfordZ Thanks for the clarification and your great work! My PR is all yours.

openrpc-bastion commented 4 years ago

:tada: This issue has been resolved in version 1.2.6 :tada:

The release is available on:

Your semantic-release bot :package::rocket: