Skip to content

Commit

Permalink
added oneOf, anyOf, not to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
alamminsalo committed Oct 6, 2019
1 parent 3746db7 commit 86f13cc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
21 changes: 21 additions & 0 deletions data/v3.0/petstore-expanded.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,24 @@ components:
format: int32
message:
type: string

PetSpecies:
oneOf:
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Cat'

Dog:
properties:
name:
type: string
age:
type: int

Cat:
properties:
name:
type: string
age:
type: int
lives:
type: int
24 changes: 19 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,8 @@ mod tests {
};

/// Helper function to write string to file.
fn write_to_file<P>(
path: P,
filename: &str,
data: &str,
) where
fn write_to_file<P>(path: P, filename: &str, data: &str)
where
P: AsRef<Path> + std::fmt::Debug,
{
println!(" Saving string to {:?}...", path);
Expand Down Expand Up @@ -232,4 +229,21 @@ mod tests {
);
}
}

#[test]
fn can_deserialize_one_of_v3() {
let openapi = from_path("data/v3.0/petstore-expanded.yaml").unwrap();
if let OpenApi::V3_0(spec) = openapi {
let components = spec.components.unwrap();
let schemas = components.schemas.unwrap();
let obj_or_ref = schemas.get("PetSpecies");

if let Some(v3_0::ObjectOrReference::Object(schema)) = obj_or_ref {
// there should be 2 schemas in there
assert_eq!(schema.one_of.as_ref().unwrap().len(), 2);
} else {
panic!("object should have been schema");
}
}
}
}
19 changes: 19 additions & 0 deletions src/v3_0/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,27 @@ pub struct Schema {

/// Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard
/// JSON Schema.
/// [allOf](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#allof)
#[serde(rename = "allOf", skip_serializing_if = "Option::is_none")]
pub all_of: Option<Vec<ObjectOrReference<Schema>>>,

/// Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard
/// JSON Schema.
/// [oneOf](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#oneof)
#[serde(rename = "oneOf", skip_serializing_if = "Option::is_none")]
pub one_of: Option<Vec<ObjectOrReference<Schema>>>,

/// Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard
/// JSON Schema.
/// [anyOf](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#anyof)
#[serde(rename = "anyOf", skip_serializing_if = "Option::is_none")]
pub any_of: Option<Vec<ObjectOrReference<Schema>>>,

/// Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard
/// JSON Schema.
/// [not](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#not)
#[serde(rename = "not", skip_serializing_if = "Option::is_none")]
pub not: Option<Vec<ObjectOrReference<Schema>>>,
}

/// Describes a single response from an API Operation, including design-time, static `links`
Expand Down

0 comments on commit 86f13cc

Please sign in to comment.