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

panic in the face of type cycles #151

Merged
merged 2 commits into from
Oct 18, 2021
Merged
Changes from 1 commit
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
60 changes: 58 additions & 2 deletions dropshot/src/type_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Utility functions for working with JsonSchema types.
*/

use std::collections::HashSet;

use indexmap::IndexMap;
use schemars::schema::{InstanceType, Schema, SchemaObject, SingleOrVec};

Expand Down Expand Up @@ -93,7 +95,7 @@ pub fn type_is_string_enum(
object: None,
reference: None,
extensions: _,
}) if matches!(instance_type.as_ref(), InstanceType::Array) => {
}) if instance_type.as_ref() == &InstanceType::Array => {
match array_validation.as_ref() {
schemars::schema::ArrayValidation {
items:
Expand All @@ -119,10 +121,11 @@ pub fn type_is_string_enum(
}
}

pub fn type_resolve<'a>(
fn type_resolve<'a>(
mut schema: &'a Schema,
dependencies: &'a IndexMap<String, Schema>,
) -> &'a Schema {
let mut set = HashSet::new();
while let Schema::Object(SchemaObject {
metadata: _,
instance_type: None,
Expand All @@ -138,6 +141,15 @@ pub fn type_resolve<'a>(
extensions: _,
}) = schema
{
if set.contains(ref_schema) {
eprintln!("{:#?}", schema);
eprintln!(
"consider #[schemars(rename = \"...\")] or \
#[schemars(transparent)]"
ahl marked this conversation as resolved.
Show resolved Hide resolved
);
panic!("type reference cycle detected");
}
set.insert(ref_schema);
const PREFIX: &str = "#/components/schemas/";
assert!(ref_schema.starts_with(PREFIX));
schema = dependencies
Expand All @@ -146,3 +158,47 @@ pub fn type_resolve<'a>(
}
schema
}

#[cfg(test)]
mod tests {
use indexmap::IndexMap;
use schemars::schema::{Schema, SchemaObject};

use super::type_resolve;

#[test]
#[should_panic(expected = "type reference cycle detected")]
fn test_reflexive_type() {
let name = "#/components/schemas/Selfie".to_string();
let schema = Schema::Object(SchemaObject {
reference: Some(name),
..Default::default()
});

let mut dependencies = IndexMap::new();
dependencies.insert("Selfie".to_string(), schema.clone());
let schema_ref = &dependencies[0];

type_resolve(schema_ref, &dependencies);
}

#[test]
#[should_panic(expected = "type reference cycle detected")]
fn test_recursive_type() {
let jack_schema = Schema::Object(SchemaObject {
reference: Some("#/components/schemas/JohnJackson".to_string()),
..Default::default()
});
let john_schema = Schema::Object(SchemaObject {
reference: Some("#/components/schemas/JackJohnson".to_string()),
..Default::default()
});

let mut dependencies = IndexMap::new();
dependencies.insert("JackJohnson".to_string(), jack_schema);
dependencies.insert("JohnJackson".to_string(), john_schema);
let schema_ref = &dependencies[0];

type_resolve(schema_ref, &dependencies);
}
}