diff --git a/schemars/src/gen.rs b/schemars/src/gen.rs index 87ec2be..4f2d301 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/gen.rs @@ -121,9 +121,29 @@ impl SchemaGenerator { &self.settings } - // TODO document/rename - #[doc(hidden)] - pub fn objectify(&self, schema: SchemaObject) -> SchemaObject { + /// Returns a `SchemaObject` equivalent to the given `schema` which may have validation, metadata or other properties set on it. + /// + /// If `schema` is not a `$ref` schema, then this returns `schema` unmodified. Otherwise, depending on this generator's settings, + /// this may wrap the `$ref` in another schema. This is required because in many JSON Schema implementations, a schema with `$ref` + /// set may not include other properties. + /// + /// # Example + /// ``` + /// use schemars::{gen::SchemaGenerator, schema::SchemaObject}; + /// + /// let gen = SchemaGenerator::default(); + /// + /// let ref_schema = SchemaObject::new_ref("foo".to_owned()); + /// assert!(ref_schema.is_ref()); + /// + /// let extensible_schema = gen.make_extensible(ref_schema.clone()); + /// assert_ne!(ref_schema, extensible_schema); + /// assert!(!extensible_schema.is_ref()); + /// + /// let extensible_schema2 = gen.make_extensible(extensible_schema.clone()); + /// assert_eq!(extensible_schema, extensible_schema2); + /// ``` + pub fn make_extensible(&self, schema: SchemaObject) -> SchemaObject { if schema.is_ref() { SchemaObject { subschemas: Some(Box::new(SubschemaValidation { diff --git a/schemars/src/json_schema_impls/core.rs b/schemars/src/json_schema_impls/core.rs index 7e8479c..209f8ae 100644 --- a/schemars/src/json_schema_impls/core.rs +++ b/schemars/src/json_schema_impls/core.rs @@ -34,7 +34,7 @@ impl JsonSchema for Option { } } if gen.settings().option_nullable { - let mut schema_obj = gen.objectify(schema.into()); + let mut schema_obj = gen.make_extensible(schema.into()); schema_obj .extensions .insert("nullable".to_owned(), json!(true)); diff --git a/schemars_derive/src/metadata.rs b/schemars_derive/src/metadata.rs index 1cf7b62..61b1d4c 100644 --- a/schemars_derive/src/metadata.rs +++ b/schemars_derive/src/metadata.rs @@ -58,7 +58,7 @@ pub fn set_metadata_on_schema(schema_expr: TokenStream, metadata: &SchemaMetadat quote! { { let schema = #schema_expr.into(); - let mut schema_obj = gen.objectify(schema); + let mut schema_obj = gen.make_extensible(schema); let mut metadata = schema_obj.metadata(); #(#setters)* schemars::schema::Schema::Object(schema_obj)