-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding support to parse discriminator field in ObjectSchema (#138)
Co-authored-by: Rob Ede <[email protected]>
- Loading branch information
Showing
6 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
"refpath", | ||
"reqwest", | ||
"rustfmt", | ||
"rustup", | ||
"semver", | ||
"serde", | ||
"spdx", | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//! Schema specification for [OpenAPI 3.1](https://github.com/OAI/OpenAPI-Specification/blob/HEAD/versions/3.1.0.md) | ||
use std::collections::BTreeMap; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
/// A discriminator object can be used to aid in serialization, deserialization, and validation when | ||
/// payloads may be one of a number of different schemas. | ||
/// | ||
/// The discriminator is a specific object in a schema which is used to inform the consumer of the | ||
/// document of an alternative schema based on the value associated with it. | ||
/// | ||
/// See <https://github.com/OAI/OpenAPI-Specification/blob/HEAD/versions/3.1.0.md#discriminator-object>. | ||
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Discriminator { | ||
/// Name of the property in the payload that will hold the discriminator value. | ||
pub property_name: String, | ||
|
||
/// Object to hold mappings between payload values and schema names or references. | ||
/// | ||
/// When using the discriminator, inline schemas will not be considered. | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub mapping: Option<BTreeMap<String, String>>, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn discriminator_property_name_parsed_correctly() { | ||
let spec = "propertyName: testName"; | ||
let discriminator = serde_yml::from_str::<Discriminator>(spec).unwrap(); | ||
assert_eq!("testName", discriminator.property_name); | ||
assert!(discriminator.mapping.is_none()); | ||
} | ||
|
||
#[test] | ||
fn discriminator_mapping_parsed_correctly() { | ||
let spec = indoc::indoc! {" | ||
propertyName: petType | ||
mapping: | ||
dog: '#/components/schemas/Dog' | ||
cat: '#/components/schemas/Cat' | ||
monster: 'https://gigantic-server.com/schemas/Monster/schema.json' | ||
"}; | ||
let discriminator = serde_yml::from_str::<Discriminator>(spec).unwrap(); | ||
|
||
assert_eq!("petType", discriminator.property_name); | ||
let mapping = discriminator.mapping.unwrap(); | ||
|
||
assert_eq!("#/components/schemas/Dog", mapping.get("dog").unwrap()); | ||
assert_eq!("#/components/schemas/Cat", mapping.get("cat").unwrap()); | ||
assert_eq!( | ||
"https://gigantic-server.com/schemas/Monster/schema.json", | ||
mapping.get("monster").unwrap() | ||
); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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