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

fix: updating the AST to support spotify's openapi spec #56

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(missing_debug_implementations)]

use serde::de::{Deserializer, MapAccess, Visitor};
use std::fmt;
use std::{fs::File, io::Read, path::Path};

mod error;
Expand Down Expand Up @@ -61,6 +63,35 @@ pub fn to_json(spec: &OpenApiV3Spec) -> Result<String, Error> {
Ok(serde_json::to_string_pretty(spec)?)
}

pub fn deserialize_extensions<'de, D>(deserializer: D) -> Result<serde_yaml::Value, D::Error>
where
D: Deserializer<'de>,
{
struct ExtraFieldsVisitor;

impl<'de> Visitor<'de> for ExtraFieldsVisitor {
type Value = serde_yaml::Value;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("extensions")
}

fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let mut map = serde_yaml::Mapping::new();
while let Some((key, value)) = access.next_entry()? {
map.insert(key, value);
}

Ok(map.into())
}
}

deserializer.deserialize_map(ExtraFieldsVisitor)
}

#[cfg(test)]
mod tests {
use std::{
Expand Down
6 changes: 5 additions & 1 deletion src/spec/components.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeMap;

use crate::deserialize_extensions;
use serde::{Deserialize, Serialize};

use super::{
Expand Down Expand Up @@ -64,5 +65,8 @@ pub struct Components {
#[serde(default)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub callbacks: BTreeMap<String, ObjectOrReference<Callback>>,
// TODO: Add "Specification Extensions" https://github.com/OAI/OpenAPI-Specification/blob/HEAD/versions/3.1.0.md#specificationExtensions}

#[serde(default)]
#[serde(deserialize_with = "deserialize_extensions")]
pub extensions: serde_yaml::Value,
}
8 changes: 8 additions & 0 deletions src/spec/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,21 @@ pub struct Parameter {
/// `header` - `simple`; for cookie - `form`.
#[serde(skip_serializing_if = "Option::is_none")]
style: Option<ParameterStyle>,

#[serde(skip_serializing_if = "Option::is_none")]
explode: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "in")]
param_in: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
enum ParameterStyle {
Form,
Simple,
DeepObject,
}

impl FromRef for Parameter {
Expand Down
10 changes: 8 additions & 2 deletions src/spec/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub enum Type {
Array,
Object,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum ObjectOrFalse<T> {
False(bool),
Object(T),
}

// FIXME: Verify against OpenAPI 3.0
/// The Schema Object allows the definition of input and output data types.
Expand Down Expand Up @@ -84,7 +90,7 @@ pub struct Schema {
/// See <https://github.com/OAI/OpenAPI-Specification/blob/HEAD/versions/3.1.0.md#properties>.
#[serde(rename = "additionalProperties")]
#[serde(skip_serializing_if = "Option::is_none")]
pub additional_properties: Option<Box<ObjectOrReference<Schema>>>,
pub additional_properties: Option<Box<ObjectOrFalse<ObjectOrReference<Schema>>>>,

//
// additional metadata
Expand All @@ -104,7 +110,7 @@ pub struct Schema {
#[serde(default)]
#[serde(rename = "enum")]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub enum_values: Vec<String>,
pub enum_values: Vec<serde_yaml::Value>,

#[serde(skip_serializing_if = "Option::is_none")]
pub pattern: Option<String>,
Expand Down
Loading