Skip to content

Commit

Permalink
fix: Fix serde issues. (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Aug 12, 2024
1 parent d933132 commit b3a538f
Show file tree
Hide file tree
Showing 35 changed files with 483 additions and 64 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

#### 🐞 Fixes

- Fixed some issues around serializing the schemas.

## 0.17.0

#### 💥 Breaking
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ semver = "1.0.23"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.121"
serde_yaml = "0.9.34"
starbase_sandbox = "0.7.2"
toml = "0.8.16"
tracing = "0.1.40"
url = "2.5.2"
2 changes: 1 addition & 1 deletion crates/schematic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ reqwest = { workspace = true, features = [
] }
serial_test = "3.1.1"
similar = "2.6.0"
starbase_sandbox = "0.7.2"
starbase_sandbox = { workspace = true }

# Types
chrono = { workspace = true, features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ schematic_types = { path = ".", features = [
"serde_yaml",
"url",
] }
starbase_sandbox = { workspace = true }

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion crates/types/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Schema {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub name: Option<String>,

#[cfg_attr(feature = "serde", serde(skip_serializing))]
#[cfg_attr(feature = "serde", serde(skip_serializing, default))]
pub nullable: bool,

pub ty: SchemaType,
Expand Down
2 changes: 1 addition & 1 deletion crates/types/src/schema_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::unions::*;
/// All possible types within a schema.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", content = "value"))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
pub enum SchemaType {
Null,
#[default]
Expand Down
121 changes: 60 additions & 61 deletions crates/types/tests/builder_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use schematic_types::*;
use starbase_sandbox::assert_snapshot;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::net::Ipv4Addr;
use std::path::{Path, PathBuf};
Expand All @@ -8,6 +9,22 @@ fn test_builder<T: Schematic>() -> Schema {
SchemaBuilder::build_root::<T>()
}

macro_rules! assert_build {
($ty:ty, $expected:expr) => {
let schema = test_builder::<$ty>();

assert_eq!(schema.ty, $expected);

let input = serde_json::to_string_pretty(&schema).unwrap();

assert_snapshot!(&input);

let output: Schema = serde_json::from_str(&input).unwrap();

assert_eq!(schema, output);
};
}

pub struct Named {
pub field: bool,
}
Expand All @@ -24,30 +41,18 @@ impl Schematic for Named {

#[test]
fn primitives() {
assert_eq!(test_builder::<()>().ty, SchemaType::Null);
assert_build!((), SchemaType::Null);

assert_eq!(
test_builder::<bool>().ty,
SchemaType::Boolean(Box::default())
);
assert_build!(bool, SchemaType::Boolean(Box::default()));

assert_eq!(
test_builder::<&bool>().ty,
SchemaType::Boolean(Box::default())
);
assert_build!(&bool, SchemaType::Boolean(Box::default()));

assert_eq!(
test_builder::<&mut bool>().ty,
SchemaType::Boolean(Box::default())
);
assert_build!(&mut bool, SchemaType::Boolean(Box::default()));

assert_eq!(
test_builder::<Box<bool>>().ty,
SchemaType::Boolean(Box::default())
);
assert_build!(Box<bool>, SchemaType::Boolean(Box::default()));

assert_eq!(
test_builder::<Option<bool>>().ty,
assert_build!(
Option<bool>,
SchemaType::Union(Box::new(UnionType::new_any(vec![
SchemaType::Boolean(Box::default()),
SchemaType::Null
Expand All @@ -57,18 +62,18 @@ fn primitives() {

#[test]
fn arrays() {
assert_eq!(
test_builder::<Vec<String>>().ty,
assert_build!(
Vec<String>,
SchemaType::Array(Box::new(ArrayType::new(SchemaType::String(Box::default()))))
);

assert_eq!(
test_builder::<&[String]>().ty,
assert_build!(
&[String],
SchemaType::Array(Box::new(ArrayType::new(SchemaType::String(Box::default()))))
);

assert_eq!(
test_builder::<[String; 3]>().ty,
assert_build!(
[String; 3],
SchemaType::Array(Box::new(ArrayType {
items_type: Box::new(Schema::string(StringType::default())),
max_length: Some(3),
Expand All @@ -77,17 +82,17 @@ fn arrays() {
}))
);

assert_eq!(
test_builder::<HashSet<String>>().ty,
assert_build!(
HashSet<String>,
SchemaType::Array(Box::new(ArrayType {
items_type: Box::new(Schema::string(StringType::default())),
unique: Some(true),
..ArrayType::default()
}))
);

assert_eq!(
test_builder::<BTreeSet<String>>().ty,
assert_build!(
BTreeSet<String>,
SchemaType::Array(Box::new(ArrayType {
items_type: Box::new(Schema::string(StringType::default())),
unique: Some(true),
Expand All @@ -98,42 +103,42 @@ fn arrays() {

#[test]
fn integers() {
assert_eq!(
test_builder::<u8>().ty,
assert_build!(
u8,
SchemaType::Integer(Box::new(IntegerType::new_kind(IntegerKind::U8)))
);

assert_eq!(
test_builder::<i32>().ty,
assert_build!(
i32,
SchemaType::Integer(Box::new(IntegerType::new_kind(IntegerKind::I32)))
);
}

#[test]
fn floats() {
assert_eq!(
test_builder::<f32>().ty,
assert_build!(
f32,
SchemaType::Float(Box::new(FloatType::new_kind(FloatKind::F32)))
);

assert_eq!(
test_builder::<f64>().ty,
assert_build!(
f64,
SchemaType::Float(Box::new(FloatType::new_kind(FloatKind::F64)))
);
}

#[test]
fn objects() {
assert_eq!(
test_builder::<HashMap<String, Named>>().ty,
assert_build!(
HashMap<String, Named>,
SchemaType::Object(Box::new(ObjectType::new(
Schema::string(StringType::default()),
test_builder::<Named>(),
)))
);

assert_eq!(
test_builder::<BTreeMap<u128, Named>>().ty,
assert_build!(
BTreeMap<u128, Named>,
SchemaType::Object(Box::new(ObjectType::new(
SchemaType::Integer(Box::new(IntegerType::new_kind(IntegerKind::U128))),
test_builder::<Named>(),
Expand All @@ -143,51 +148,45 @@ fn objects() {

#[test]
fn strings() {
assert_eq!(
test_builder::<char>().ty,
assert_build!(
char,
SchemaType::String(Box::new(StringType {
max_length: Some(1),
min_length: Some(1),
..StringType::default()
}))
);

assert_eq!(
test_builder::<&str>().ty,
SchemaType::String(Box::default())
);
assert_build!(&str, SchemaType::String(Box::default()));

assert_eq!(
test_builder::<String>().ty,
SchemaType::String(Box::default())
);
assert_build!(String, SchemaType::String(Box::default()));

assert_eq!(
test_builder::<&Path>().ty,
assert_build!(
&Path,
SchemaType::String(Box::new(StringType {
format: Some("path".into()),
..StringType::default()
}))
);

assert_eq!(
test_builder::<PathBuf>().ty,
assert_build!(
PathBuf,
SchemaType::String(Box::new(StringType {
format: Some("path".into()),
..StringType::default()
}))
);

assert_eq!(
test_builder::<Ipv4Addr>().ty,
assert_build!(
Ipv4Addr,
SchemaType::String(Box::new(StringType {
format: Some("ipv4".into()),
..StringType::default()
}))
);

assert_eq!(
test_builder::<Duration>().ty,
assert_build!(
Duration,
SchemaType::String(Box::new(StringType {
format: Some("duration".into()),
..StringType::default()
Expand All @@ -197,8 +196,8 @@ fn strings() {

#[test]
fn tuples() {
assert_eq!(
test_builder::<(bool, i16, f32, String)>().ty,
assert_build!(
(bool, i16, f32, String),
SchemaType::Tuple(Box::new(TupleType::new(vec![
SchemaType::Boolean(Box::default()),
SchemaType::Integer(Box::new(IntegerType::new_kind(IntegerKind::I16))),
Expand Down
14 changes: 14 additions & 0 deletions crates/types/tests/snapshots/builder_test__arrays-2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/types/tests/builder_test.rs
expression: "& input"
---
{
"ty": {
"type": "Array",
"items_type": {
"ty": {
"type": "String"
}
}
}
}
16 changes: 16 additions & 0 deletions crates/types/tests/snapshots/builder_test__arrays-3.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/types/tests/builder_test.rs
expression: "& input"
---
{
"ty": {
"type": "Array",
"items_type": {
"ty": {
"type": "String"
}
},
"max_length": 3,
"min_length": 3
}
}
15 changes: 15 additions & 0 deletions crates/types/tests/snapshots/builder_test__arrays-4.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/types/tests/builder_test.rs
expression: "& input"
---
{
"ty": {
"type": "Array",
"items_type": {
"ty": {
"type": "String"
}
},
"unique": true
}
}
15 changes: 15 additions & 0 deletions crates/types/tests/snapshots/builder_test__arrays-5.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/types/tests/builder_test.rs
expression: "& input"
---
{
"ty": {
"type": "Array",
"items_type": {
"ty": {
"type": "String"
}
},
"unique": true
}
}
14 changes: 14 additions & 0 deletions crates/types/tests/snapshots/builder_test__arrays.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/types/tests/builder_test.rs
expression: "& input"
---
{
"ty": {
"type": "Array",
"items_type": {
"ty": {
"type": "String"
}
}
}
}
Loading

0 comments on commit b3a538f

Please sign in to comment.