-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[Rust Server] Support OpenAPI v3 callbacks #5405
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
Merged
richardwhiuk
merged 8 commits into
OpenAPITools:5.0.x
from
Metaswitch:rust-server-callbacks
Mar 7, 2020
Merged
[Rust Server] Support OpenAPI v3 callbacks #5405
richardwhiuk
merged 8 commits into
OpenAPITools:5.0.x
from
Metaswitch:rust-server-callbacks
Mar 7, 2020
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I'll leave this open for a short while longer in case there's any feedback, as it's quite a large change. |
BitRise: Swift 4 tests failed None of these are interesting. |
alexsuperdev
added a commit
to alexsuperdev/openapi-generator
that referenced
this pull request
Mar 15, 2020
catch NPE if no OneOf Schemas is setted. merged from 5.0 branch [Rust Server] Support header objects (OpenAPITools#5337) [Rust Server] Support header objects Support operations with objects in the header [Rust Server] Support objects as query parameters (OpenAPITools#5338) - Support objects as query parameters - Update samples [Rust Server] Add support for untyped properties and models (OpenAPITools#5339) * [Rust Server] Add support for untyped properties * [Rust Server] Improve support for untyped data * Update samples [Rust Server] Support RFC 7807 (OpenAPITools#5407) * Support RFC 7807 - Problem Details for HTTP APIs * Add test for RFC 7807 * Update samples [Rust Server] Nullable fixes (OpenAPITools#5408) * [Rust Server] Nullable fixes * [Rust Server] Add tests for nullable cases * Update samples [Rust Server] Handle numeric response description (OpenAPITools#5452) * [Rust Server] Handle response descriptions which start with a number. * [Rust Server] Add test for numeric response descriptions * Update samples [Rust Server] Support numeric operation IDs (OpenAPITools#5453) * [Rust Server] Support operation IDs which begin with a number * [Rust Server] Add test for a numeric operation ID * Update samples [Rust Server] Support RFC 7386 (OpenAPITools#5454) * [Rust Server] Support RFC 7386 Support application/merge-patch+json as defined by RFC 7386 - https://tools.ietf.org/html/rfc7386 Handle exactly the same as application/json. * [Rust Server] Add test for RFC 7386 * Update samples [Rust Server] Suffix reserved words with _ (OpenAPITools#5455) * [Rust Server] Suffix reserved words with _ Suffix reserved words with an underscore instead of prefixing them. This follows convention in the Rust community, as prefixing with an underscore indicates an unused variable in Rust. * Update samples [Rust Server] Don't change API version (OpenAPITools#5458) Don't change the API version which is exposed in `crate::API_VERSION`. - If the API version isn't set, we don't expose it. - If it is set, we leave it well alone. - Always pass a package version: - Pass in the passed package version by preference - Pass in the API version if it's not. - If the API version isn't set, we use 1.0.0 - If it is set, and isn't a valid semver, we append .0 such that we have something with three digits (so that an API version of 1 works). [Rust Server] Support OpenAPI v3 callbacks (OpenAPITools#5405) * [Rust Server] Support Callbacks * [Rust Server] Support callbacks in the examples * [Rust Server] Update features documentation * [Rust Server] Mark as supporting callbacks * Update samples * [Rust Server] Add tests for callbacks * [Rust Server] Fix README Don't suggest examples which don't exist [Rust Server] Test allOf objects including base properties (OpenAPITools#5457) * [Rust Server] Add operationIds for rust-server-test * [Rust Server] Add test for allOf * Update samples [Rust Server] Make parse error Display-able (OpenAPITools#5490) * [Rust Server] Make parse error displayable Change error type to be displayable to prevent compile errors * [Rust Server] Add test for enum in path * Update samples
Open
6 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds support for callbacks to Rust Server. This is a reasonable substantial change to the templates - mainly to prevent duplication between the server and client cases.
This work was done by myself on behalf of @Metaswitch, and has been reviewed, tested and fixed by others @Metaswitch.
Design
The key change that callbacks make is that the server now needs to act as a client (in order to send callbacks), and the client needs to act as a server (in order to receive callbacks).
As such, we modify the generator to produce a
api::server::callbacks
and aapi::client::callbacks
module. These are only generated if the API uses callbacks (to avoid making non callback using API code more complex).api::server::callbacks
provides a Client which implements all of the callback APIs from a server perspective. This is largely similar in nature toapi::client
.api::client::callbacks
provides a Service which implements all of the callback APIs from a client perspective. This provides a Service which can be implemented by the client. This is largely identical to api::server.Implementation
Low level details of what's happened:
Rust changes:
We now generate two different Path Set Maps, which contain the paths to generate - one for the callbacks, and one for the general set.
We recursively postProcessModelsWithOperations, so that we can add metadata to the operations in callbacks.
We add the callback mustache files if any of the operations have callbacks. We also annotate the API with whether it has callbacks, and include the path set map.
Mustache template composition is used extensively.
mimetype.rs
is now formed frommimetype.mustache
,mimetype-request.mustache
andmimetype-response.mustache
.This pulls out the logic for generating mimetypes for a request/response into individual files, which can then be included per request/response.
This is exploited to generate mimetypes for callback operations, without duplication.
lib.rs
is formed fromlib.mustache
, andresponse.mustache
which contains the common definition for a Response type.client/mod.rs
is now formed from:client-mod.mustache
the base, as beforeclient-import.mustache
which contains imports common to the clients.client-api.mustache
which defines how a Client implements an OpenAPI operationserver/mod.rs
is now formed from:server-mod.mustache
the base, as beforeserver-imports.mustache
which contains imports common to the servers.server-paths.mustache
which determines the regular expressions used to match the operationsserver-make-service.mustache
which the MakeService definitionserver-service-header.mustache
which contains the Service definition, and implementation preludeserver-service-footer.mustache
which contains the Service implementation endingserver-operation.mustache
which provides the implementation of an OpenAPI operationclient/callbacks.rs
is new, and uses the same skeleton asserver/mod.rs
, with most of the same includes (as this contains the client's server).server/callbacks.rs
is new, and uses the same skeleton asclient/mod.rs
.A similar task is solved in the examples as well:
examples/server/server.rs
consists of:examples-server-server.mustache
- the base structureexamples-server-common.mustache
- the common definition and creation codeexamples-server-api.mustache
- which provides the dummy implementation of an OpenAPI operationexample/client/server.rs
is the client's callback server, and uses the same core set of templates asexamples/server/server.rs
.We restructure the examples to use Rust's recommend model for multi-file examples:
We move the examples to use
log
andenv_logger
instead of justprintln!
We fix a bug where the examples README suggests examples exist, even if we failed to generate code for them.
Limitations
We make no attempt to support nested callbacks (i.e. callbacks of callbacks). This would be a further enhancement if there's desire to support this.
Currently we provide a single callback API, and require the implementor to implement all the functions.
Rust Server Technical Committee
PR checklist
./bin/
(or Windows batch scripts under.\bin\windows
) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run./bin/{LANG}-petstore.sh
,./bin/openapi3/{LANG}-petstore.sh
if updating the code or mustache templates for a language ({LANG}
) (e.g. php, ruby, python, etc).master
,4.3.x
,5.0.x
. Default:master
.