Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Derives Default for all structs
* Derives Clone for all structs
* Changes the order of the output to be more similar to OpenAPI examples
* Adds lots of properties related to minimum and maximum values

# 0.1.5

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ license = "MIT"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde_yaml = "0.7"
error-chain = "0.10"
serde_yaml = "0.8"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No particular reason for these two. Newer is better I'd hope

error-chain = "0.12"
semver = "0.9.0"
url = "1.6.0"
url_serde = "0.2.0"
106 changes: 97 additions & 9 deletions src/v2/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,67 @@ pub struct Operation {
pub schemes: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<String>>,
#[serde(rename = "operationId")]
#[serde(skip_serializing_if = "Option::is_none")]
pub operation_id: Option<String>,
pub responses: BTreeMap<String, Response>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parameters: Option<Vec<ParameterOrRef>>,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum PropertyDefault {
Integer(i32),
Boolean(bool),
String(String),
}

impl From<i32> for PropertyDefault {
fn from(item: i32) -> Self {
PropertyDefault::Integer(item)
}
}

impl From<bool> for PropertyDefault {
fn from(item: bool) -> Self {
PropertyDefault::Boolean(item)
}
}

impl From<String> for PropertyDefault {
fn from(item: String) -> Self {
PropertyDefault::String(item)
}
}

impl From<PropertyDefault> for i32 {
fn from(item: PropertyDefault) -> Self {
match item {
PropertyDefault::Integer(item) => item,
_ => 1,
}
}
}

impl From<PropertyDefault> for bool {
fn from(item: PropertyDefault) -> Self {
match item {
PropertyDefault::Boolean(item) => item,
_ => true,
Copy link
Owner

Choose a reason for hiding this comment

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

What do you think about using rusts defaults for these i.e. returning Default::default()? I'm also wondering if there's a better way to communicate this wasn't the type you thought it was. I.e. if you're defererning a string default as a book ect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I'm using Default::default() now.
I did not understand the second part of your comment

}
}
}

impl From<PropertyDefault> for String {
fn from(item: PropertyDefault) -> Self {
match item {
PropertyDefault::String(item) => item,
_ => "".to_string(),
}
}
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct Parameter {
Expand All @@ -184,6 +238,16 @@ pub struct Parameter {
pub format: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
//#[serde(deserialize_with = "deserialize_default_param")]
pub default: Option<PropertyDefault>,
#[serde(skip_serializing_if = "Option::is_none")]
pub minimum: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub maximum: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "enum")]
pub enum_values: Option<Vec<String>>,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
Expand Down Expand Up @@ -223,17 +287,40 @@ pub enum ParameterOrRef {
/// of use. GitHub Flavored Markdown is allowed.
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
/// The default value of this parameter.
/// Deser from String because it can be any type.
#[serde(skip_serializing_if = "Option::is_none")]
//#[serde(deserialize_with = "deserialize_default_param")]
default: Option<PropertyDefault>,
/// The minimum valid value for this parameter.
#[serde(skip_serializing_if = "Option::is_none")]
minimum: Option<i32>,
/// When set to true the value of the minimum property is not part of the range
#[serde(skip_serializing_if = "Option::is_none")]
exclusiveMinimum: Option<bool>,
/// The maximum valid value for this parameter.
#[serde(skip_serializing_if = "Option::is_none")]
maximum: Option<i32>,
/// When set to true the value of the maximum property is not part of the range
#[serde(skip_serializing_if = "Option::is_none")]
exclusiveMaximum: Option<bool>,
/// The maximum number of characters of a String
#[serde(skip_serializing_if = "Option::is_none")]
maxLength: Option<i32>,
/// The minimum number of characters of a String
#[serde(skip_serializing_if = "Option::is_none")]
minLength: Option<i32>,
/// The maximum number of items of an array
#[serde(skip_serializing_if = "Option::is_none")]
maxItems: Option<i32>,
/// The minimmum number of items of an array
#[serde(skip_serializing_if = "Option::is_none")]
minItems: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "enum")]
enum_values: Option<Vec<String>>,
// collectionFormat: ???
// default: ???
// maximum ?
// exclusiveMaximum ??
// minimum ??
// exclusiveMinimum ??
// maxLength ??
// minLength ??
// pattern ??
// maxItems ??
// minItems ??
// enum ??
// multipleOf ??
// allowEmptyValue ( for query / body params )
Expand All @@ -243,6 +330,7 @@ pub enum ParameterOrRef {
ref_path: String,
},
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(tag = "type")]
pub enum Security {
Expand Down