Fix deriving JsonSchema inside macro (#79)
This commit is contained in:
parent
dada8582ee
commit
4d3400152e
4 changed files with 71 additions and 21 deletions
|
@ -1,5 +1,9 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [0.8.3] - **In-dev**
|
||||||
|
### Fixed:
|
||||||
|
- Fix deriving JsonSchema on types defined inside macros (https://github.com/GREsau/schemars/issues/66 / https://github.com/GREsau/schemars/pull/79)
|
||||||
|
|
||||||
## [0.8.2] - 2021-03-27
|
## [0.8.2] - 2021-03-27
|
||||||
### Added:
|
### Added:
|
||||||
- Enable generating a schema from any serializable value using `schema_for_value!(...)` macro or `SchemaGenerator::root_schema_for_value()`/`SchemaGenerator::into_root_schema_for_value()` methods (https://github.com/GREsau/schemars/pull/75)
|
- Enable generating a schema from any serializable value using `schema_for_value!(...)` macro or `SchemaGenerator::root_schema_for_value()`/`SchemaGenerator::into_root_schema_for_value()` methods (https://github.com/GREsau/schemars/pull/75)
|
||||||
|
|
20
schemars/tests/expected/macro_built_struct.json
Normal file
20
schemars/tests/expected/macro_built_struct.json
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "A",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"v",
|
||||||
|
"x"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"x": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "uint8",
|
||||||
|
"minimum": 0.0
|
||||||
|
},
|
||||||
|
"v": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
schemars/tests/macro.rs
Normal file
22
schemars/tests/macro.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
mod util;
|
||||||
|
use schemars::JsonSchema;
|
||||||
|
use util::*;
|
||||||
|
|
||||||
|
macro_rules! build_struct {
|
||||||
|
(
|
||||||
|
$id:ident { $($t:tt)* }
|
||||||
|
) => {
|
||||||
|
#[derive(Debug, JsonSchema)]
|
||||||
|
pub struct $id {
|
||||||
|
x: u8,
|
||||||
|
$($t)*
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
build_struct!(A { v: i32 });
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn macro_built_struct() -> TestResult {
|
||||||
|
test_default_generated_schema::<A>("macro_built_struct")
|
||||||
|
}
|
|
@ -390,7 +390,9 @@ fn expr_for_struct(
|
||||||
|
|
||||||
let mut type_defs = Vec::new();
|
let mut type_defs = Vec::new();
|
||||||
|
|
||||||
let properties: Vec<_> = property_fields.into_iter().map(|field| {
|
let properties: Vec<_> = property_fields
|
||||||
|
.into_iter()
|
||||||
|
.map(|field| {
|
||||||
let name = field.name();
|
let name = field.name();
|
||||||
let default = field_default_expr(field, set_container_default.is_some());
|
let default = field_default_expr(field, set_container_default.is_some());
|
||||||
|
|
||||||
|
@ -411,11 +413,13 @@ fn expr_for_struct(
|
||||||
type_defs.push(type_def);
|
type_defs.push(type_def);
|
||||||
}
|
}
|
||||||
|
|
||||||
quote_spanned! {ty.span()=>
|
let args = quote!(gen, &mut schema_object, #name.to_owned(), #metadata, #required);
|
||||||
<#ty as schemars::JsonSchema>::add_schema_as_property(gen, &mut schema_object, #name.to_owned(), #metadata, #required);
|
|
||||||
}
|
|
||||||
|
|
||||||
}).collect();
|
quote_spanned! {ty.span()=>
|
||||||
|
<#ty as schemars::JsonSchema>::add_schema_as_property(#args);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
let flattens: Vec<_> = flattened_fields
|
let flattens: Vec<_> = flattened_fields
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue