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

Add OpenRPC schema generator #56

Merged
merged 4 commits into from
Oct 17, 2023
Merged

Add OpenRPC schema generator #56

merged 4 commits into from
Oct 17, 2023

Conversation

qweeze
Copy link
Contributor

@qweeze qweeze commented Oct 14, 2023

Closes #54

@smagafurov @spumer Please have a look. I'll add some tests if this looks good enough to you

'code': error.CODE,
'message': error.MESSAGE,
}
for error in route.errors
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about more complicated error schema? OpenRPC allow use extra fields except code and message?

Right now fastapi-jsonrpc support custom ErrorModel which allow you extend jsonrpc error results and this custom schema is visible in openapi (Swagger)

Copy link
Collaborator

@spumer spumer Oct 14, 2023

Choose a reason for hiding this comment

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

Yep, https://spec.open-rpc.org/#error-object allow custom data too

BaseError (item of route.errors list) have a get_data_model() method. You can use it to pass correct schema into openRPC data part

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, custom errors' schemas are now supported 818c158

@spumer spumer mentioned this pull request Oct 14, 2023
@spumer
Copy link
Collaborator

spumer commented Oct 15, 2023

LGTM, other minor improvements like cache and route override/customize schema we can do later.

You can start make tests 🔥
Thank you!

@qweeze
Copy link
Contributor Author

qweeze commented Oct 15, 2023

All right, now we have some tests f9cb6cb
Let me know if you'd like to add any other test cases

@spumer
Copy link
Collaborator

spumer commented Oct 15, 2023

All right, now we have some tests f9cb6cb Let me know if you'd like to add any other test cases

Is there support for examples in openRPC?

I mean if we have two error with the same code like (6001, ValidationError) but have different data field. It's possible include this into openRPC?

@qweeze
Copy link
Contributor Author

qweeze commented Oct 15, 2023

Is there support for examples in openRPC?

There is, but it's meant to be used only for params and result values. As for errors, the only place to store them is errors array inside method objects, and they're required to be unique. Spec explicitly states that The Errors MUST have unique error codes.

At the same time, error.data field is not typed, so technically it's OK to "merge" such errors without abusing the spec like this:

{
    "error": 6000,
    "message": "ValidationError",
    "data": {
         "anyOf": [
            {"title": "MyError.Data", "type": "object", "properties": {"details": {"title": "Details", "type": "string"}}, "required": ["details"]},
            {"title": "MyError.Data", "type": "string"}
          ]
    }
}

@spumer
Copy link
Collaborator

spumer commented Oct 16, 2023

Can you make it? But i think current implementation of openRPC playground does not supported it.

Other things looks good for me

@qweeze
Copy link
Contributor Author

qweeze commented Oct 16, 2023

Can you make it?

I had to move errors' defs into components.errors, this way tools can resolve $refs in error body. Anyway, data schemes of same-coded errors are now joined with anyOf, I added a test for this case here

@spumer spumer merged commit c1cd600 into smagafurov:master Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support openrpc
2 participants