-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
feat: Adding support to parse discriminator field in ObjectSchema #138
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb575c0
feat: Added support to parse discriminator
vgerber 40706b4
doc: Added discriminator doc string
vgerber 575afe3
Merge branch 'main' into any-discriminator
vgerber 2abbc5a
Merge branch 'main' into any-discriminator
vgerber 8660ba1
Merge branch 'main' into any-discriminator
robjtede 2bef3bb
test: fix schema discriminator test
robjtede 3a40850
docs: update changelog
robjtede d364fc3
test: tweak spacing
robjtede adeb5f0
chore: move discriminator to right section
robjtede File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! 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}; | ||
|
||
/// 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://spec.openapis.org/oas/v3.1.0#discriminator-object> | ||
#[derive(Clone, Debug, PartialEq, Default, Deserialize, Serialize)] | ||
pub struct Discriminator { | ||
/// The name of the property in the payload that will hold the discriminator value. | ||
#[serde(rename = "propertyName")] | ||
pub property_name: String, | ||
|
||
/// An 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation incorrect and not caught by test due to lacking assertions on content