Allow arbitrary expressions in doc/title/description attributes (#327)
This commit is contained in:
		
							parent
							
								
									5547e77bcd
								
							
						
					
					
						commit
						df06fc5f66
					
				
					 17 changed files with 206 additions and 159 deletions
				
			
		| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
mod util;
 | 
			
		||||
use schemars::{generate::SchemaSettings, JsonSchema};
 | 
			
		||||
use schemars::JsonSchema;
 | 
			
		||||
use util::*;
 | 
			
		||||
 | 
			
		||||
#[allow(dead_code)]
 | 
			
		||||
| 
						 | 
				
			
			@ -15,6 +15,9 @@ struct MyStruct {
 | 
			
		|||
    my_undocumented_bool: bool,
 | 
			
		||||
    /// A unit struct instance
 | 
			
		||||
    my_unit: MyUnitStruct,
 | 
			
		||||
    #[doc = concat!("# Documented ", "bool")]
 | 
			
		||||
    #[doc = concat!("This bool is documented")]
 | 
			
		||||
    my_documented_bool: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// # A Unit
 | 
			
		||||
| 
						 | 
				
			
			@ -57,12 +60,6 @@ fn doc_comments_struct() -> TestResult {
 | 
			
		|||
    test_default_generated_schema::<MyStruct>("doc_comments_struct")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn doc_comments_struct_ref_siblings() -> TestResult {
 | 
			
		||||
    let settings = SchemaSettings::draft2019_09();
 | 
			
		||||
    test_generated_schema::<MyStruct>("doc_comments_struct_ref_siblings", settings)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn doc_comments_enum() -> TestResult {
 | 
			
		||||
    test_default_generated_schema::<MyEnum>("doc_comments_enum")
 | 
			
		||||
| 
						 | 
				
			
			@ -81,6 +78,8 @@ struct OverrideDocs {
 | 
			
		|||
    /// Also overridden
 | 
			
		||||
    #[schemars(title = "", description = "")]
 | 
			
		||||
    my_undocumented_bool: bool,
 | 
			
		||||
    #[schemars(title = concat!("Documented ", "bool"), description = "Capitalized".to_uppercase())]
 | 
			
		||||
    my_documented_bool: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,10 +12,16 @@
 | 
			
		|||
    },
 | 
			
		||||
    "my_undocumented_bool": {
 | 
			
		||||
      "type": "boolean"
 | 
			
		||||
    },
 | 
			
		||||
    "my_documented_bool": {
 | 
			
		||||
      "title": "Documented bool",
 | 
			
		||||
      "description": "CAPITALIZED",
 | 
			
		||||
      "type": "boolean"
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  "required": [
 | 
			
		||||
    "my_int",
 | 
			
		||||
    "my_undocumented_bool"
 | 
			
		||||
    "my_undocumented_bool",
 | 
			
		||||
    "my_documented_bool"
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -15,12 +15,18 @@
 | 
			
		|||
    "my_unit": {
 | 
			
		||||
      "description": "A unit struct instance",
 | 
			
		||||
      "$ref": "#/$defs/MyUnitStruct"
 | 
			
		||||
    },
 | 
			
		||||
    "my_documented_bool": {
 | 
			
		||||
      "title": "Documented bool",
 | 
			
		||||
      "description": "This bool is documented",
 | 
			
		||||
      "type": "boolean"
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  "required": [
 | 
			
		||||
    "my_int",
 | 
			
		||||
    "my_undocumented_bool",
 | 
			
		||||
    "my_unit"
 | 
			
		||||
    "my_unit",
 | 
			
		||||
    "my_documented_bool"
 | 
			
		||||
  ],
 | 
			
		||||
  "$defs": {
 | 
			
		||||
    "MyUnitStruct": {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,31 +0,0 @@
 | 
			
		|||
{
 | 
			
		||||
  "$schema": "https://json-schema.org/draft/2019-09/schema",
 | 
			
		||||
  "title": "This is the struct's title",
 | 
			
		||||
  "description": "This is the struct's description.",
 | 
			
		||||
  "type": "object",
 | 
			
		||||
  "properties": {
 | 
			
		||||
    "my_int": {
 | 
			
		||||
      "title": "An integer",
 | 
			
		||||
      "type": "integer",
 | 
			
		||||
      "format": "int32"
 | 
			
		||||
    },
 | 
			
		||||
    "my_undocumented_bool": {
 | 
			
		||||
      "type": "boolean"
 | 
			
		||||
    },
 | 
			
		||||
    "my_unit": {
 | 
			
		||||
      "description": "A unit struct instance",
 | 
			
		||||
      "$ref": "#/$defs/MyUnitStruct"
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  "required": [
 | 
			
		||||
    "my_int",
 | 
			
		||||
    "my_undocumented_bool",
 | 
			
		||||
    "my_unit"
 | 
			
		||||
  ],
 | 
			
		||||
  "$defs": {
 | 
			
		||||
    "MyUnitStruct": {
 | 
			
		||||
      "title": "A Unit",
 | 
			
		||||
      "type": "null"
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue