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

Add JsonSchema impls for BytesOrString and BoolFromInt #683

Merged
merged 7 commits into from
Jan 23, 2024
Merged
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
92 changes: 90 additions & 2 deletions serde_with/src/schemars_0_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
//! see [`JsonSchemaAs`].

use crate::{
formats::Separator,
formats::{Flexible, Separator, Strict},
prelude::{Schema as WrapSchema, *},
};
use ::schemars_0_8::{
gen::SchemaGenerator,
schema::{ArrayValidation, InstanceType, Schema, SchemaObject},
schema::{
ArrayValidation, InstanceType, Metadata, NumberValidation, Schema, SchemaObject,
SubschemaValidation,
},
JsonSchema,
};
use std::borrow::Cow;
Expand Down Expand Up @@ -362,6 +365,55 @@ impl<T> JsonSchemaAs<T> for DisplayFromStr {
forward_schema!(String);
}

impl JsonSchemaAs<bool> for BoolFromInt<Strict> {
fn schema_name() -> String {
"BoolFromInt<Strict>".into()
}

fn schema_id() -> Cow<'static, str> {
"serde_with::BoolFromInt<Strict>".into()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Integer.into()),
number: Some(Box::new(NumberValidation {
jonasbb marked this conversation as resolved.
Show resolved Hide resolved
minimum: Some(0.0),
maximum: Some(1.0),
..Default::default()
})),
..Default::default()
}
.into()
}

fn is_referenceable() -> bool {
false
}
}

impl JsonSchemaAs<bool> for BoolFromInt<Flexible> {
fn schema_name() -> String {
"BoolFromInt<Flexible>".into()
}

fn schema_id() -> Cow<'static, str> {
"serde_with::BoolFromInt<Flexible>".into()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Integer.into()),
..Default::default()
}
.into()
}

fn is_referenceable() -> bool {
false
}
}

impl<'a, T: 'a> JsonSchemaAs<Cow<'a, T>> for BorrowCow
where
T: ?Sized + ToOwned,
Expand All @@ -374,6 +426,42 @@ impl<T> JsonSchemaAs<T> for Bytes {
forward_schema!(Vec<u8>);
}

impl JsonSchemaAs<Vec<u8>> for BytesOrString {
fn schema_name() -> String {
"BytesOrString".into()
}

fn schema_id() -> Cow<'static, str> {
"serde_with::BytesOrString".into()
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
any_of: Some(std::vec![
gen.subschema_for::<Vec<u8>>(),
SchemaObject {
instance_type: Some(InstanceType::String.into()),
metadata: Some(Box::new(Metadata {
write_only: true,
jonasbb marked this conversation as resolved.
Show resolved Hide resolved
..Default::default()
})),
..Default::default()
}
.into()
]),
..Default::default()
})),
..Default::default()
}
.into()
}

fn is_referenceable() -> bool {
false
}
}

impl<T, TA> JsonSchemaAs<T> for DefaultOnError<TA>
where
TA: JsonSchemaAs<T>,
Expand Down
114 changes: 114 additions & 0 deletions serde_with/tests/schemars_0_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,120 @@ mod array {
}
}

mod bool_from_int {
use super::*;
use serde_with::formats::{Flexible, Strict};

#[serde_as]
#[derive(Serialize, JsonSchema)]
struct BoolStrict {
#[serde_as(as = "BoolFromInt<Strict>")]
value: bool,
}

#[serde_as]
#[derive(Serialize, JsonSchema)]
struct BoolFlexible {
#[serde_as(as = "BoolFromInt<Flexible>")]
value: bool,
}

#[test]
fn test_serialized_strict_is_valid() {
check_valid_json_schema(&vec![
BoolStrict { value: true },
BoolStrict { value: false },
]);
}

#[test]
fn test_serialized_flexible_is_valid() {
check_valid_json_schema(&vec![
BoolFlexible { value: true },
BoolFlexible { value: false },
]);
}

#[test]
#[should_panic]
fn strict_out_of_range() {
check_matches_schema::<BoolStrict>(&json!({
"value": 5
}));
}

#[test]
fn flexible_out_of_range() {
check_matches_schema::<BoolFlexible>(&json!({
"value": 5
}));
}

#[test]
#[should_panic]
fn flexible_wrong_type() {
check_matches_schema::<BoolFlexible>(&json!({
"value": "seven"
}));
}

#[test]
#[should_panic]
fn test_fractional_value_strict() {
check_matches_schema::<BoolStrict>(&json!({
"value": 0.5
}))
}

#[test]
#[should_panic]
fn test_fractional_value_flexible() {
check_matches_schema::<BoolFlexible>(&json!({
"value": 0.5
}))
}
}

mod bytes_or_string {
use super::*;

#[serde_as]
#[derive(Serialize, JsonSchema)]
struct Test {
#[serde_as(as = "BytesOrString")]
bytes: Vec<u8>,
}

#[test]
fn test_serialized_is_valid() {
check_valid_json_schema(&Test {
bytes: b"test".to_vec(),
});
}

#[test]
fn test_string_valid_json() {
check_matches_schema::<Test>(&json!({
"bytes": "test string"
}));
}

#[test]
fn test_bytes_valid_json() {
check_matches_schema::<Test>(&json!({
"bytes": [1, 2, 3, 4]
}));
}

#[test]
#[should_panic]
fn test_int_not_valid_json() {
check_matches_schema::<Test>(&json!({
"bytes": 5
}));
}
}

#[test]
fn test_borrow_cow() {
use std::borrow::Cow;
Expand Down