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

Forward merge v5 fixes #143

Merged
merged 15 commits into from
Mar 18, 2021
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ branches:
- staging
- trying
- master
- v5
deploy:
- provider: script
script: bash deploy.sh
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Upgrade bytes to 1.0
- Upgrade tokio to 1.0

## [5.1.0] - 2021-03-04
### Added
- Support conversion for AnyOf and OneOf
- Expose CompositeMakeServiceEntry

## [5.0.2] - 2021-01-13
### Fixed
- Fix off by one error declaring OneOf and AnyOf with more than 10 arguments
Expand Down Expand Up @@ -175,6 +180,7 @@ No changes. We now think we've got enough to declare this crate stable.

[Unreleased]: https://github.com/Metaswitch/swagger-rs/compare/6.0.0-alpha.1...HEAD
[6.0.0-alpha.1]: https://github.com/Metaswitch/swagger-rs/compare/5.0.2...6.0.0-alpha.1
[5.1.0]: https://github.com/Metaswitch/swagger-rs/compare/5.0.2...5.1.0
[5.0.2]: https://github.com/Metaswitch/swagger-rs/compare/5.0.1...5.0.2
[5.0.1]: https://github.com/Metaswitch/swagger-rs/compare/5.0.0...5.0.1
[5.0.0]: https://github.com/Metaswitch/swagger-rs/compare/4.0.2...5.0.0
Expand Down
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ http2 = ["hyper/http2"]
client = ["hyper/client"]
tcp = ["hyper/tcp"]
tls = ["native-tls", "openssl", "hyper-openssl", "hyper-tls"]
conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-enum-derive"]

[dependencies]
base64 = "0.13"
Expand All @@ -36,6 +37,13 @@ hyper-old-types = "0.11.0"
chrono = "0.4.6"
futures = "0.3"

# Conversion
frunk = { version = "0.3.0", optional = true }
frunk_derives = { version = "0.3.0", optional = true }
frunk_core = { version = "0.3.0", optional = true }
frunk-enum-derive = { version = "0.2.1", optional = true }
frunk-enum-core = { version = "0.2.1", optional = true }

# multipart/form-data
mime = { version = "0.3", optional = true }

Expand Down
11 changes: 9 additions & 2 deletions src/composites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,17 @@ type CompositeServiceVec<ReqBody, ResBody, Error> = Vec<(
Box<dyn CompositedService<ReqBody, ResBody, Error> + Send>,
)>;

type CompositeMakeServiceVec<Target, ReqBody, ResBody, Error, MakeError> = Vec<(
type CompositeMakeServiceVec<Target, ReqBody, ResBody, Error, MakeError> =
Vec<CompositeMakeServiceEntry<Target, ReqBody, ResBody, Error, MakeError>>;

/// Service which can be composited with other services as part of a CompositeMakeService
///
/// Consists of a base path for requests which should be handled by this service, and a boxed
/// MakeService.
pub type CompositeMakeServiceEntry<Target, ReqBody, ResBody, Error, MakeError> = (
&'static str,
Box<dyn CompositedMakeService<Target, ReqBody, ResBody, Error, MakeError> + Send>,
)>;
);

/// Wraps a vector of pairs, each consisting of a base path as a `&'static str`
/// and a `MakeService` instance. Implements `Deref<Vec>` and `DerefMut<Vec>` so
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use connector::Connector;
#[cfg(all(feature = "server", any(feature = "http1", feature = "http2")))]
pub mod composites;
#[cfg(all(feature = "server", any(feature = "http1", feature = "http2")))]
pub use composites::{CompositeMakeService, CompositeService, NotFound};
pub use composites::{CompositeMakeService, CompositeMakeServiceEntry, CompositeService, NotFound};

pub mod add_context;
pub use add_context::{AddContextMakeService, AddContextService};
Expand Down
3 changes: 3 additions & 0 deletions src/one_any_of.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Implementations of OpenAPI `oneOf` and `anyOf` types, assuming rules are just types
#[cfg(feature = "conversion")]
use frunk_enum_derive::LabelledGenericEnum;
use serde::{
de::Error,
Deserialize, Deserializer, Serialize, Serializer,
Expand All @@ -15,6 +17,7 @@ macro_rules! common_one_any_of {
$($i:ident),*
) => {
/// $t
#[cfg_attr(feature = "conversion", derive(LabelledGenericEnum))]
#[derive(Debug, PartialEq, Clone)]
pub enum $t<$($i),*> where
$($i: PartialEq,)*
Expand Down
34 changes: 16 additions & 18 deletions src/request_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro_rules! request_parser_joiner {
impl <B> RequestParser<B> for $name
where $($T: RequestParser<B>, )*
{
fn parse_operation_id(request: &Request<B>) -> Result<&'static str, ()> {
fn parse_operation_id(request: &Request<B>) -> Option<&'static str> {
__impl_request_parser_joiner!(request, $($T), *)
}
}
Expand All @@ -30,8 +30,8 @@ macro_rules! __impl_request_parser_joiner {
($argname:expr, $head:ty) => {<$head as RequestParser<B>>::parse_operation_id(&$argname)};
($argname:expr, $head:ty, $( $tail:ty), *) => {
match <$head as RequestParser<B>>::parse_operation_id(&$argname) {
Ok(s) => Ok(s),
Err(_) => __impl_request_parser_joiner!($argname, $( $tail), *),
Some(s) => Some(s),
None => __impl_request_parser_joiner!($argname, $( $tail), *),
}
};
}
Expand All @@ -46,10 +46,8 @@ macro_rules! __impl_request_parser_joiner {
pub trait RequestParser<B> {
/// Retrieve the Swagger operation identifier that matches this request.
///
/// Returns `Err(())` if this request does not match any known operation on this API.
// Allow this lint, as changing the signature is a breaking change.
#[allow(clippy::result_unit_err)]
fn parse_operation_id(req: &Request<B>) -> Result<&'static str, ()>;
/// Returns `None` if this request does not match any known operation on this API.
richardwhiuk marked this conversation as resolved.
Show resolved Hide resolved
fn parse_operation_id(req: &Request<B>) -> Option<&'static str>;
}

#[cfg(test)]
Expand All @@ -61,23 +59,23 @@ mod context_tests {
struct TestParser1;

impl RequestParser<Body> for TestParser1 {
fn parse_operation_id(request: &Request<Body>) -> Result<&'static str, ()> {
fn parse_operation_id(request: &Request<Body>) -> Option<&'static str> {
richardwhiuk marked this conversation as resolved.
Show resolved Hide resolved
match request.uri().path() {
"/test/t11" => Ok("t11"),
"/test/t12" => Ok("t12"),
_ => Err(()),
"/test/t11" => Some("t11"),
"/test/t12" => Some("t12"),
_ => None,
}
}
}

struct TestParser2;

impl RequestParser<Body> for TestParser2 {
fn parse_operation_id(request: &Request<Body>) -> Result<&'static str, ()> {
fn parse_operation_id(request: &Request<Body>) -> Option<&'static str> {
match request.uri().path() {
"/test/t21" => Ok("t21"),
"/test/t22" => Ok("t22"),
_ => Err(()),
"/test/t21" => Some("t21"),
"/test/t22" => Some("t22"),
_ => None,
}
}
}
Expand All @@ -95,8 +93,8 @@ mod context_tests {

request_parser_joiner!(JoinedReqParser, TestParser1, TestParser2);

assert_eq!(JoinedReqParser::parse_operation_id(&req1), Ok("t11"));
assert_eq!(JoinedReqParser::parse_operation_id(&req2), Ok("t22"));
assert_eq!(JoinedReqParser::parse_operation_id(&req3), Err(()));
assert_eq!(JoinedReqParser::parse_operation_id(&req1), Some("t11"));
assert_eq!(JoinedReqParser::parse_operation_id(&req2), Some("t22"));
assert_eq!(JoinedReqParser::parse_operation_id(&req3), None);
}
}