Fix schemas for internally tagged newtype variants

Fixes #2
This commit is contained in:
Graham Esau 2019-10-06 20:22:27 +01:00
parent 01632b37fc
commit a555d7739a
6 changed files with 199 additions and 54 deletions

View file

@ -3,8 +3,13 @@ use crate::{JsonSchemaError, Map, Result, Set};
impl Schema { impl Schema {
pub fn flatten(self, other: Self) -> Result { pub fn flatten(self, other: Self) -> Result {
let s1 = ensure_flattenable(self)?; if is_null_type(&self) {
let s2 = ensure_flattenable(other)?; return Ok(other)
} else if is_null_type(&other) {
return Ok(self)
}
let s1 = ensure_object_type(self)?;
let s2 = ensure_object_type(other)?;
Ok(Schema::Object(s1.merge(s2))) Ok(Schema::Object(s1.merge(s2)))
} }
} }
@ -111,12 +116,23 @@ impl Merge for SingleOrVec<InstanceType> {
} }
} }
fn ensure_flattenable(schema: Schema) -> Result<SchemaObject> { fn is_null_type(schema: &Schema) -> bool {
let s = match schema {
Schema::Object(s) => s,
_ => return false,
};
match &s.instance_type {
Some(SingleOrVec::Single(t)) if **t == InstanceType::Null => true,
_ => false,
}
}
fn ensure_object_type(schema: Schema) -> Result<SchemaObject> {
let s = match schema { let s = match schema {
Schema::Object(s) => s, Schema::Object(s) => s,
s => { s => {
return Err(JsonSchemaError::new( return Err(JsonSchemaError::new(
"Only schemas with type `object` can be flattened.", "Only schemas with type `object` or `null` can be flattened.",
s, s,
)) ))
} }
@ -124,13 +140,13 @@ fn ensure_flattenable(schema: Schema) -> Result<SchemaObject> {
match s.instance_type { match s.instance_type {
Some(SingleOrVec::Single(ref t)) if **t != InstanceType::Object => { Some(SingleOrVec::Single(ref t)) if **t != InstanceType::Object => {
Err(JsonSchemaError::new( Err(JsonSchemaError::new(
"Only schemas with type `object` can be flattened.", "Only schemas with type `object` or `null` can be flattened.",
s.into(), s.into(),
)) ))
} }
Some(SingleOrVec::Vec(ref t)) if !t.contains(&InstanceType::Object) => { Some(SingleOrVec::Vec(ref t)) if !t.contains(&InstanceType::Object) => {
Err(JsonSchemaError::new( Err(JsonSchemaError::new(
"Only schemas with type `object` can be flattened.", "Only schemas with type `object` or `null` can be flattened.",
s.into(), s.into(),
)) ))
} }

View file

@ -2,11 +2,22 @@ mod util;
use schemars::{JsonSchema, Map}; use schemars::{JsonSchema, Map};
use util::*; use util::*;
#[derive(Debug, JsonSchema)]
pub struct UnitStruct;
#[derive(Debug, JsonSchema)]
pub struct Struct {
foo: i32,
bar: bool,
}
#[derive(Debug, JsonSchema)] #[derive(Debug, JsonSchema)]
#[schemars(rename_all = "camelCase")] #[schemars(rename_all = "camelCase")]
pub enum External { pub enum External {
UnitOne, UnitOne,
StringMap(Map<String, String>), StringMap(Map<String, String>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct { foo: i32, bar: bool }, Struct { foo: i32, bar: bool },
UnitTwo, UnitTwo,
Tuple(i32, bool), Tuple(i32, bool),
@ -22,6 +33,8 @@ fn enum_external_tag() -> TestResult {
pub enum Internal { pub enum Internal {
UnitOne, UnitOne,
StringMap(Map<String, String>), StringMap(Map<String, String>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct { foo: i32, bar: bool }, Struct { foo: i32, bar: bool },
UnitTwo, UnitTwo,
} }
@ -36,6 +49,8 @@ fn enum_internal_tag() -> TestResult {
pub enum Untagged { pub enum Untagged {
UnitOne, UnitOne,
StringMap(Map<String, String>), StringMap(Map<String, String>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct { foo: i32, bar: bool }, Struct { foo: i32, bar: bool },
Tuple(i32, bool), Tuple(i32, bool),
} }

View file

@ -10,6 +10,9 @@
}, },
{ {
"type": "object", "type": "object",
"required": [
"stringMap"
],
"properties": { "properties": {
"stringMap": { "stringMap": {
"type": "object", "type": "object",
@ -17,16 +20,42 @@
"type": "string" "type": "string"
} }
} }
}, }
"required": [
"stringMap"
]
}, },
{ {
"type": "object", "type": "object",
"required": [
"unitStructNewType"
],
"properties": {
"unitStructNewType": {
"$ref": "#/definitions/UnitStruct"
}
}
},
{
"type": "object",
"required": [
"structNewType"
],
"properties": {
"structNewType": {
"$ref": "#/definitions/Struct"
}
}
},
{
"type": "object",
"required": [
"struct"
],
"properties": { "properties": {
"struct": { "struct": {
"type": "object", "type": "object",
"required": [
"bar",
"foo"
],
"properties": { "properties": {
"bar": { "bar": {
"type": "boolean" "type": "boolean"
@ -35,19 +64,15 @@
"type": "integer", "type": "integer",
"format": "int32" "format": "int32"
} }
},
"required": [
"bar",
"foo"
]
} }
}, }
"required": [ }
"struct"
]
}, },
{ {
"type": "object", "type": "object",
"required": [
"tuple"
],
"properties": { "properties": {
"tuple": { "tuple": {
"type": "array", "type": "array",
@ -63,10 +88,28 @@
"maxItems": 2, "maxItems": 2,
"minItems": 2 "minItems": 2
} }
},
"required": [
"tuple"
]
} }
] }
],
"definitions": {
"Struct": {
"type": "object",
"required": [
"bar",
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
}
}
},
"UnitStruct": {
"type": "null"
}
}
} }

View file

@ -4,6 +4,9 @@
"anyOf": [ "anyOf": [
{ {
"type": "object", "type": "object",
"required": [
"typeProperty"
],
"properties": { "properties": {
"typeProperty": { "typeProperty": {
"type": "string", "type": "string",
@ -11,13 +14,13 @@
"UnitOne" "UnitOne"
] ]
} }
}, }
"required": [
"typeProperty"
]
}, },
{ {
"type": "object", "type": "object",
"required": [
"typeProperty"
],
"properties": { "properties": {
"typeProperty": { "typeProperty": {
"type": "string", "type": "string",
@ -26,15 +29,54 @@
] ]
} }
}, },
"required": [
"typeProperty"
],
"additionalProperties": { "additionalProperties": {
"type": "string" "type": "string"
} }
}, },
{ {
"type": "object", "type": "object",
"required": [
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"UnitStructNewType"
]
}
}
},
{
"type": "object",
"required": [
"bar",
"foo",
"typeProperty"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
},
"typeProperty": {
"type": "string",
"enum": [
"StructNewType"
]
}
}
},
{
"type": "object",
"required": [
"bar",
"foo",
"typeProperty"
],
"properties": { "properties": {
"bar": { "bar": {
"type": "boolean" "type": "boolean"
@ -49,15 +91,13 @@
"Struct" "Struct"
] ]
} }
}, }
"required": [
"bar",
"foo",
"typeProperty"
]
}, },
{ {
"type": "object", "type": "object",
"required": [
"typeProperty"
],
"properties": { "properties": {
"typeProperty": { "typeProperty": {
"type": "string", "type": "string",
@ -65,10 +105,7 @@
"UnitTwo" "UnitTwo"
] ]
} }
}, }
"required": [
"typeProperty"
]
} }
] ]
} }

View file

@ -11,8 +11,18 @@
"type": "string" "type": "string"
} }
}, },
{
"$ref": "#/definitions/UnitStruct"
},
{
"$ref": "#/definitions/Struct"
},
{ {
"type": "object", "type": "object",
"required": [
"bar",
"foo"
],
"properties": { "properties": {
"bar": { "bar": {
"type": "boolean" "type": "boolean"
@ -21,11 +31,7 @@
"type": "integer", "type": "integer",
"format": "int32" "format": "int32"
} }
}, }
"required": [
"bar",
"foo"
]
}, },
{ {
"type": "array", "type": "array",
@ -41,5 +47,26 @@
"maxItems": 2, "maxItems": 2,
"minItems": 2 "minItems": 2
} }
] ],
"definitions": {
"Struct": {
"type": "object",
"required": [
"bar",
"foo"
],
"properties": {
"bar": {
"type": "boolean"
},
"foo": {
"type": "integer",
"format": "int32"
}
}
},
"UnitStruct": {
"type": "null"
}
}
} }

View file

@ -174,7 +174,7 @@ fn schema_for_internal_tagged_enum<'a>(
instance_type: Some(schemars::schema::InstanceType::String.into()), instance_type: Some(schemars::schema::InstanceType::String.into()),
enum_values: Some(vec![#name.into()]), enum_values: Some(vec![#name.into()]),
}); });
let schema = wrap_schema_fields(quote! { let tag_schema = wrap_schema_fields(quote! {
instance_type: Some(schemars::schema::InstanceType::Object.into()), instance_type: Some(schemars::schema::InstanceType::Object.into()),
object: schemars::schema::ObjectValidation { object: schemars::schema::ObjectValidation {
properties: { properties: {
@ -190,14 +190,21 @@ fn schema_for_internal_tagged_enum<'a>(
..Default::default() ..Default::default()
}, },
}); });
if is_unit_variant(&variant) { let variant_schema = match variant.style {
schema Style::Unit => return tag_schema,
} else { Style::Newtype => {
let sub_schema = schema_for_untagged_enum_variant(variant, cattrs); let field = &variant.fields[0];
quote! { let ty = get_json_schema_type(field);
#schema.flatten(#sub_schema)? quote_spanned! {field.original.span()=>
<#ty>::json_schema(gen)?
} }
} }
Style::Struct => schema_for_struct(&variant.fields, cattrs),
Style::Tuple => unreachable!("Internal tagged enum tuple variants will have caused serde_derive_internals to output a compile error already."),
};
quote! {
#tag_schema.flatten(#variant_schema)?
}
}); });
wrap_schema_fields(quote! { wrap_schema_fields(quote! {